HAProxy 实现 mysql 负载均衡

时间:2023-01-30 07:12:07

通过yum 安装和配置HAProxy

# yum install -y haproxy     #安装haproxy
# rpm -qa | grep haproxy     #查看安装的haproxy信息
# rpm -ql haproxy-1.5.18-7.el7.x86_64     #查看配置文件位置
# vim /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
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 http
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
#---------------------------------------------------------------------
#frontend main *:5000
# acl url_static path_beg -i /static /images /javascript /stylesheets
# acl url_static path_end -i .jpg .gif .png .css .js
# use_backend static if url_static
# default_backend app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
# balance roundrobin
# server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
listen mysql
bind 0.0.0.0:5000 #绑定端口
mode tcp
balance roundrobin #轮询
server mysql3316 xxx.xxx.xxx.xxx:3316 check inter 2000 fall 3 weight 30
server mysql3326 xxx.xxx.xxx.xxx:3326 check inter 2000 fall 3 weight 30
# systemctl start haproxy #启动haproxy
# systemctl status haproxy #查看haproxy服务状态

新建数据库连接

HAProxy 实现 mysql 负载均衡

连接数据库,查询数据

HAProxy 实现 mysql 负载均衡

断开数据库重连,查询数据

HAProxy 实现 mysql 负载均衡

验证负载均衡配置成功