3.配置代理 # vi /etc/haproxy/haproxy.cfg global log 127.0.0.1 local2
chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon
# turn on stats unix socket stats socket /var/lib/haproxy/stats
#--------------------------------------------------------------------- # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- defaults mode tcp log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000
#--------------------------------------------------------------------- # main frontend which proxys to the backends #--------------------------------------------------------------------- #指定mysql 集群 listen mysql #指定当前haproxy服务监听地址 0.0.0.0.0代表本机的所有IP, 3306为haproxy绑定的端口 bind 0.0.0.0:3306 #数据库代理需要实现 4层TCP端口代理, mode tcp #轮询负载算法 roundrobin轮询 least_conn最少连接 source根据源地址,类似与ip_hash balance roundrobin #msql1 自定义的后台服务器名 192.168.182.150:3306实际的后台服务地址及端口 #check port 3306进行mysql服务器的健康检查,如不写haproxy默认为健康的 maxconn 300指定最大连接数 server mysql1 192.168.182.150:3306 check port 3306 maxconn 3000 server mysql2 192.168.182.151:3306 check port 3306 maxconn 3000