您好,登錄后才能下訂單哦!
基于GTID的主從replication并配合MHA搭建高可用架構,請參考之前的博客:http://linzhijian.blog.51cto.com/1047212/1906434。這里只敘述如何在此基礎上增加maxscale中間件,實現讀寫分離的功能。
MaxScale是maridb開發的一個MySQL數據中間件,其配置簡單,能夠實現讀寫分離,并且可以根據主從狀態實現寫庫的自動切換。官方文檔:https://mariadb.com/kb/en/mariadb-enterprise/about-mariadb-maxscale/
測試環境簡要介紹:
master:192.168.110.131:3306
slave1: 192.168.110.132:3306
slave2: 192.168.110.130:3306
maxscale: 192.168.110.132
maxscale安裝:
1、依賴包安裝:
yum install libaio.x86_64 libaio-devel.x86_64 novacom-server.x86_64 libedit -y
2、maxscale包下載:
https://downloads.mariadb.com/files/MaxScale maxscale-2.0.5-1.centos.6.x86_64.rpm
3、創建監控用戶:
mysql> create user scalemon@'%' identified by "scalemon"; mysql> grant replication slave, replication client on *.* to scalemon@'%';
4、創建路由用戶:
mysql> create user scaleroute@'%' identified by "scaleroute"; mysql> grant select on mysql.* to scaleroute@'%'; mysql> grant show databases on *.* to 'scaleroute'@'%';
5、修改配置文件:
vim /etc/maxscale.conf
# MaxScale documentation on GitHub: # https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Documentation-Contents.md # Global parameters # # Complete list of configuration options: # https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Getting-Started/Configuration-Guide.md [maxscale] threads=1 log_info=1 logdir=/tmp/ # Server definitions # # Set the address of the server to the network # address of a MySQL server. # [server1] type=server address=192.168.110.131 port=3306 protocol=MySQLBackend [server2] type=server address=192.168.110.132 port=3306 protocol=MySQLBackend [server3] type=server address=192.168.110.130 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/master/Documentation/Monitors/MySQL-Monitor.md [MySQL Monitor] type=monitor module=mysqlmon servers=server1,server2,server3 user=scalemon passwd=scalemon monitor_interval=10000 # Service definitions # # Service Definition for a read-only service and # a read/write splitting service. # # ReadConnRoute documentation: # https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Routers/ReadConnRoute.md #[Read-Only Service] #type=service #router=readconnroute #servers=server2,server3 #user=scaleroute #passwd=scaleroute #router_options=slave # ReadWriteSplit documentation: # https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Routers/ReadWriteSplit.md [Read-Write Service] type=service router=readwritesplit servers=server1,server2,server3 user=scaleroute passwd=scaleroute max_slave_connections=100% # This service enables the use of the MaxAdmin interface # MaxScale administration guide: # https://github.com/mariadb-corporation/MaxScale/blob/master/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
6、啟動服務:
service maxscale start 或者 maxscale --conf=/etc/maxscale.conf
7、檢查maxscale日志:
cat /tmp/maxscale1.log
MariaDB Corporation MaxScale /tmp/maxscale1.log Fri Apr 7 12:26:23 2017 ----------------------------------------------------------------------- 2017-04-07 12:26:23 notice : Working directory: /tmp 2017-04-07 12:26:23 notice : MariaDB MaxScale 2.0.5 started 2017-04-07 12:26:23 notice : MaxScale is running in process 10866 2017-04-07 12:26:23 notice : Configuration file: /etc/maxscale.cnf 2017-04-07 12:26:23 notice : Log directory: /tmp 2017-04-07 12:26:23 notice : Data directory: /var/lib/maxscale 2017-04-07 12:26:23 notice : Module directory: /usr/lib64/maxscale 2017-04-07 12:26:23 notice : Service cache: /var/cache/maxscale 2017-04-07 12:26:23 notice : The logging of informational messages has been enabled. 2017-04-07 12:26:23 notice : Initialise CLI router module V1.0.0. 2017-04-07 12:26:23 notice : Loaded module cli: V1.0.0 from /usr/lib64/maxscale/libcli.so 2017-04-07 12:26:23 notice : Initializing statemend-based read/write split router module. 2017-04-07 12:26:23 notice : Loaded module readwritesplit: V1.1.0 from /usr/lib64/maxscale/libreadwritesplit.so 2017-04-07 12:26:23 notice : Initialise the MySQL Monitor module V1.4.0. 2017-04-07 12:26:23 notice : Loaded module mysqlmon: V1.4.0 from /usr/lib64/maxscale/libmysqlmon.so 2017-04-07 12:26:23 notice : No query classifier specified, using default 'qc_sqlite'. 2017-04-07 12:26:23 notice : Loaded module qc_sqlite: V1.0.0 from /usr/lib64/maxscale/libqc_sqlite.so 2017-04-07 12:26:23 info : qc_sqlite loaded. 2017-04-07 12:26:23 info : qc_sqlite: In-memory sqlite database successfully opened for thread 140338182019040. 2017-04-07 12:26:23 notice : Encrypted password file /var/lib/maxscale/.secrets can't be accessed (No such file or directory). Password encryption is not used. 2017-04-07 12:26:23 info : Notification service feedback is not enabled. 2017-04-07 12:26:23 info : Read-Write Service: User scaleroute@% for database mysql added to service user table. 2017-04-07 12:26:23 info : Read-Write Service: User repl@192.168.% for database no db added to service user table. 2017-04-07 12:26:23 info : Read-Write Service: User linzj@192.168.110.% for database ANY added to service user table. 2017-04-07 12:26:23 info : Read-Write Service: User mha@192.168.110.% for database ANY added to service user table. 2017-04-07 12:26:23 info : Read-Write Service: User plum@192.168.110.% for database ANY added to service user table. 2017-04-07 12:26:23 info : Read-Write Service: User plum@192.168.110.131 for database no db added to service user table. 2017-04-07 12:26:23 info : Read-Write Service: User test@127.0.0.1 for database ANY added to service user table. 2017-04-07 12:26:23 info : Read-Write Service: User scalemon@% for database no db added to service user table. 2017-04-07 12:26:23 notice : Loaded 8 MySQL Users for service [Read-Write Service]. 2017-04-07 12:26:23 notice : Loaded module MySQLClient: V1.1.0 from /usr/lib64/maxscale/libMySQLClient.so 2017-04-07 12:26:23 notice : Listening connections at 0.0.0.0:4006 with protocol MySQL 2017-04-07 12:26:23 info : Started session [0] for Read-Write Service service 2017-04-07 12:26:23 info : Initialise MaxScaled Protocol module. 2017-04-07 12:26:23 notice : Loaded module maxscaled: V2.0.0 from /usr/lib64/maxscale/libmaxscaled.so 2017-04-07 12:26:23 notice : Listening connections at /tmp/maxadmin.sock with protocol MaxScale Admin 2017-04-07 12:26:23 info : Started session [0] for MaxAdmin Service service 2017-04-07 12:26:23 notice : MaxScale started with 1 server threads. 2017-04-07 12:26:23 notice : Started MaxScale log flusher. 2017-04-07 12:26:23 notice : Server changed state: server1[192.168.110.131:3306]: new_master. [Running] -> [Master, Running] 2017-04-07 12:26:23 notice : Server changed state: server2[192.168.110.132:3306]: new_slave. [Running] -> [Slave, Running] 2017-04-07 12:26:23 notice : Server changed state: server3[192.168.110.130:3306]: new_slave. [Running] -> [Slave, Running] 2017-04-07 12:26:23 notice : A Master Server is now available: 192.168.110.131:3306
8、登陸maxscale管理器,檢查后端數據庫狀態信息:
maxadmin -S /tmp/maxadmin.sock
MaxScale> list servers Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.110.131 | 3306 | 0 | Master, Running server2 | 192.168.110.132 | 3306 | 0 | Slave, Running server3 | 192.168.110.130 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+-------------------- MaxScale> list services Services. --------------------------+----------------------+--------+--------------- Service Name | Router Module | #Users | Total Sessions --------------------------+----------------------+--------+--------------- Read-Write Service | readwritesplit | 1 | 1 MaxAdmin Service | cli | 2 | 2 --------------------------+----------------------+--------+--------------- MaxScale>
9、驗證maxscale的monitor插件:
關閉mysql2的數據庫服務:sh /home/linzj/shell/mysql.sh stop
MaxScale> list servers Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.110.131 | 3306 | 0 | Master, Running server2 | 192.168.110.132 | 3306 | 0 | Slave, Running server3 | 192.168.110.130 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+-------------------- MaxScale> list servers Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.110.131 | 3306 | 0 | Master, Running server2 | 192.168.110.132 | 3306 | 0 | Down server3 | 192.168.110.130 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+--------------------
重新拉起mysql2的數據庫服務: sh /home/linzh/shell/mysql.sh start
MaxScale> list servers Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.110.131 | 3306 | 0 | Master, Running server2 | 192.168.110.132 | 3306 | 0 | Down server3 | 192.168.110.130 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+-------------------- MaxScale> list servers Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.110.131 | 3306 | 0 | Master, Running server2 | 192.168.110.132 | 3306 | 0 | Running server3 | 192.168.110.130 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+--------------------
由于配置了skip_slave_start = 1這個參數,mysql2重啟后需要手工start slave啟動復制線程。
mysql> start slave;
MaxScale> list servers Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.110.131 | 3306 | 0 | Master, Running server2 | 192.168.110.132 | 3306 | 0 | Running server3 | 192.168.110.130 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+-------------------- MaxScale> list servers Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.110.131 | 3306 | 0 | Master, Running server2 | 192.168.110.132 | 3306 | 0 | Slave, Running server3 | 192.168.110.130 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+--------------------
10、驗證讀寫分離:
[root@ansible log]# mysql -ulinzj -plinzj -P4006 -h 192.168.110.130 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 11069 Server version: 5.5.5-10.0.0 2.0.5-maxscale 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. linzj@192.168.110.130:4006 12:54: [(none)]> linzj@192.168.110.130:4006 12:54: [(none)]> linzj@192.168.110.130:4006 12:54: [(none)]> linzj@192.168.110.130:4006 12:54: [(none)]>select @@hostname; +------------+ | @@hostname | +------------+ | mysql2 | +------------+ 1 row in set (0.00 sec) linzj@192.168.110.130:4006 12:54: [(none)]>begin; Query OK, 0 rows affected (0.00 sec) linzj@192.168.110.130:4006 12:54: [(none)]>select @@hostname; +------------+ | @@hostname | +------------+ | mysql1 | +------------+ 1 row in set (0.00 sec) linzj@192.168.110.130:4006 12:54: [(none)]>rollback; Query OK, 0 rows affected (0.00 sec) linzj@192.168.110.130:4006 12:54: [(none)]>select @@hostname; +------------+ | @@hostname | +------------+ | mysql2 | +------------+ 1 row in set (0.00 sec) linzj@192.168.110.130:4006 12:54: [(none)]>select @@hostname; +------------+ | @@hostname | +------------+ | mysql2 | +------------+ 1 row in set (0.00 sec)
從select的結果可知:讀操作都路由到mysql2(slave)上去執行,而寫操作路由到mysql1(master)上去執行,讀寫分離功能實現。
11、MHA高可用情形一(master crash)
環境:啟動MHA manager進程, 手工pkill mysql1的數據庫服務
MaxScale> list servers Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.110.131 | 3306 | 0 | Master, Running server2 | 192.168.110.132 | 3306 | 0 | Slave, Running server3 | 192.168.110.130 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+-------------------- MaxScale> list servers Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.110.131 | 3306 | 0 | Down server2 | 192.168.110.132 | 3306 | 0 | Master, Running server3 | 192.168.110.130 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+-------------------
[root@ansible shell]# tail /var/log/masterha/app1/manager.log Check MHA Manager logs at ansible:/var/log/masterha/app1/manager.log for details. Started automated(non-interactive) failover. Invalidated master IP address on mysql1(192.168.110.131:3306) Selected mysql2(192.168.110.132:3306) as a new master. mysql2(192.168.110.132:3306): OK: Applying all logs succeeded. mysql2(192.168.110.132:3306): OK: Activated master IP address. ansible(192.168.110.130:3306): OK: Slave started, replicating from mysql2(192.168.110.132:3306) mysql2(192.168.110.132:3306): Resetting slave info succeeded. Master failover to mysql2(192.168.110.132:3306) completed successfully.
從MHA的failover日志可知,mysql1主庫down,mysql2升級成新的master主庫。從maxscale管理界面上也可以看到,maxscale也能正確地識別了后端數據庫的狀態。
12、MHA高可用情形二(online change)
環境:繼續上面的例子,目前mysql2為主庫,mysql1重啟后根據MHA的failover日志的信息重新配置change master作為從庫加入集群。
MaxScale> list servers Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.110.131 | 3306 | 0 | Running server2 | 192.168.110.132 | 3306 | 0 | Master, Running server3 | 192.168.110.130 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+-------------------- MaxScale> list servers Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.110.131 | 3306 | 0 | Slave, Running server2 | 192.168.110.132 | 3306 | 0 | Master, Running server3 | 192.168.110.130 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+--------------------
采用MHA online change的方式,將mysql1重新升級成主庫。
shell>sh /home/linzj/shell/masterha_switch.sh manual_online_change
MHA Manager is not running on app1(2:NOT_RUNNING). Fri Apr 7 18:00:21 2017 - [info] MHA::MasterRotate version 0.56. Fri Apr 7 18:00:21 2017 - [info] Starting online master switch.. Fri Apr 7 18:00:21 2017 - [info] Fri Apr 7 18:00:21 2017 - [info] * Phase 1: Configuration Check Phase.. Fri Apr 7 18:00:21 2017 - [info] Fri Apr 7 18:00:21 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Fri Apr 7 18:00:21 2017 - [info] Reading application default configuration from /etc/masterha/app1.cnf.. Fri Apr 7 18:00:21 2017 - [info] Reading server configuration from /etc/masterha/app1.cnf.. Fri Apr 7 18:00:21 2017 - [info] GTID failover mode = 1 Fri Apr 7 18:00:21 2017 - [info] Current Alive Master: mysql2(192.168.110.132:3306) Fri Apr 7 18:00:21 2017 - [info] Alive Slaves: Fri Apr 7 18:00:21 2017 - [info] mysql1(192.168.110.131:3306) Version=5.6.34-log (oldest major version between slaves) log-bin:enabled Fri Apr 7 18:00:21 2017 - [info] GTID ON Fri Apr 7 18:00:21 2017 - [info] Replicating from 192.168.110.132(192.168.110.132:3306) Fri Apr 7 18:00:21 2017 - [info] ansible(192.168.110.130:3306) Version=5.6.34-log (oldest major version between slaves) log-bin:enabled Fri Apr 7 18:00:21 2017 - [info] GTID ON Fri Apr 7 18:00:21 2017 - [info] Replicating from 192.168.110.132(192.168.110.132:3306) Fri Apr 7 18:00:21 2017 - [info] Not candidate for the new Master (no_master is set) Fri Apr 7 18:00:21 2017 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time.. Fri Apr 7 18:00:21 2017 - [info] ok. Fri Apr 7 18:00:21 2017 - [info] Checking MHA is not monitoring or doing failover.. Fri Apr 7 18:00:21 2017 - [info] Checking replication health on mysql1.. Fri Apr 7 18:00:21 2017 - [info] ok. Fri Apr 7 18:00:21 2017 - [info] Checking replication health on ansible.. Fri Apr 7 18:00:21 2017 - [info] ok. Fri Apr 7 18:00:21 2017 - [info] mysql1 can be new master. Fri Apr 7 18:00:21 2017 - [info] From: mysql2(192.168.110.132:3306) (current master) +--mysql1(192.168.110.131:3306) +--ansible(192.168.110.130:3306) To: mysql1(192.168.110.131:3306) (new master) +--ansible(192.168.110.130:3306) +--mysql2(192.168.110.132:3306) Fri Apr 7 18:00:21 2017 - [info] Checking whether mysql1(192.168.110.131:3306) is ok for the new master.. Fri Apr 7 18:00:21 2017 - [info] ok. Fri Apr 7 18:00:21 2017 - [info] mysql2(192.168.110.132:3306): SHOW SLAVE STATUS returned empty result. To check replication filtering rules, temporarily executing CHANGE MASTER to a dummy host. Fri Apr 7 18:00:21 2017 - [info] mysql2(192.168.110.132:3306): Resetting slave pointing to the dummy host. Fri Apr 7 18:00:21 2017 - [info] ** Phase 1: Configuration Check Phase completed. Fri Apr 7 18:00:21 2017 - [info] Fri Apr 7 18:00:21 2017 - [info] * Phase 2: Rejecting updates Phase.. Fri Apr 7 18:00:21 2017 - [info] Fri Apr 7 18:00:21 2017 - [info] Executing master ip online change script to disable write on the current master: Fri Apr 7 18:00:21 2017 - [info] /usr/bin/master_ip_online_change --command=stop --orig_master_host=mysql2 --orig_master_ip=192.168.110.132 --orig_master_port=3306 --orig_master_user='mha' --orig_master_password='mha' --new_master_host=mysql1 --new_master_ip=192.168.110.131 --new_master_port=3306 --new_master_user='mha' --new_master_password='mha' --orig_master_ssh_user=root --new_master_ssh_user=root --orig_master_is_new_slave Fri Apr 7 18:00:21 2017 637019 Set read_only on the new master.. ok. Fri Apr 7 18:00:21 2017 639365 Waiting all running 2 threads are disconnected.. (max 1500 milliseconds) {'Time' => '281','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '55','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for binlog to be updated','Host' => '192.168.110.130:33058'} {'Time' => '73','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '56','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for binlog to be updated','Host' => '192.168.110.131:50008'} Fri Apr 7 18:00:22 2017 144206 Waiting all running 2 threads are disconnected.. (max 1000 milliseconds) {'Time' => '281','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '55','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for binlog to be updated','Host' => '192.168.110.130:33058'} {'Time' => '73','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '56','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for binlog to be updated','Host' => '192.168.110.131:50008'} Fri Apr 7 18:00:22 2017 649414 Waiting all running 2 threads are disconnected.. (max 500 milliseconds) {'Time' => '282','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '55','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for binlog to be updated','Host' => '192.168.110.130:33058'} {'Time' => '74','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '56','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for binlog to be updated','Host' => '192.168.110.131:50008'} Fri Apr 7 18:00:23 2017 153220 Set read_only=1 on the orig master.. ok. Fri Apr 7 18:00:23 2017 154715 Waiting all running 2 queries are disconnected.. (max 500 milliseconds) {'Time' => '282','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '55','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for binlog to be updated','Host' => '192.168.110.130:33058'} {'Time' => '74','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '56','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for binlog to be updated','Host' => '192.168.110.131:50008'} Fri Apr 7 18:00:23 2017 658225 Killing all application threads.. Fri Apr 7 18:00:23 2017 659547 done. Disabling the VIP on old master: mysql2 Fri Apr 7 18:00:24 2017 - [info] ok. Fri Apr 7 18:00:24 2017 - [info] Locking all tables on the orig master to reject updates from everybody (including root): Fri Apr 7 18:00:24 2017 - [info] Executing FLUSH TABLES WITH READ LOCK.. Fri Apr 7 18:00:24 2017 - [info] ok. Fri Apr 7 18:00:24 2017 - [info] Orig master binlog:pos is mysql-bin.000003:188723758. Fri Apr 7 18:00:24 2017 - [info] Waiting to execute all relay logs on mysql1(192.168.110.131:3306).. Fri Apr 7 18:00:24 2017 - [info] master_pos_wait(mysql-bin.000003:188723758) completed on mysql1(192.168.110.131:3306). Executed 0 events. Fri Apr 7 18:00:24 2017 - [info] done. Fri Apr 7 18:00:24 2017 - [info] Getting new master's binlog name and position.. Fri Apr 7 18:00:24 2017 - [info] mysql-bin.000007:191 Fri Apr 7 18:00:24 2017 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='mysql1 or 192.168.110.131', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='xxx'; Fri Apr 7 18:00:24 2017 - [info] Executing master ip online change script to allow write on the new master: Fri Apr 7 18:00:24 2017 - [info] /usr/bin/master_ip_online_change --command=start --orig_master_host=mysql2 --orig_master_ip=192.168.110.132 --orig_master_port=3306 --orig_master_user='mha' --orig_master_password='mha' --new_master_host=mysql1 --new_master_ip=192.168.110.131 --new_master_port=3306 --new_master_user='mha' --new_master_password='mha' --orig_master_ssh_user=root --new_master_ssh_user=root --orig_master_is_new_slave Fri Apr 7 18:00:24 2017 190044 Set read_only=0 on the new master. Enabling the VIP - 192.168.110.100 on the new master - mysql1 Fri Apr 7 18:00:27 2017 - [info] ok. Fri Apr 7 18:00:27 2017 - [info] Fri Apr 7 18:00:27 2017 - [info] * Switching slaves in parallel.. Fri Apr 7 18:00:27 2017 - [info] Fri Apr 7 18:00:27 2017 - [info] -- Slave switch on host ansible(192.168.110.130:3306) started, pid: 11714 Fri Apr 7 18:00:27 2017 - [info] Fri Apr 7 18:00:28 2017 - [info] Log messages from ansible ... Fri Apr 7 18:00:28 2017 - [info] Fri Apr 7 18:00:27 2017 - [info] Waiting to execute all relay logs on ansible(192.168.110.130:3306).. Fri Apr 7 18:00:27 2017 - [info] master_pos_wait(mysql-bin.000003:188723758) completed on ansible(192.168.110.130:3306). Executed 0 events. Fri Apr 7 18:00:27 2017 - [info] done. Fri Apr 7 18:00:27 2017 - [info] Resetting slave ansible(192.168.110.130:3306) and starting replication from the new master mysql1(192.168.110.131:3306).. Fri Apr 7 18:00:27 2017 - [info] Executed CHANGE MASTER. Fri Apr 7 18:00:28 2017 - [info] Slave started. Fri Apr 7 18:00:28 2017 - [info] End of log messages from ansible ... Fri Apr 7 18:00:28 2017 - [info] Fri Apr 7 18:00:28 2017 - [info] -- Slave switch on host ansible(192.168.110.130:3306) succeeded. Fri Apr 7 18:00:28 2017 - [info] Unlocking all tables on the orig master: Fri Apr 7 18:00:28 2017 - [info] Executing UNLOCK TABLES.. Fri Apr 7 18:00:28 2017 - [info] ok. Fri Apr 7 18:00:28 2017 - [info] Starting orig master as a new slave.. Fri Apr 7 18:00:28 2017 - [info] Resetting slave mysql2(192.168.110.132:3306) and starting replication from the new master mysql1(192.168.110.131:3306).. Fri Apr 7 18:00:28 2017 - [info] Executed CHANGE MASTER. Fri Apr 7 18:00:29 2017 - [info] Slave started. Fri Apr 7 18:00:29 2017 - [info] All new slave servers switched successfully. Fri Apr 7 18:00:29 2017 - [info] Fri Apr 7 18:00:29 2017 - [info] * Phase 5: New master cleanup phase.. Fri Apr 7 18:00:29 2017 - [info] Fri Apr 7 18:00:29 2017 - [info] mysql1: Resetting slave info succeeded. Fri Apr 7 18:00:29 2017 - [info] Switching master to mysql1(192.168.110.131:3306) completed successfully.
MaxScale> list servers Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.110.131 | 3306 | 0 | Slave, Running server2 | 192.168.110.132 | 3306 | 0 | Master, Running server3 | 192.168.110.130 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+-------------------- MaxScale> list servers Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.110.131 | 3306 | 0 | Master, Running server2 | 192.168.110.132 | 3306 | 0 | Slave, Running server3 | 192.168.110.130 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+--------------------
從MHA的online change日志中可以發現,mysql1重新升級成主庫,mysql2降級為從庫。而maxscale也可以準確的識別出后端數據庫的狀態。
13、通過sysbench壓測maxscale性能。
壓測腳本test_sysbench.sh如下:
#!/bin/sh ssh root@ansible '/usr/bin/ansible all -m shell -a "sh /home/linzj/shell/mysql.sh stop; sh /home/linzj/shell/mysql.sh start"' sleep 60 sysbench --test=oltp --mysql-host=192.168.110.130 --mysql-port=4006 --mysql-user=linzj --mysql-password=linzj --mysql-db=sbtest1 --oltp-num-tables=10 --oltp-table-size=500000 --report-interval=100 --max-requests=0 --oltp-test-mode=complex --oltp-nontrx-mode=select --oltp-read-only=on --max-time=300 --num-threads=16 run > /tmp/130_4006_readonly.log ssh root@ansible '/usr/bin/ansible all -m shell -a "sh /home/linzj/shell/mysql.sh stop; sh /home/linzj/shell/mysql.sh start"' sleep 60 sysbench --test=oltp --mysql-host=192.168.110.130 --mysql-port=4008 --mysql-user=linzj --mysql-password=linzj --mysql-db=sbtest1 --oltp-num-tables=10 --oltp-table-size=500000 --report-interval=100 --max-requests=0 --oltp-test-mode=complex --oltp-nontrx-mode=select --oltp-read-only=on --max-time=300 --num-threads=16 run > /tmp/130_4008_readonly.log ssh root@ansible '/usr/bin/ansible all -m shell -a "sh /home/linzj/shell/mysql.sh stop; sh /home/linzj/shell/mysql.sh start"' sleep 60 sysbench --test=oltp --mysql-host=192.168.110.131 --mysql-port=3306 --mysql-user=linzj --mysql-password=linzj --mysql-db=sbtest1 --oltp-num-tables=10 --oltp-table-size=500000 --report-interval=100 --max-requests=0 --oltp-test-mode=complex --oltp-nontrx-mode=select --oltp-read-only=on --max-time=300 --num-threads=16 run > /tmp/131_3306_readonly.log ssh root@ansible '/usr/bin/ansible all -m shell -a "sh /home/linzj/shell/mysql.sh stop; sh /home/linzj/shell/mysql.sh start"' sleep 60 sysbench --test=oltp --mysql-host=192.168.110.130 --mysql-port=4006 --mysql-user=linzj --mysql-password=linzj --mysql-db=sbtest1 --oltp-num-tables=10 --oltp-table-size=500000 --report-interval=100 --max-requests=0 --oltp-test-mode=complex --oltp-nontrx-mode=select --oltp-read-only=off --max-time=300 --num-threads=16 run > /tmp/130_4006_readwrite.log ssh root@ansible '/usr/bin/ansible all -m shell -a "sh /home/linzj/shell/mysql.sh stop; sh /home/linzj/shell/mysql.sh start"' sleep 60 sysbench --test=oltp --mysql-host=192.168.110.131 --mysql-port=3306 --mysql-user=linzj --mysql-password=linzj --mysql-db=sbtest1 --oltp-num-tables=10 --oltp-table-size=500000 --report-interval=100 --max-requests=0 --oltp-test-mode=complex --oltp-nontrx-mode=select --oltp-read-only=off --max-time=300 --num-threads=16 run > /tmp/131_3306_readwrite.log
這里我通過ansible管理3臺MySQL,在做壓測前重啟3臺機器,保證壓測結果不受到緩沖的影響。
ansible(192.168.110.130)部署了maxscale中間件,該壓測腳本部署在mysql1(192.168.110.131)。分只讀和混合讀寫兩種情況壓測,只讀分別測試130機的4006讀寫分離端口、4008只讀端口和131機的3306數據庫端口。
只讀(開啟事務) | |||
機器 | 端口 | transactions | response time of 95% |
maxscale機 | 4006 | 35340 | 582.21ms |
maxscale機 | 4008 | 123507 | 63.91ms |
數據庫本機 | 3306 | 80570 | 354.22ms |
混合讀寫(開啟事務) | |||
機器 | 端口 | transactions | response time of 95% |
maxscale機 | 4006 | 6899 | 2435.64ms |
數據庫本機 | 3306 | 6759 | 2651.76ms |
只讀(關閉事務) | |||
機器 | 端口 | transactions | response time of 95% |
maxscale機 | 4006 | 943008 | 19.34ms |
maxscale機 | 4008 | 3216914 | 3.48ms |
數據庫本機 | 3306 | 2338108 | 0.55ms |
混合讀寫(關閉事務) | |||
機器 | 端口 | transactions | response time of 95% |
maxscale機 | 4006 | 1227877 | 8.41ms |
數據庫本機 | 3306 | 2563155 | 0.51ms |
因為壓測時間只有600s,并且由于虛擬機性能的緣故,只開了16個線程進行并發,測試結果可能偏差較大。但是仍然可以總結出一些結論:
1、關閉事務(即autocommit=1)的情況下,通過maxscale訪問方式性能遠高于直連數據庫方式
2、混合讀寫的情況下,通過maxscale訪問方式有一定的性能損耗。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。