1. 程式人生 > >Haproxy Mysql cluster 高可用Mysql集群

Haproxy Mysql cluster 高可用Mysql集群

最大 default fault 修改 日誌記錄 true chroot check linux


-----client-----------haproxy---------mysql1----------mysql2------
192.168.1.250 192.168.1.1 192.168.1.10 192.168.1.20

一、安裝mysql
[root@localhost ~]#tar -zxvf bison-2.5.tar.gz
[root@localhost ~]#./configure && make&& make install
[root@localhost ~]#tar -zxvf cmake-2.8.7.tar.gz
[root@localhost ~]#./bootstrap && gmake && gmake install
[root@localhost ~]#tar -zxvf mysql-5.5.22.tar.gz
[root@localhost ~]#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc/ -DDEFAULT_CHARSET=utf8 -
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;


二、安裝haproxy
1、安裝
[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
2、配置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限制 :可增加ulimit-n 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
3、啟動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 查看集群的狀態


四、MySql授權用戶登錄(集群內的mysql都要授權)
mysql> GRANT ALL ON *.* TO ‘root‘@‘192.168.56.%‘ IDENTIFIED BY ‘redhat‘;
mysql> flush privileges;


五、測試:
[root@localhost ~]# mysql -u root -h 192.168.56.200 -p
登錄兩次分別查看
可增加keepalived

Haproxy Mysql cluster 高可用Mysql集群