mysql負載均衡搭建(haproxy)

HelloWorld-Q發表於2020-12-08

從庫192.168.110(安裝haproxy)
從庫192.168.111(安裝haproxy)

Haproxy安裝

yum install haproxy –y

配置檔案:/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
    stats socket /var/lib/haproxy/stats

defaults
    mode                   tcp
    log                     global
    option                  tcplog
    option                  dontlognull
    option http-server-close
    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

frontend    mysql
    bind        0.0.0.0:3307
    mode       tcp
    log             global
    default_backend         mysql_server

backend     mysql_server
    balance     roundrobin
    server  mysql1 192.168.8.110:3306 check inter 5s rise 2 fall 3
    server  mysql2 192.168.8.111:3306 check inter 5s rise 2 fall 3

listen stats
    mode http
    bind 0.0.0.0:1080
    stats enable
    stats hide-version
    stats uri /haproxyadmin?stats
    stats realm Haproxy\ Statistics
    stats auth admin:admin
    stats admin if TRUE

啟動命令:

/usr/sbin/haproxy –f /etc/haproxy/haproxy.cfg

檢視haproxy是否正常啟動

ps –ef | grep haproxy

位址列訪問

192.168.8.111:1080/haproxyadmin?sta...
mysql負載均衡搭建(haproxy)

本地mysql測試(輪詢)

>mysql -h192.168.8.111 -uqiao -proot -P3307  -e "show variables like 'server_id'"

>mysql -h192.168.8.111 -uqiao -proot -P3307  -e "show variables like 'server_id'"
D:\phpstudy_pro\Extensions\MySQL5.7.26\bin>mysql -h192.168.8.111 -uqiao -proot -P3307  -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 2     |
+---------------+-------+

D:\phpstudy_pro\Extensions\MySQL5.7.26\bin>mysql -h192.168.8.111 -uqiao -proot -P3307  -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 3     |
+---------------+-------+

D:\phpstudy_pro\Extensions\MySQL5.7.26\bin>mysql -h192.168.8.110 -uqiao -proot -P3307  -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 3     |
+---------------+-------+

D:\phpstudy_pro\Extensions\MySQL5.7.26\bin>mysql -h192.168.8.110 -uqiao -proot -P3307  -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 2     |
+---------------+-------+

D:\phpstudy_pro\Extensions\MySQL5.7.26\bin>

本作品採用《CC 協議》,轉載必須註明作者和本文連結
有夢想的人睡不著,沒有夢想的人睡不醒。

相關文章