-----client---------haproxy-------mysql1---------mysql2------mysql
192.168.1.250 192.168.1.1 192.168.1.10 192.168.1.20linux
1、安裝mysqlredis
[root@localhost ~]#tar -zxvf bison-2.5.tar.gzsql
[root@localhost ~]#./configure && make&& make install數據庫
[root@localhost ~]#tar -zxvf cmake-2.8.7.tar.gzbootstrap
[root@localhost ~]#./bootstrap && gmake && gmake installvim
[root@localhost ~]#tar -zxvf mysql-5.5.22.tar.gz服務器
[root@localhost ~]#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -socket
DSYSCONFDIR=/etc/ -DDEFAULT_CHARSET=utf8 -tcp
DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all
[root@localhost ~]#make && make install
初始化mysql:
[root@localhost ~]# cp support-files/my-medium.cnf /etc/my.cnf
[root@localhost ~]# cd scripts
[root@localhost ~]# ./mysql_install_db --basedir=/usr/local/mysql/ --
datadir=/usr/local/mysql/data/ --user=mysql
[root@localhost ~]# cp ../support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
[root@localhost ~]# mysqladmin -u root password 'redhat'
登陸測試:
[root@localhost ~]# mysql -u root -p
建立數據庫:
[root@localhost ~]# mysql> createdata server1;
固然你也可使用yum的方式安裝mysql
[root@localhost ~]# yum -y install mysql mysql-devel mysql-server
2、安裝haproxy
一、安裝
[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# tar -zxvf haproxy-1.4.24.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/haproxy-1.4.24/
[root@localhost ~]# make TARGET=linux26 PREFIX=/usr/local/haproxy
注意:linux26 是指linux 的內核版本號。
[root@localhost ~]# make install PREFIX=/usr/local/haproxy
二、配置haproxy
[root@localhost ~]# mkdir /etc/haproxy
[root@localhost ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
修改:
global
log 127.0.0.1 local0 //配置日誌記錄,local0 爲日誌設備,默認存放到系統日誌
log 127.0.0.1 local1 notice //notice 爲日誌級別,一般有7 個級別
#log loghost local0 info
maxconn 4096 //默認最大鏈接數,需考慮ulimit-n 限制 :可增長ulimitn
819200 #ulimit 的數量限制
chroot /usr/share/haproxy //運行路徑
uid 99
gid 99
#debug
#quiet
defaults
log global //定義日誌爲global 中的日誌
mode tcp //模式爲四層
option tcplog //採用http 的日誌格式
option dontlognull //不記錄健康檢查日誌信息
retries 3 //三次鏈接失敗就認爲是服務器不可用,也能夠經過後面設置
#redispatch
maxconn 2000 //最大鏈接數
contimeout 5000 //鏈接超時時間
clitimeout 50000 //客戶端超時時間
srvtimeout 50000 //服務端超時時間
listen stats
mode http
bind :6677
stats enable
stats hide-version
stats uri /haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
listen mysqlcluster 0.0.0.0:3306
balance roundrobin
server m1 192.168.56.202:3306 check port 3306 maxconn 300
server m2 192.168.56.203:3306 check port 3306 maxconn 300
注意:
若是啓動時出現報錯:/haproxy.main()] Cannot chroot(/usr/share/haproxy)
則手動建立:
[root@localhost ~]# mkdir /usr/share/haproxy
若是啓動時出現報錯:Starting proxy cacti: cannot bind socket
則執行:
[root@localhost ~]# sysctl -e net.ipv4.ip_nonlocal_bind=1
三、啓動haproxy
[root@localhost ~]# ln -s /usr/local/haproxy/sbin/* /usr/sbin/ //注意軟連接的目錄
[root@localhost ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init
/etc/init.d/haproxy
[root@localhost ~]# chmod +x /etc/init.d/haproxy
[root@localhost ~]# /etc/init.d/haproxy start
[root@localhost ~]# /etc/init.d/haproxy status
[root@localhost ~]# netstat -anp | grep haproxy //佔用的也是TCP 的80 端口
[root@localhost ~]# chkconfig --add haproxy
[root@localhost ~]# chkconfig haproxy on
http://192.168.56.200:6677/haproxyadmin?stats 查看集羣的狀態
4、MySql 受權用戶登陸(集羣內的mysql 都要受權)
mysql> GRANT ALL ON *.* TO 'root'@'192.168.56.%' IDENTIFIED BY 'redhat';
mysql> flush privileges;
5、測試:
[root@localhost ~]# mysql -u root -h 192.168.56.200 -p
登陸兩次分別查看
可增長keepalived