一. 環境
1. 主從複製環境:
- 主庫:192.168.1.101
- 從庫:192.168.1.102
- MySQL:5.7.22
2. 當前主從狀態爲開啓GTID模式:
root@db 06:32: [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.101
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1480
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 1098
Relay_Master_Log_File: mysql-bin.000001
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: 1480
Relay_Log_Space: 1306
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: 7d66c13f-41f0-11e7-aff4-f44d306bf92c
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: 7d66c13f-41f0-11e7-aff4-f44d306bf92c:96-98
Executed_Gtid_Set: 7d66c13f-41f0-11e7-aff4-f44d306bf92c:1-98
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
root@db 06:32: [(none)]>
二. GTID模式改爲傳統模式
1. 測試
操作過程如下:
(1) 先在從庫中執行 stop slave,停止主從複製,然後調整爲傳統複製模式,讓master_auto_position=0
root@db 07:04: [test]> stop slave;
Query OK, 0 rows affected (0.01 sec)
root@db 10:39: [test]> CHANGE MASTER TO master_auto_position=0,Master_Host='192.168.1.101',MASTER_USER='repluser',MASTER_PASSWORD='reppassword',Master_Log_File='mysql-bin.000001',MASTER_LOG_POS=2280;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
root@db 10:42: [test]>
開啓slave複製功能
root@db 10:46: [test]> start slave;
Query OK, 0 rows affected (0.01 sec)
root@db 10:47: [test]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.101
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 194
Relay_Log_File: relay-bin.000006
Relay_Log_Pos: 353
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: 194
Relay_Log_Space: 601
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: 7d66c13f-41f0-11e7-aff4-f44d306bf92c
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: 7d66c13f-41f0-11e7-aff4-f44d306bf92c:1-101
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
root@db 10:47: [test]>
主從服務器上同時調整GTID模式爲on_permissive
主服務器上:
root@db 10:45: [test]> set global gtid_mode=on_permissive;
Query OK, 0 rows affected (0.00 sec)
root@db 10:45: [test]>
從服務器上:
root@db 10:42: [test]> set global gtid_mode=on_permissive;
Query OK, 0 rows affected (0.02 sec)
root@db 10:46: [test]>
需要在主從服務器上關閉GTID功能
主服務器:
root@db 11:18: [test]> set global enforce_gtid_consistency=off;
Query OK, 0 rows affected (0.00 sec)
root@db 11:18: [test]> set global gtid_mode=off;
ERROR 1788 (HY000): The value of @@GLOBAL.GTID_MODE can only be changed one step at a time: OFF <-> OFF_PERMISSIVE <-> ON_PERMISSIVE <-> ON. Also note that this value must be stepped up or down simultaneously on all servers. See the Manual for instructions.
root@db 11:19: [test]>
從服務器:
root@db 11:19: [test]> set global enforce_gtid_consistency=off;
Query OK, 0 rows affected (0.00 sec)
root@db 11:21: [test]> set global gtid_mode=off;
ERROR 1788 (HY000): The value of @@GLOBAL.GTID_MODE can only be changed one step at a time: OFF <-> OFF_PERMISSIVE <-> ON_PERMISSIVE <-> ON. Also note that this value must be stepped up or down simultaneously on all servers. See the Manual for instructions.
root@db 11:21: [test]>
發現主從服務器執行set global gitd_mode=off時報錯,因爲我在前面說過gtid_mode的值有有四種狀態(off、off_permissive、on_permissive,on),雖然四種狀態支持動態修改,但是不能跳躍修改,即只能嚴格依次順序修改,
測試切換是否成功,在主庫中插入數據進行測試:
root@db 11:24: [test]> desc tt;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | MUL | NULL | |
| score | int(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
root@db 11:24: [test]> insert into tt (name,score) values ('kids',99);
Query OK, 1 row affected (0.01 sec)
root@db 11:25: [test]> select * from tt;
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 1 | Tome | 80 |
| 2 | Janne | 90 |
| 3 | Kases | 84 |
| 4 | kids | 99 |
+----+-------+-------+
4 rows in set (0.00 sec)
root@db 11:25: [test]>
從庫查看:
root@db 11:17: [test]> show slave status\G
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 14
Current database: test
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.101
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 194
Relay_Log_File: relay-bin.000006
Relay_Log_Pos: 353
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: 194
Relay_Log_Space: 601
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: 7d66c13f-41f0-11e7-aff4-f44d306bf92c
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: 7d66c13f-41f0-11e7-aff4-f44d306bf92c:1-101
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
root@db 11:17: [test]> set global enforce_gtid_consistency=off;
Query OK, 0 rows affected (0.00 sec)
root@db 11:25: [test]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.101
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 460
Relay_Log_File: relay-bin.000006
Relay_Log_Pos: 619
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: 460
Relay_Log_Space: 867
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: 7d66c13f-41f0-11e7-aff4-f44d306bf92c
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: 7d66c13f-41f0-11e7-aff4-f44d306bf92c:102
Executed_Gtid_Set: 7d66c13f-41f0-11e7-aff4-f44d306bf92c:1-102
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
root@db 11:25: [test]>
發現插入前後Excuted_Gtid_Set發生變化,說明我們這裏切換沒有成功,接下來我們把參數嚴格按照順序修改,再進行測試:
切換之前我們查看下主從gitd_mode參數值:
2. GTID模式改爲傳統模式步驟
主服務器:gtid_mode值
root@db 11:45: [(none)]> show variables like '%gtid_mode%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| gtid_mode | ON |
+---------------+-------+
1 row in set (0.01 sec)
root@db 11:46: [(none)]>
從服務器gitd_mode值:
root@db 11:44: [(none)]> show variables like '%gtid_mode%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| gtid_mode | ON |
+---------------+-------+
1 row in set (0.01 sec)
root@db 11:47: [(none)]>
從服務器執行
root@db 11:47: [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.101
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000012
Read_Master_Log_Pos: 346
Relay_Log_File: relay-bin.000024
Relay_Log_Pos: 505
Relay_Master_Log_File: mysql-bin.000012
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: 346
Relay_Log_Space: 753
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: 7d66c13f-41f0-11e7-aff4-f44d306bf92c
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: 7d66c13f-41f0-11e7-aff4-f44d306bf92c:103
Executed_Gtid_Set: 7d66c13f-41f0-11e7-aff4-f44d306bf92c:1-103
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
root@db 11:48: [(none)]>
先在從庫上關閉slave,停止主從複製,然後修改複製模式爲傳統模式,master_auto_position=0
root@db 11:48: [(none)]> stop slave;
Query OK, 0 rows affected (0.00 sec)
root@db 11:50: [(none)]> CHANGE MASTER TO master_auto_position=0,Master_Host='192.168.1.101',MASTER_USER='repluser',MASTER_PASSWORD='reppassword',Master_Log_File='mysql-bin.000012',MASTER_LOG_POS=346;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
root@db 11:52: [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)
root@db 11:53: [(none)]> set global gtid_mode=on_permissive;
Query OK, 0 rows affected (0.01 sec)
root@db 11:53: [(none)]> set global gtid_mode=off_permissive;
Query OK, 0 rows affected (0.01 sec)
root@db 11:54: [(none)]> set global gtid_mode=off;
Query OK, 0 rows affected (0.01 sec)
root@db 11:55: [(none)]> set global enforce_gtid_consistency=off;
Query OK, 0 rows affected (0.00 sec)
root@db 11:55: [(none)]>
root@db 11:55: [(none)]> show variables like '%gtid_mode%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| gtid_mode | OFF |
+---------------+-------+
row in set (0.00 sec)
root@db 11:58: [(none)]>
同樣主庫做如下操作:
root@db 11:45: [(none)]> show variables like '%gtid_mode%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| gtid_mode | ON |
+---------------+-------+
1 row in set (0.01 sec)
root@db 11:46: [(none)]> set global gtid_mode=on_permissive;
Query OK, 0 rows affected (0.02 sec)
root@db 11:53: [(none)]> set global gtid_mode=off_permissive;
Query OK, 0 rows affected (0.01 sec)
root@db 11:54: [(none)]> set global gtid_mode=off;
Query OK, 0 rows affected (0.02 sec)
root@db 11:55: [(none)]> set global enforce_gtid_consistency=off;
Query OK, 0 rows affected (0.00 sec)
root@db 11:55: [(none)]> show variables like '%gtid_mode%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| gtid_mode | OFF |
+---------------+-------+
1 row in set (0.00 sec)
root@db 11:57: [(none)]>
兩邊gtid複製模式方式現已關閉,同時開始傳統模式,開始驗證傳統複製模式是否生效:
3. 驗證
驗證前查記錄下當前Excuted_Gtid_Set值,方便後面做對比。
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 7d66c13f-41f0-11e7-aff4-f44d306bf92c:1-103
Auto_Position: 0
Replicate_Rewrite_DB:
在主庫插入數據:
root@db 12:11: [test]> select * from tt;
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 1 | Tome | 80 |
| 2 | Janne | 90 |
| 3 | Kases | 84 |
| 4 | kids | 99 |
+----+-------+-------+
4 rows in set (0.00 sec)
root@db 12:12: [test]> insert into tt (name,score) values('MySQL',82);
Query OK, 1 row affected (0.01 sec)
root@db 12:13: [test]> select * from tt;
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 1 | Tome | 80 |
| 2 | Janne | 90 |
| 3 | Kases | 84 |
| 4 | kids | 99 |
| 5 | MySQL | 82 |
+----+-------+-------+
5 rows in set (0.00 sec)
root@db 12:13: [test]>
從庫查看插入數據:
root@db 12:06: [(none)]> use test
Database changed
root@db 12:14: [test]> select * from tt;
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 1 | Tome | 80 |
| 2 | Janne | 90 |
| 3 | Kases | 84 |
| 4 | kids | 99 |
| 5 | MySQL | 82 |
+----+-------+-------+
5 rows in set (0.00 sec)
root@db 12:14: [test]>
發現數據已經過來了 ,再查看Excuted_Gtid_Set的值。
root@db 12:14: [test]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.101
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000015
Read_Master_Log_Pos: 461
Relay_Log_File: relay-bin.000008
Relay_Log_Pos: 620
Relay_Master_Log_File: mysql-bin.000015
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
--------------------省略--------------
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 7d66c13f-41f0-11e7-aff4-f44d306bf92c:1-103
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
通過對比發現GTID的值沒有增加,證明切換成功。
三. 傳統模式切換到GTID模式
接下來做相反操作,通過傳統模式切換到GTID模式
在主從庫上修修改enforce_gtid_consistency=warn,確保在error.log中不出現警告信息,如果有需要先修復。
主庫:
root@db 12:13: [test]> set global enforce_gtid_consistency=warn;
Query OK, 0 rows affected (0.00 sec)
root@db 12:19: [test]> show variables like '%enforce_gtid_consistency%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| enforce_gtid_consistency | WARN |
+--------------------------+-------+
1 row in set (0.01 sec)
root@db 12:20: [test]>
從庫:
root@db 12:18: [test]> set global enforce_gtid_consistency=warn;
Query OK, 0 rows affected (0.00 sec)
root@db 12:19: [test]> show variables like '%enforce_gtid_consistency%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| enforce_gtid_consistency | WARN |
+--------------------------+-------+
1 row in set (0.01 sec)
root@db 12:20: [test]>
在主從庫上調整enforce_gtid_consistency=on,保證GTID的一致性
主庫:
root@db 12:20: [test]> set global enforce_gtid_consistency=on;
Query OK, 0 rows affected (0.00 sec)
root@db 12:21: [test]> show variables like '%enforce_gtid_consistency%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| enforce_gtid_consistency | ON |
+--------------------------+-------+
1 row in set (0.01 sec)
root@db 12:21: [test]>
從庫:
root@db 12:20: [test]> set global enforce_gtid_consistency=on;
Query OK, 0 rows affected (0.00 sec)
root@db 12:22: [test]> show variables like '%enforce_gtid_consistency%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| enforce_gtid_consistency | ON |
+--------------------------+-------+
1 row in set (0.01 sec)
root@db 12:22: [test]>
然後在主從庫安裝前面的順序再一次修改回去,直至gtid_mode=on爲止
主庫操作:
root@db 12:21: [test]> set global gtid_mode=off_permissive;
Query OK, 0 rows affected (0.02 sec)
root@db 12:24: [test]> set global gtid_mode=on_permissive;
Query OK, 0 rows affected (0.01 sec)
root@db 12:24: [test]> set global gtid_mode=on;
Query OK, 0 rows affected (0.01 sec)
root@db 12:24: [test]> show variables like '%gtid_mode%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| gtid_mode | ON |
+---------------+-------+
1 row in set (0.00 sec)
root@db 12:24: [test]>
從庫操作:
root@db 12:22: [test]> set global gtid_mode=off_permissive;
Query OK, 0 rows affected (0.01 sec)
root@db 12:25: [test]> set global gtid_mode=on_permissive;
Query OK, 0 rows affected (0.01 sec)
root@db 12:25: [test]> set global gtid_mode=on;
Query OK, 0 rows affected (0.01 sec)
root@db 12:25: [test]> show variables like '%gtid_mode%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| gtid_mode | ON |
+---------------+-------+
1 row in set (0.01 sec)
root@db 12:26: [test]>
提示:上述gtid_mode參數值修改必須嚴格安裝順序依次修改,否則會報錯。
查看從庫的參數Ongoing_anonymous_transaction_count參數值是否爲0,如果爲0,意味着沒有等待的事務,可以直接進行後面的操作
查看從庫的參數Ongoing_anonymous_transaction_count參數值是否爲0,如果爲0,意味着沒有等待的事務,可以直接進行後面的操作
root@db 12:29: [test]> show global status like '%ongoing_anonymous%';
+-------------------------------------+-------+
| Variable_name | Value |
+-------------------------------------+-------+
| Ongoing_anonymous_transaction_count | 0 |
+-------------------------------------+-------+
1 row in set (0.01 sec)
root@db 12:29: [test]>
說明沒有等待提交的事務,可以進入後面的操作。
再次覈對GTID相關參數狀態:
主服務器:
root@db 12:24: [test]> show variables like '%gtid%';
+----------------------------------+-------------------------------------------+
| Variable_name | Value |
+----------------------------------+-------------------------------------------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed_compression_period | 1000 |
| gtid_mode | ON |
| gtid_next | AUTOMATIC |
| gtid_owned | |
| gtid_purged | 7d66c13f-41f0-11e7-aff4-f44d306bf92c:1-92 |
| session_track_gtids | OFF |
+----------------------------------+-------------------------------------------+
8 rows in set (0.06 sec)
root@db 12:30: [test]>
從服務器:
root@db 12:29: [test]> show variables like '%gtid%';
+----------------------------------+-------------------------------------------+
| Variable_name | Value |
+----------------------------------+-------------------------------------------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed_compression_period | 1000 |
| gtid_mode | ON |
| gtid_next | AUTOMATIC |
| gtid_owned | |
| gtid_purged | 7d66c13f-41f0-11e7-aff4-f44d306bf92c:1-95 |
| session_track_gtids | OFF |
+----------------------------------+-------------------------------------------+
8 rows in set (0.01 sec)
root@db 12:31: [test]>
我們把傳統的複製模式改爲GTID模式要把傳統的複製停掉,然後執行stop slave,然後執行change master to master_auto_position=1
停止slave,查看當前主從狀態:
root@db 12:35: [test]> stop slave;
Query OK, 0 rows affected (0.01 sec)
root@db 12:35: [test]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.101
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000018
Read_Master_Log_Pos: 194
Relay_Log_File: relay-bin.000014
Relay_Log_Pos: 353
Relay_Master_Log_File: mysql-bin.000018
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: 194
Relay_Log_Space: 601
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: 7d66c13f-41f0-11e7-aff4-f44d306bf92c
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:
Executed_Gtid_Set: 7d66c13f-41f0-11e7-aff4-f44d306bf92c:1-103
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
root@db 12:35: [test]>
執行change master to master_auto_position=1.開啓複製狀態,start slave
root@db 12:36: [test]> change master to master_auto_position=1;
Query OK, 0 rows affected (0.02 sec)
root@db 12:36: [test]> start slave;
Query OK, 0 rows affected (0.00 sec)
root@db 12:37: [test]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.101
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000018
Read_Master_Log_Pos: 194
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 353
Relay_Master_Log_File: mysql-bin.000018
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: 194
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: 7d66c13f-41f0-11e7-aff4-f44d306bf92c
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: 7d66c13f-41f0-11e7-aff4-f44d306bf92c:1-103
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
root@db 12:37: [test]>
驗證
驗證切換是否成功,主庫插入數據:
root@db 12:30: [test]> insert into tt (name,score) values('gtid',82);
Query OK, 1 row affected (0.00 sec)
root@db 12:38: [test]> insert into tt (name,score) values('gtid_on',85);
Query OK, 1 row affected (0.00 sec)
root@db 12:38: [test]> select * from tt;
+----+---------+-------+
| id | name | score |
+----+---------+-------+
| 1 | Tome | 80 |
| 2 | Janne | 90 |
| 3 | Kases | 84 |
| 4 | kids | 99 |
| 5 | MySQL | 82 |
| 6 | gtid | 82 |
| 7 | gtid_on | 85 |
+----+---------+-------+
7 rows in set (0.01 sec)
root@db 12:38: [test]>
從庫查看數據和Excuted_Gtid_Set對應值是否發生變化:
root@db 12:37: [test]> select * from tt;
+----+---------+-------+
| id | name | score |
+----+---------+-------+
| 1 | Tome | 80 |
| 2 | Janne | 90 |
| 3 | Kases | 84 |
| 4 | kids | 99 |
| 5 | MySQL | 82 |
| 6 | gtid | 82 |
| 7 | gtid_on | 85 |
+----+---------+-------+
7 rows in set (0.00 sec)
root@db 12:40: [test]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.101
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000018
Read_Master_Log_Pos: 729
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 888
Relay_Master_Log_File: mysql-bin.000018
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
--------省略部分---------------------
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 7d66c13f-41f0-11e7-aff4-f44d306bf92c:104-105
Executed_Gtid_Set: 7d66c13f-41f0-11e7-aff4-f44d306bf92c:1-105
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
root@db 12:40: [test]>
通過上述查詢,發現數據已經複製過來,說明數據同步成功,而Excuted_Gtid_Set的值由“ 7d66c13f-41f0-11e7-aff4-f44d306bf92c:1-103” 變換爲:“7d66c13f-41f0-11e7-aff4-f44d306bf92c:1-105”,說明切換成功,因爲GTID的值增加了 ,證明開啓了GTID的複製方式。
至此MySQL的傳統複製方式和GTID方式互相切換演示完成,需要注意的是gtid_mode的值雖然支持動態修改,但是在修改時不能跳躍式的修改,必須得嚴格按照順序修改。