1. 程式人生 > 其它 >MySQL使用MaxScale實現讀寫分離

MySQL使用MaxScale實現讀寫分離

1.資料庫節點安裝mysql
[root@mysql ~]# yum install https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm -y
[root@mysql ~]# yum clean all && yum makecache
[root@mysql ~]# yum list | grep mysql-community
[root@mysql ~]# yum install mysql-community-* --exclude=mysql-community-release
[root@mysql ~]# grep password /var/log/mysqld.log

mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=4;
mysql> alter user root@"localhost" identified by"123456";
mysql> flush privileges;

2.配置主從同步(略)
3.主庫上建立監控賬號、路由賬號和測試賬號
mysql> grant replication slave, replication client on *.* to scalemon@'%' identified by '123456';
mysql> grant select on *.* to maxscale@'%' identified by '123456';
mysql> grant all privileges on *.* to test@'%' identified by '123456';
mysql> flush privileges;

4.maxscale節點安裝maxscale
[root@maxscale ~]# wget https://downloads.mariadb.com/MaxScale/centos/7/x86_64/maxscale-2.3.7-1.centos.7.x86_64.rpm
[root@maxscale ~]# yum localinstall -y maxscale-2.3.7-1.centos.7.x86_64.rpm

5.修改配置檔案
[root@maxscale ~]# vim /etc/maxscale.cnf
# MaxScale documentation on GitHub:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Documentation-Contents.md

# Global parameters
#
# Complete list of configuration options:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Getting-Started/Configuration-Guide.md

[maxscale]
threads=auto # 修改執行緒數

# Server definitions
#
# Set the address of the server to the network
# address of a MySQL server.
#

[server1]
type=server
address=172.25.25.3 # node1的ip
port=3306
protocol=MySQLBackend

[server2]
type=server
address=172.25.25.4 # node2的ip
port=3306
protocol=MySQLBackend

[server3]
type=server
address=172.25.25.5 # node3的ip
port=3306
protocol=MySQLBackend

# Monitor for the servers
#
# This will keep MaxScale aware of the state of the servers.
# MySQL Monitor documentation:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Monitors/MySQL-Monitor.md

[MySQL Monitor]
type=monitor
module=mysqlmon
servers=server1,server2,server3 # 叢集的所有server
user=scalemon # 監控賬號
passwd=123456 # 監控賬號密碼
monitor_interval=10000 # 監控的時間間隔,單位為毫秒

assume_unique_hostnames=false # 假定唯一主機名設定為false
#https://stackoverflow.com/questions/60525604/maxscale-does-not-split-select-queries

# Service definitions
#
# Service Definition for a read-only service and
# a read/write splitting service.
#

# ReadConnRoute documentation:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Routers/ReadConnRoute.md

# [Read-Only Service] # 讀負載均衡模組,由於讀寫分離模組也能實現讀負載均衡,因此註釋掉該模組
# type=service
# router=readconnroute
# servers=server1
# user=myuser
# passwd=mypwd
# router_options=slave

# ReadWriteSplit documentation:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Routers/ReadWriteSplit.md

[Read-Write Service]
type=service
router=readwritesplit
servers=server1,server2,server3 # 叢集的所有server
user=maxscale # 路由賬號
passwd=123456 # 路由賬號密碼
max_slave_connections=100% # 多少比例的從伺服器被使用,預設就是所有的從伺服器都提供讀服務

# This service enables the use of the MaxAdmin interface
# MaxScale administration guide:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Reference/MaxAdmin.md

[MaxAdmin Service]
type=service
router=cli

# Listener definitions for the services
#
# These listeners represent the ports the
# services will listen on.
#

# [Read-Only Listener] # 註釋該模組
# type=listener
# service=Read-Only Service
# protocol=MySQLClient
# port=4008

[Read-Write Listener]
type=listener
service=Read-Write Service
protocol=MySQLClient
port=4006

