本文介绍如何配置GTIDs主从复制与故障自动切换
一、安装mysql-utilities-1.6.5
从MySQL官网下载安装即可
二、配置GTIDs主从
1. 配置文件
服务器:mysql5722_01
[root@mysql5722_01 ~]# cat /etc/my.cnf [mysqld] sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES basedir = /usr/local/mysql datadir = /data socket=/tmp/mysql.sock innodb_file_per_table = ON server-id = 572201 log-bin=mysql5722_01-bin port=3306 log-error = /data/mysql5722_01_error.log ## GTID gtid-mode=on enforce-gtid-consistency=true binlog-format=ROW log-slave-updates=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-workers=0 binlog-checksum=CRC32 master-verify-checksum=1 slave-sql-verify-checksum=1 binlog-rows-query-log_events=1 skip_slave_start=1 report-host=192.168.10.193 report-port=3306
服务器:mysql5722_02
[root@mysql5722_02 ~]# cat /etc/my.cnf [mysqld] sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES basedir = /usr/local/mysql datadir = /data socket=/tmp/mysql.sock innodb_file_per_table = ON server-id = 572202 log-bin=mysql5722_02-bin port=3306 log-error = /data/mysql5722_02_error.log ## GTID gtid-mode=on enforce-gtid-consistency=true binlog-format=ROW log-slave-updates=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-workers=0 binlog-checksum=CRC32 master-verify-checksum=1 slave-sql-verify-checksum=1 binlog-rows-query-log_events=1 skip_slave_start=1 report-host=192.168.10.194 report-port=3306
2. 配置GTIDs主从
这里使用mysqlreplicate进行快速配置
[root@mysql5722_01 ~]# mysqlreplicate --master=root:[email protected]:3306 --slave=root:[email protected]:3306 --rpl-user=root:123456 WARNING: Using a password on the command line interface can be insecure. # master on 192.168.10.194: ... connected. # slave on 192.168.10.193: ... connected. # Checking for binary logging on master... # Setting up replication... # ...done.
3. 检查主从
服务器:mysql5722_01
mysql> show master status; +-------------------------+----------+--------------+------------------+------------------------------------------------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------------+----------+--------------+------------------+------------------------------------------------------------------------------------+ | mysql5722_01-bin.000003 | 1418 | | | bec93e1d-9c95-11e8-bb3b-000c29e63227:1-5, fb68c455-91aa-11e8-995c-000c2981f834:1-5 | +-------------------------+----------+--------------+------------------+------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.194 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql5722_02-bin.000005 Read_Master_Log_Pos: 2602 Relay_Log_File: mysql5722_01-relay-bin.000002 Relay_Log_Pos: 1659 Relay_Master_Log_File: mysql5722_02-bin.000005 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 2602 Relay_Log_Space: 1913 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 572202 Master_UUID: bec93e1d-9c95-11e8-bb3b-000c29e63227 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: bec93e1d-9c95-11e8-bb3b-000c29e63227:1-5 Executed_Gtid_Set: bec93e1d-9c95-11e8-bb3b-000c29e63227:1-5, fb68c455-91aa-11e8-995c-000c2981f834:1-5 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
服务器:mysql5722_02
mysql> show master status; +-------------------------+----------+--------------+------------------+------------------------------------------------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------------+----------+--------------+------------------+------------------------------------------------------------------------------------+ | mysql5722_02-bin.000005 | 2602 | | | bec93e1d-9c95-11e8-bb3b-000c29e63227:1-5, fb68c455-91aa-11e8-995c-000c2981f834:1-5 | +-------------------------+----------+--------------+------------------+------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> show slave hosts; +-----------+----------------+------+-----------+--------------------------------------+ | Server_id | Host | Port | Master_id | Slave_UUID | +-----------+----------------+------+-----------+--------------------------------------+ | 572201 | 192.168.10.193 | 3306 | 572202 | fb68c455-91aa-11e8-995c-000c2981f834 | +-----------+----------------+------+-----------+--------------------------------------+ 1 row in set (0.00 sec)
4. 开启主从故障自动切换
[root@mysql5722_02 tmp]# mysqlfailover --master=root:[email protected]:3306 --discover-slaves-login=root:123456 --force WARNING: Using a password on the command line interface can be insecure. # Discovering slaves for master at 192.168.10.194:3306 # Discovering slave at 192.168.10.193:3306 # Found slave: 192.168.10.193:3306 # Checking privileges. MySQL Replication Failover Utility Failover Mode = auto Next Interval = Thu Oct 11 08:22:20 2018 Master Information ------------------ Binary Log File Position Binlog_Do_DB Binlog_Ignore_DB mysql5722_02-bin.000 2602 GTID Executed Set bec93e1d-9c95-11e8-bb3b-000c29e63227:1-5 [...] Replication Health Status +-----------------+-------+---------+--------+------------+---------+ | host | port | role | state | gtid_mode | health | +-----------------+-------+---------+--------+------------+---------+ | 192.168.10.194 | 3306 | MASTER | UP | ON | OK | | 192.168.10.193 | 3306 | SLAVE | UP | ON | OK | +-----------------+-------+---------+--------+------------+---------+
5. 模拟故障切换
关闭主:mysql5722_02
[root@mysql5722_02 ~]# service mysql.server stop Shutting down MySQL............ SUCCESS!
监控输出
Failed to reconnect to the master after 3 attemps. Failover starting in 'auto' mode... # Candidate slave 192.168.10.193:3306 will become the new master. # Checking slaves status (before failover). # Preparing candidate for failover. # Creating replication user if it does not exist. # Stopping slaves. # Performing STOP on all slaves. # Switching slaves to new master. # Disconnecting new master as slave. # Starting slaves. # Performing START on all slaves. # Checking slaves for errors. # Failover complete. # Discovering slaves for master at 192.168.10.193:3306 Failover console will restart in 5 seconds. MySQL Replication Failover Utility Failover Mode = auto Next Interval = Thu Oct 11 08:28:06 2018 Master Information ------------------ Binary Log File Position Binlog_Do_DB Binlog_Ignore_DB mysql5722_01-bin.000 1418 GTID Executed Set bec93e1d-9c95-11e8-bb3b-000c29e63227:1-5 [...] Replication Health Status +-----------------+-------+---------+--------+------------+---------+ | host | port | role | state | gtid_mode | health | +-----------------+-------+---------+--------+------------+---------+ | 192.168.10.193 | 3306 | MASTER | UP | ON | OK | +-----------------+-------+---------+--------+------------+---------+
6. 重新恢复主从
[root@mysql5722_02 ~]# service mysql.server start Starting MySQL.. SUCCESS! [root@mysql5722_02 ~]# mysqlreplicate --master=root:[email protected]:3306 --slave=root:[email protected]:3306 --rpl-user=root:123456 WARNING: Using a password on the command line interface can be insecure. # master on 192.168.10.193: ... connected. # slave on 192.168.10.194: ... connected. # Checking for binary logging on master... # Setting up replication... # ...done.
监控输出
# Discovering slaves for master at 192.168.10.193:3306 MySQL Replication Failover Utility Failover Mode = auto Next Interval = Thu Oct 11 08:32:03 2018 Master Information ------------------ Binary Log File Position Binlog_Do_DB Binlog_Ignore_DB mysql5722_01-bin.000 1418 GTID Executed Set bec93e1d-9c95-11e8-bb3b-000c29e63227:1-5 [...] Replication Health Status +-----------------+-------+---------+--------+------------+---------+ | host | port | role | state | gtid_mode | health | +-----------------+-------+---------+--------+------------+---------+ | 192.168.10.193 | 3306 | MASTER | UP | ON | OK | | 192.168.10.194 | 3306 | SLAVE | UP | ON | OK | +-----------------+-------+---------+--------+------------+---------+
至此,CentOS 7.2 + MySQL 5.7 GTIDs 主从复制与故障自动切换搭建完毕。