安裝
yum install haproxy
修改配置
vi /etc/haproxy/haproxy.cfg
配置如下
global
daemon
nbproc 1
pidfile /var/run/haproxy.pid
defaults
mode tcp #默認的模式mode { tcp|http|health },tcp是4層,http是7層,health只會返回OK
retries 3 #兩次連接失敗就認為是服務器不可用,也可以通過后面設置
option redispatch #當serverId對應的服務器掛掉后,強制定向到其他健康的服務器
option abortonclose #當服務器負載很高的時候,自動結束掉當前隊列處理比較久的鏈接
maxconn 4096 #默認的最大連接數
timeout connect 5000ms #連接超時
timeout client 30000ms #客戶端超時
timeout server 30000ms #服務器超時
timeout check 2000 #=心跳檢測超時
log 127.0.0.1 local0 err #[err warning info debug]
listen configMysql
bind 127.0.0.1:3306
mode tcp
maxconn 4086
server s1 192.168.75.128:3306
server s2 192.168.75.130:3306
啟動
在解壓目錄下執行haproxy -f /etc/haproxy/haproxy.cfg
或者使用
/bin/systemctl start haproxy.service
查看狀態
/bin/systemctl status haproxy.service
需要注意的點:
1、端口3306不能被占用,如果本機有mysql,服務必須關閉。
2、/var/run/haproxy.pid的目錄權限給夠,如果存在haproxy.pid在第一次啟動前必選刪除。
3、其他錯誤請參考博客http://blog.csdn.net/aa168b/article/details/50372649
HAProxy厲害的點,不需要寫mysql的健康檢測,軟件本身可以檢測出mysql服務停止,然后切換到正常的服務器上,而且當mysql服務器重啟之后,HAProxy不需要重啟,也能識別並且切換上去。