[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
# socket=default # 註釋該socket
port=6603 # 為maxadmin選擇一個埠

6.啟動maxscale
[root@maxscale ~]# systemctl start maxscale.service

# 預設的使用者名稱和密碼是admin/mariadb
[root@maxscale ~]# maxadmin --user=admin --password=mariadb
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
-------------------+-----------------+-------+-------------+--------------------
server1 | 172.25.25.3 | 3306 | 0 | Master, Running
server2 | 172.25.25.4 | 3306 | 0 | Slave of External Server, Running
server3 | 172.25.25.5 | 3306 | 0 | Slave of External Server, Running
-------------------+-----------------+-------+-------------+--------------------
MaxScale> show dbusers Read-Write-Service
User names (Read-Write-Listener): mysql.session@localhost mysql.sys@localhost [email protected].% scalemon@% maxscale@% mysql.session@localhost

7.測試讀寫分離

[root@maxscale ~]# mysql -utest -p123456 -h172.25.25.2 -P4006
mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| master02 |
+------------+
1 row in set (0.01 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| master01 |
+------------+
1 row in set (0.00 sec)

mysql> rollback;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| master02 |
+------------+
1 row in set (0.00 sec)

8.測試讀取負載均衡

[root@maxscale ~]# mysql -utest -p123456 -h172.25.25.2 -P4006 -BNe "select @@hostname;select sleep(1000)"
mysql: [Warning] Using a password on the command line interface can be insecure.
master02

另開一個終端
[root@maxscale ~]# mysql -utest -p123456 -h172.25.25.2 -P4006 -BNe "select @@hostname;select sleep(1000)"
mysql: [Warning] Using a password on the command line interface can be insecure.
master03

另開一個終端
[root@maxscale ~]# mysql -utest -p123456 -h172.25.25.2 -P4006 -BNe "select @@hostname;select sleep(1000)"
mysql: [Warning] Using a password on the command line interface can be insecure.
master02

資料庫伺服器上檢視程序(略)
mysql> show processlist;

9.模擬從庫故障

節點2停止salve
mysql> stop slave;

MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
-------------------+-----------------+-------+-------------+--------------------
server1 | 172.25.25.3 | 3306 | 0 | Master, Running
server2 | 172.25.25.4 | 3306 | 0 | Running
server3 | 172.25.25.5 | 3306 | 0 | Slave, Running
-------------------+-----------------+-------+-------------+--------------------

[root@maxscale ~]# mysql -utest -p123456 -h172.25.25.2 -P4006 -BNe "select @@hostname;select sleep(1000)"
mysql: [Warning] Using a password on the command line interface can be insecure.
master03

另開一個終端
[root@maxscale ~]# mysql -utest -p123456 -h172.25.25.2 -P4006 -BNe "select @@hostname;select sleep(1000)"
mysql: [Warning] Using a password on the command line interface can be insecure.
master03

另開一個終端
[root@maxscale ~]# mysql -utest -p123456 -h172.25.25.2 -P4006 -BNe "select @@hostname;select sleep(1000)"
mysql: [Warning] Using a password on the command line interface can be insecure.
master03

可以看到僅由節點3提供讀服務

節點3停止salve
mysql> stop slave;

MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
-------------------+-----------------+-------+-------------+--------------------
server1 | 172.25.25.3 | 3306 | 3 | Master, Running
server2 | 172.25.25.4 | 3306 | 0 | Running
server3 | 172.25.25.5 | 3306 | 0 | Running
-------------------+-----------------+-------+-------------+--------------------

[root@maxscale ~]# mysql -utest -p123456 -h172.25.25.2 -P4006 -BNe "select @@hostname;select sleep(1000)"
mysql: [Warning] Using a password on the command line interface can be insecure.
master01

另開一個終端
[root@maxscale ~]# mysql -utest -p123456 -h172.25.25.2 -P4006 -BNe "select @@hostname;select sleep(1000)"
mysql: [Warning] Using a password on the command line interface can be insecure.
master01

另開一個終端
[root@maxscale ~]# mysql -utest -p123456 -h172.25.25.2 -P4006 -BNe "select @@hostname;select sleep(1000)"
mysql: [Warning] Using a password on the command line interface can be insecure.
master01

可以看到僅由主庫提供讀服務

恢復
mysql> start slave;
mysql> start slave;

10.測試從庫延遲
主庫重新授權
mysql> grant all on *.* to scalemon@'%' identified by '123456';
mysql> flush privileges;

修改配置檔案
[root@maxscale ~]# vim /etc/maxscale.cnf
[MySQL Monitor]
detect_replication_lag=1 # 是否檢查從伺服器的延遲

[Read-Write Service]
max_slave_replication_lag=60 # 如果從伺服器的延遲大於該值,就不向該從伺服器轉發連線,單位秒
# 這個引數必須和detect_replication_lag一起設定

節點2鎖表
mysql> flush table with read lock;

主庫寫入資料
mysql> use test;
mysql> insert into user values(3,"wangwu");

等待60秒驗證
[root@maxscale ~]# mysql -utest -p123456 -h172.25.25.2 -P4006 -BNe "select @@hostname;select sleep(1000)"
mysql: [Warning] Using a password on the command line interface can be insecure.
master03

另開一個終端
[root@maxscale ~]# mysql -utest -p123456 -h172.25.25.2 -P4006 -BNe "select @@hostname;select sleep(1000)"
mysql: [Warning] Using a password on the command line interface can be insecure.
master03

另開一個終端
[root@maxscale ~]# mysql -utest -p123456 -h172.25.25.2 -P4006 -BNe "select @@hostname;select sleep(1000)"
mysql: [Warning] Using a password on the command line interface can be insecure.
master03

可以看到僅由節點3提供讀服務

節點2解鎖表
mysql> unlock table;