MySQL5.7配置基於GTID的複製及GTID回退到傳統模式的方法
- 2019 年 10 月 5 日
- 筆記
MySQL5.7下配置GTID複製的方法:
環境:
CentOS6.8X86_64
MySQL Community 5.7.17
node1:192.168.2.171 主庫
node2:192.168.2.172 從庫
修改主庫和從庫的配置文件,加入紅色部分的配置項:
主庫:
[mysqld]
log-bin=mysql-bin
binlog_format= ROW
gtid-mode = ON
enforce_gtid_consistency = ON
master-info-repository=TABLE
relay-log-info-repository=TABLE
從庫:
[mysqld]
log-bin=mysql-bin
binlog_format= ROW
log_slave_updates = ON
gtid-mode = ON
enforce_gtid_consistency = ON
master-info-repository=TABLE
relay-log-info-repository=TABLE
重啟主庫和從庫,使上面的配置生效。
然後在node1上導出全量的數據並傳到node2:
mysqldump -uroot -proot -q–single-transaction -A > /root/all.sql
scp /root/all.sql [email protected]:/root
在node2上導入: mysql -uroot -proot < /root/all.sql
然後,還要在node1的主庫創建個複製許可權的帳號:
> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repluser'@'192.168.2.%' IDENTIFIED BY 'Abcd@1234';
在slave上配置change master to指向(如下6行程式碼):
> change master to
master_HOST='192.168.2.171',
master_PORT=3306,
master_USER='repluser',
master_PASSWORD='Abcd@1234',
master_AUTO_POSITION=1;
> start slave;
> show slave statusG 結果如下:
***************************1. row ***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 192.168.2.171
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql.000006
Read_Master_Log_Pos: 786
Relay_Log_File:node2-relay-bin.000002
Relay_Log_Pos: 991
Relay_Master_Log_File: mysql.000006
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: 786
Relay_Log_Space: 1198
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:101
Master_UUID:e2deedc8-ed36-11e6-b826-000c29f17302
Master_Info_File:mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has readall 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:e2deedc8-ed36-11e6-b826-000c29f17302:1-3
Executed_Gtid_Set:e2deedc8-ed36-11e6-b826-000c29f17302:1-3
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
這樣,我們的基於GTID的複製就配置完成了。
GTID複製轉成傳統模式的方法:
如果之前啟用過了GTID,那麼就不能不能再使用傳統的change master to的方式了,會報錯,如下:
ERROR 1776 (HY000): Parameters MASTER_LOG_FILE, MASTER_LOG_POS,RELAY_LOG_FILE and RELAY_LOG_POS cannot be set when MASTER_AUTO_POSITION isactive.
要轉換成傳統模式,需要在my.cnf裡面注釋掉下面2行:
gtid-mode=ON
enforce_gtid_consistency = ON
然後重啟MySQL。
解決方法:
> stop slave;
> show slave statusG
記錄下當前複製到哪裡了,如下圖紅色部分:
> change master to MASTER_AUTO_POSITION=0; # 設置為0關閉這個選項,這個選項是GTID複製才用到的。
> CHANGE MASTER TO
MASTER_HOST = '192.168.2.171',
MASTER_USER='repluser',
MASTER_PASSWORD='Abcd@1234',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql.000006',
MASTER_LOG_POS=786,
MASTER_CONNECT_RETRY=10;
> start slave;
> show slave statusG 驗證下是否IO/SQL都是YES狀態。如下圖,我們已經將GTID複製的轉成傳統模式了。