MySQL的GTID复制与传统复制的相互切换
生活随笔
收集整理的這篇文章主要介紹了
MySQL的GTID复制与传统复制的相互切换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
MySQL的GTID復制與傳統復制的相互轉換
-
- 1. GTID復制轉換成傳統復制
- 1.1 環境準備
- 1.2 停止slave
- 1.3 查看當前主從狀態
- 1.4 change master
- 1.5 啟動主從復制
- 1.6 在線關閉主從服務器的GTID
- 2. 傳統復制轉換成GTID復制
- 2.1 環境準備
- 2.2 將enforce_gtid_consistency設置為warn
- 2.3 將enforce_gtid_consistency設置為on
- 2.4 設置GTID模式為off_permissive
- 2.5 設置GTID模式為on_permissive
- 2.6 檢查從庫Ongoing_anonymous_transaction_count狀態值
- 2.7 確保所有的匿名事務已經被完全復制到所有的slave上
- 2.8 設置GTID_MODE為on
- 2.9 change master
- 1. GTID復制轉換成傳統復制
?
1. GTID復制轉換成傳統復制
上篇博文已經介紹了MySQL 5.7版本的GTID復制模式的搭建,下面操作將GTID復制轉換成傳統復制模式。
1.1 環境準備
| master | 192.168.56.100 | 3307 | 1003307 |
| slave | 192.168.56.200 | 3307 | 2003307 |
1.2 停止slave
root@localhost [(none)] 09:38:41>stop slave1.3 查看當前主從狀態
記錄Relay_Master_Log_File和Exec_Master_Log_Pos信息。
root@localhost [(none)] 09:41:22>show slave status\G *************************** 1. row ***************************Slave_IO_State: Master_Host: 192.168.56.100Master_User: replMaster_Port: 3307Connect_Retry: 60Master_Log_File: my3307_binlog.000005Read_Master_Log_Pos: 194Relay_Log_File: mysqldb2-relay-bin.000010Relay_Log_Pos: 375Relay_Master_Log_File: my3307_binlog.000005Slave_IO_Running: NoSlave_SQL_Running: NoReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 194Relay_Log_Space: 585Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1003307Master_UUID: 3a068bf8-cdeb-11e8-8176-080027b0b461Master_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Master_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: 3a068bf8-cdeb-11e8-8176-080027b0b461:1-12Auto_Position: 1Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)1.4 change master
根據上面記錄的Relay_Master_Log_File和Exec_Master_Log_Pos信息,change master:
root@localhost [(none)] 09:41:26>change master to master_log_file='my3307_binlog.000005',master_log_pos=194,master_auto_position=0; Query OK, 0 rows affected (0.01 sec)#參數解釋: master_auto_position=0 表示禁用master_auto_position1.5 啟動主從復制
root@localhost [test] 09:51:28>start slave;- ?
1.6 在線關閉主從服務器的GTID
root@localhost [test] 09:55:01> set global gtid_mode=on_permissive; Query OK, 0 rows affected (0.01 sec)root@localhost [test] 09:55:25> set global gtid_mode=off_permissive; Query OK, 0 rows affected (0.01 sec)root@localhost [test] 09:55:58> set global gtid_mode=off; Query OK, 0 rows affected (0.01 sec)root@localhost [test] 09:56:09> set global enforce_gtid_consistency = 0; Query OK, 0 rows affected (0.00 sec)root@localhost [test] 09:57:34> show variables like '%gtid%'; +----------------------------------+-----------+ | Variable_name | Value | +----------------------------------+-----------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | OFF | | gtid_executed_compression_period | 1000 | | gtid_mode | OFF | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | | | session_track_gtids | OFF | +----------------------------------+-----------+ 8 rows in set (0.01 sec)可以把 gtid_mode=off 與 enforce_gtid_consistency=OFF 寫入配置文件my.cnf中,重啟之后也能生效。2. 傳統復制轉換成GTID復制
此博文介紹了MySQL 5.7版本的傳統復制模式的搭建,下面操作將傳統復制轉換成GTID復制模式。
2.1 環境準備
| master | 192.168.56.100 | 3306 | 1003306 |
| slave | 192.168.56.200 | 3306 | 2003306 |
2.2 將enforce_gtid_consistency設置為warn
在MySQL每個實例上, 將enforce_gtid_consistency設置為warn
root@localhost [(none)] 10:21:01> set @@global.enforce_gtid_consistency=warn;- ?
【注意】:執行完這條語句后,如果出現GTID不兼容的語句用法,會在error log記錄相關信息,那么需要調整該程序避免不兼容的寫法,直到完全沒有不兼容的語句。
2.3 將enforce_gtid_consistency設置為on
在?2.2完成后,如果沒有不兼容語句,可以把MySQL每個實例的ENFORCE_GTID_CONSISTENCY值設置為on。
root@localhost [(none)] 11:05:11> set @@global.enforce_gtid_consistency=on; Query OK, 0 rows affected (0.00 sec)2.4 設置GTID模式為off_permissive
將所有MySQL實例的GTID模式設置為off_permissive
root@localhost [(none)] 11:06:55>SET @@GLOBAL.GTID_MODE = OFF_PERMISSIVE; Query OK, 0 rows affected (0.01 sec)2.5 設置GTID模式為on_permissive
root@localhost [(none)] 11:12:20> SET @@GLOBAL.GTID_MODE = ON_PERMISSIVE; Query OK, 0 rows affected (0.01 sec)2.6 檢查從庫Ongoing_anonymous_transaction_count狀態值
檢查從庫Ongoing_anonymous_transaction_count狀態值是否為0,如果為0,一味著沒有等待的事務,可以進行下一步操作
root@localhost [(none)] 11:13:09>SHOW STATUS LIKE 'ONGOING_ANONYMOUS_TRANSACTION_COUNT'; +-------------------------------------+-------+ | Variable_name | Value | +-------------------------------------+-------+ | Ongoing_anonymous_transaction_count | 0 | +-------------------------------------+-------+ 1 row in set (0.00 sec)2.7 確保所有的匿名事務已經被完全復制到所有的slave上
root@localhost [(none)] 11:29:55>show slave status\G *************************** 1. row *************************** ...Relay_Master_Log_File: my3306_binlog.000090 ...Exec_Master_Log_Pos: 194 ...#在slave上使用函數: root@localhost [(none)] 11:30:01>SELECT MASTER_POS_WAIT('my3306_binlog.000090', 194); +----------------------------------------------+ | MASTER_POS_WAIT('my3306_binlog.000090', 194) | +----------------------------------------------+ | 0 | +----------------------------------------------+ 1 row in set (0.00 sec) 返回結果大于或等于0就說明匿名事務已經全部復制完成2.8 設置GTID_MODE為on
root@localhost [(none)] 11:31:36>SET @@GLOBAL.GTID_MODE = ON; Query OK, 0 rows affected (0.01 sec)2.9 change master
root@localhost [(none)] 11:37:17>STOP slave; Query OK, 0 rows affected (0.00 sec)root@localhost [(none)] 11:37:24>change master to master_auto_position=1; Query OK, 0 rows affected (0.00 sec)root@localhost [(none)] 11:37:52>start slave; Query OK, 0 rows affected (0.00 sec)總結
以上是生活随笔為你收集整理的MySQL的GTID复制与传统复制的相互切换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实战MySQL升级的最佳方法
- 下一篇: 修改大表字段属性报主键冲突