HAProxy 實現 MySQL 的負載均衡
使用 HAProxy 實現 MySQL 的負載均衡:
1、DS 服務器、負載均衡服務器停掉 LVS 負載均衡的配置:
[root@server04 ~]# ./lvs_dr_ds_mysql.sh stop
[root@server04 ~]#
[root@server04 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@server04 ~]#
2、修改 haproxy 配置:
1)將 http(七層)改成 tcp(四層):
2)修改前端端口,從 80 改到 3306:
3)修改後端 app 的定義:
3、啓動 haproxy:
[root@server04 haproxy]# service haproxy start
Redirecting to /bin/systemctl start haproxy.service
4、檢查 haproxy 進程:
[root@server04 haproxy]# ps aux |grep haproxy
root 45777 0.0 0.0 44744 1748 ? Ss 20:55 0:00 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
haproxy 45778 0.0 0.1 48532 3180 ? S 20:55 0:00 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
haproxy 45779 0.0 0.1 48672 2040 ? Ss 20:55 0:03 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
root 46657 0.0 0.0 112812 972 pts/0 S+ 23:51 0:00 grep --color=auto haproxy
5、啓動出現一個錯誤:
檢查發現是 mysql 佔用了 3306,將 mysql 停掉,問題得到解決:
[root@server04 haproxy]# systemctl start haproxy
[root@server04 haproxy]#
[root@server04 haproxy]#
[root@server04 haproxy]# systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2025-05-11 00:01:38 CST; 6s ago
Main PID: 46812 (haproxy-systemd)
CGroup: /system.slice/haproxy.service
├─46812 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy...
├─46813 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
└─46814 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
May 11 00:01:38 server04 systemd[1]: Started HAProxy Load Balancer.
May 11 00:01:38 server04 haproxy-systemd-wrapper[46812]: haproxy-systemd-wrapper: executing /u...Ds
May 11 00:01:38 server04 haproxy-systemd-wrapper[46812]: [WARNING] 130/000138 (46813) : parsin...'.
May 11 00:01:38 server04 haproxy-systemd-wrapper[46812]: [WARNING] 130/000138 (46813) : config...e.
May 11 00:01:38 server04 haproxy-systemd-wrapper[46812]: [WARNING] 130/000138 (46813) : config...e.
May 11 00:01:38 server04 haproxy-systemd-wrapper[46812]: [WARNING] 130/000138 (46813) : config...e.
Hint: Some lines were ellipsized, use -l to show in full.
[root@server04 haproxy]# ps -ef |grep haproxy
root 46812 1 0 00:01 ? 00:00:00 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
haproxy 46813 46812 0 00:01 ? 00:00:00 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
haproxy 46814 46813 0 00:01 ? 00:00:00 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
root 46817 45183 0 00:01 pts/0 00:00:00 grep --color=auto haproxy
6、檢查 haproxy 的端口:
[root@server04 haproxy]# netstat -atunlp |grep haproxy
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 46814/haproxy
udp 0 0 0.0.0.0:47296 0.0.0.0:* 46813/haproxy
7、進行 mysql 客戶端連接測試:
[root@server02 bin]# ./mysql -h 192.168.17.205 -ulvs -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.33 Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| server01 |
| tp5shop |
+--------------------+
5 rows in set (0.00 sec)
mysql> exit
Bye
[root@server02 bin]# ./mysql -h 192.168.17.205 -ulvs -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.33 Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| server03 |
| tp5shop |
+--------------------+
5 rows in set (0.00 sec)
mysql>
測試 haproxy 實現 mysql 負載均衡。
我們在這個示例中,我們也知道了四層和七層的修改配置,也知道了前端和後端的配置。
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/K6nfQKY-PPW7BB5BLbsgPg