haproxy實戰之haproxy實現mysql負載均衡
實驗目的
haproxy + mysql實現負載均衡
1.準備機器,做好時間同步,域名主機名解析
192.168.42.151 [node2 haproxy] 192.168.42.152 [node3 mariadb] 192.168.42.153 [node4 mariadb]
2.node3,node4上安裝mariadb
(1).安裝mariadb
yum install mariadb-server -y
(2).更改基本配置
vim /etc/my.cnf.d/server.cnf [mysqld] skip_name_resolve=1 log-bin=mysql-bin innodb_file_per_table = 1
(3).啟動mariadb
systemctl start mariadb
(4).更改密碼
mysqladmin -uroot -p password "root"
(5).登錄mysql
mysql -u root -p
(6).添加外部訪問賬號
MariaDB [(none)]> grant all privileges on *.* to 'magedu'@'192.168.42.%' identified by '123456'; MariaDB [(none)]> flush privileges;
(7)登錄測試一下
mysql -u magedu -h 192.168.42.155 -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 7 Server version: 5.5.52-MariaDB MariaDB Server Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
3.安裝haproxy
node1: (1).haproxy的安裝
yum install haproxy -y
(2).配置haproxy 配置文件如下:
#--------------------------------------------------------------------- # 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 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 #--------------------------------------------------------------------- frontend mysql bind *:3306 log global mode tcp default_backend mysqlsrvs backend mysqlsrvs balance roundrobin server mysql1 192.168.42.152:3306 check server mysql2 192.168.42.153:3306 check
(3)啟動haproxy
systemctl start haproxy [root@node2 haproxy]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 3000 *:3306 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::*
(4)安裝mariadb 客戶端工具
yum install mariadb -y
(5).測試
mysql -u maged -h 192.168.42.151 -p MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 此時是只有4個庫,我們創建一個庫,并退出 MariaDB [(none)]> create database leip; Query OK, 1 row affected (0.01 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | leip | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]> exit; Bye
(6).再一次連接測試
[root@node2 haproxy]# mysql -u magedu -h 192.168.42.151 -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 6 Server version: 5.5.52-MariaDB MariaDB Server Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) MariaDB [(none)]> exit Bye
從上面可以看到,此時我們的數據庫是只有4個庫了,因此我們可以判斷,我們連接的是兩個不同主機上的mariadb,也就是說負載均衡mysql成功了
原創文章,作者:srayban,如若轉載,請注明出處:http://www.www58058.com/78829