mysql5.7 gtid和传统复制在线切换,5.7.6 之后 不用重启可以直接在线切换
目前的环境:
master 192.168.26.233 3306 GTID
slave 192.168.26.108 3306 GTID
gtid_mode 几种状态的说明:
off : 不产生GTID,基于binlog+position ,slave也不能接受GTID的日志
off_permissive : 不产生GTID,但作为slave可以识别到GTID事物,也可以识别非GTID事物
on_permissive : 产生GTID事物,Slave可以处理GTID事物和非GTID事物
on : 产生GTID事物,slave只接受GTID事物
1.停止slave
root@localhost [zw3306]>stop slave;
Query OK, 0 rows affected (0.00 sec)
2. 查看slave状态,记录master_log_file和position信息。
root@localhost [zw3306]>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.26.233
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 4563
Relay_Log_File: relay-bin.000007
Relay_Log_Pos: 1996
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: No
Slave_SQL_Running: No
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: 4563
Relay_Log_Space: 5277
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: NULL
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: 3306100
Master_UUID: 7e354a2c-6f5f-11e6-997d-005056a36f08
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 7e354a2c-6f5f-11e6-997d-005056a36f08:1-33
Executed_Gtid_Set: 7e354a2c-6f5f-11e6-997d-005056a36f08:1-33,
ba0d5587-74d6-11e6-ab5c-005056a3f46e:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
3.根据上面记录的信息 change master
root@localhost [zw3306]>change master to master_log_file='mysql-bin.000003',master_log_pos=4563,master_auto_position=0;
Query OK, 0 rows affected (0.18 sec)
参数解释:
master_log_file = Relay_Master_Log_File: mysql-bin.000003
master_log_pos = Exec_Master_Log_Pos: 4563
master_auto_position=0 禁用: master_auto_position
root@localhost [zw3306]>start slave;
Query OK, 0 rows affected (0.00 sec)
root@localhost [zw3306]>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.26.233
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 4563
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000003
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: 4563
Relay_Log_Space: 561
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: 3306100
Master_UUID: 7e354a2c-6f5f-11e6-997d-005056a36f08
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:
Executed_Gtid_Set: 7e354a2c-6f5f-11e6-997d-005056a36f08:1-33,
ba0d5587-74d6-11e6-ab5c-005056a3f46e:1-2
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
主库删除3条数据测试
mysql> delete from t1 where id =7;
Query OK, 1 row affected (0.00 sec)
mysql> delete from t1 where id =8;
Query OK, 2 rows affected (0.00 sec)
mysql> delete from t1 where id =9;
Query OK, 1 row affected (0.00 sec)
slave查看状态
root@localhost [zw3306]>show slave status\G;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 19
Current database: zw3306
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.26.233
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 5339
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 1096
Relay_Master_Log_File: mysql-bin.000003
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: 5339
Relay_Log_Space: 1337
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: 3306100
Master_UUID: 7e354a2c-6f5f-11e6-997d-005056a36f08
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: 7e354a2c-6f5f-11e6-997d-005056a36f08:34-36
Executed_Gtid_Set: 7e354a2c-6f5f-11e6-997d-005056a36f08:1-36,
ba0d5587-74d6-11e6-ab5c-005056a3f46e:1-2
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
可以看到有事物同步过来。7,8,9这三条数据也同步被删了。
root@localhost [zw3306]>select * from t1;
+------+
| id |
+------+
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 19 |
| 20 |
+------+
4. 更改gtid模式(master和slave都执行)
4.1 master 和 slave都要修改
mysql> set @@global.gtid_mode=on_permissive;
Query OK, 0 rows affected (0.10 sec)
mysql> show variables like 'gtid_mode';
+---------------+---------------+
| Variable_name | Value |
+---------------+---------------+
| gtid_mode | ON_PERMISSIVE |
+---------------+---------------+
1 row in set (0.01 sec)
on_permissive 产生GTID事物,Slave可以处理GTID事物和非GTID事物
可以在主库上insert数据,show master查看信息。(步骤略)
4.2 master和slave都要修改
mysql> set @@global.gtid_mode=off_permissive;
Query OK, 0 rows affected (0.15 sec)
mysql> show variables like 'gtid_mode';
+---------------+----------------+
| Variable_name | Value |
+---------------+----------------+
| gtid_mode | OFF_PERMISSIVE |
+---------------+----------------+
1 row in set (0.00 sec)
off_permissive 不产生GTID,但作为slave可以识别到GTID事物,也可以识别非GTID事物
可以测试一下
insert into 一条数据,
show master status;(具体步骤略)
4.3 master和slave端都要修改
root@localhost [zw3306]>set @@global.gtid_mode=off;
Query OK, 0 rows affected (0.08 sec)
root@localhost [zw3306]>show variables like 'gtid_mode';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| gtid_mode | OFF |
+---------------+-------+
1 row in set (0.01 sec)
OFF : 不产生GTID,基于binlog+position ,slave也不能接受GTID的日志
5.查看更改是否有效
5.1先记录slave的相关信息
root@localhost [zw3306]>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.26.233
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 194
Relay_Log_File: relay-bin.000008
Relay_Log_Pos: 407
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
......
Exec_Master_Log_Pos: 194
Relay_Log_Space: 695
......
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 7e354a2c-6f5f-11e6-997d-005056a36f08:34-36
Executed_Gtid_Set: 7e354a2c-6f5f-11e6-997d-005056a36f08:1-36,
ba0d5587-74d6-11e6-ab5c-005056a3f46e:1-2
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
5.2 在master做一些操作:
mysql> delete from t1 where id=10;
Query OK, 1 row affected (0.00 sec)
mysql> delete from t1 where id=11;
Query OK, 1 row affected (0.00 sec)
mysql> delete from t1 where id=12;
Query OK, 1 row affected (0.00 sec)
mysql> delete from t1 where id=13;
Query OK, 1 row affected (0.00 sec)
查看slave同步状态:
root@localhost [zw3306]>select * from t1;
+------+
| id |
+------+
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 19 |
| 20 |
+------+
8 rows in set (0.00 sec)
可以看到相关数据没啦!
root@localhost [zw3306]>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.26.233
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 1222
Relay_Log_File: relay-bin.000008
Relay_Log_Pos: 1435
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
......
Exec_Master_Log_Pos: 1222
Relay_Log_Space: 1722
......
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 7e354a2c-6f5f-11e6-997d-005056a36f08:34-36
Executed_Gtid_Set: 7e354a2c-6f5f-11e6-997d-005056a36f08:1-36,
ba0d5587-74d6-11e6-ab5c-005056a3f46e:1-2
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
可以看到pos的信息变化了,而gtid的信息没有变。说明现在已经转换成了传统复制。
6.最后一步:修改配置文件my.conf
[mysqld]
gtid_mode=off;
enforce_gtid_consistency=off;