mysql8基於gtid導出導入搭建主從
開啓GTID的狀況下導出導入庫的注意事項
在開啓了 GTID 功能的 MySQL 數據庫中, 不管是否使用了 GTID 的方式作了主從同步, 導出導入時都須要特別注意數據庫中的 GTID 信息.mysql
# 導出
# 在主庫上全備,--set-gtid-purged決定是否帶gtid,這裏建議明確指出是ON仍是OFFsql
[root@db145 bak]# mysqldump --login-path=instance_3306 --set-gtid-purged=ON --all-databases --single-transaction --master-data=2 --triggers --routines --events >/home/bak/all_database.dump.sql
# 在主庫上全備,不帶gtid數據庫
[root@db145 bak]# mysqldump --login-path=instance_3306 --set-gtid-purged=OFF --all-databases --single-transaction --master-data=2 --triggers --routines --events >/home/bak/all_database.dump.sql
# 導入
# 導入的時候也分兩種, 一種是導入帶有 GTID 的信息的庫, 一種是導入不帶有 GTID 信息的庫
不帶有 GTID 信息的 dump 文件, 無論目標數據庫實例是否開啓了 GTID 功能, 且無論數據庫實例是否已有其餘 GTID 信息, 均可以順利導入vim
帶有 GTID 信息的 dump 文件, 要求目標數據庫實例必須開啓 GTID 功能, 且當前數據庫中無其餘 GTID 信息.
若是目標數據庫中已經記錄了一條或一條以上的 GTID 信息, 那麼在導入數據庫時會報出相似以下的錯誤session
[root@db143 bak]# mysql --login-path=instance_3306_root <all_database.dump.sql ERROR 3546 (HY000) at line 26: @@GLOBAL.GTID_PURGED cannot be changed: the added gtid set must not overlap with @@GLOBAL.GTID_EXECUTED
這個時候你有兩個選擇:app
從新 dump 數據庫, 使用--set-gtid-purged=OFF的參數禁止導出 GTID 信息,再 導入到目標數據庫
在目標數據庫中執行ide
MySQL>reset slave all; reset master;
清空全部 GTID 信息以後就能夠導入了this
[root@db143 bak]# mysql --login-path=instance_3306_root <all_database.dump.sql You have new mail in /var/spool/mail/root [root@db143 bak]#
# 搭建主從spa
-- drop user 'repuser'@'%';
create user 'repuser'@'%' identified WITH 'mysql_native_password' by 'repuserpwd';
grant replication slave on *.* to 'repuser'@'%';3d
CHANGE MASTER TO MASTER_HOST='192.168.142.145', MASTER_USER='repuser', MASTER_PASSWORD='repuserpwd', MASTER_PORT=3306, MASTER_AUTO_POSITION = 1; start slave ; show slave status\G;
# gtid報錯處理
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.142.145 Master_User: repuser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000013 Read_Master_Log_Pos: 1837 Relay_Log_File: db143-relay-bin.000002 Relay_Log_Pos: 1104 Relay_Master_Log_File: mysql-bin.000013 Slave_IO_Running: Yes 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: 1008 Last_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 1 failed executing transaction 'b2f0bfdc-87db-11e9-b83d-000c298bc91b:12' at master log mysql-bin.000013, end_log_pos 1837. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any. Skip_Counter: 0 Exec_Master_Log_Pos: 1664 Relay_Log_Space: 1477 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: 1008 Last_SQL_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 1 failed executing transaction 'b2f0bfdc-87db-11e9-b83d-000c298bc91b:12' at master log mysql-bin.000013, end_log_pos 1837. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any. Replicate_Ignore_Server_Ids: Master_Server_Id: 1453306 Master_UUID: b2f0bfdc-87db-11e9-b83d-000c298bc91b 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: 191230 14:31:12 Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: b2f0bfdc-87db-11e9-b83d-000c298bc91b:9-12 Executed_Gtid_Set: 58f6e65e-9309-11e9-9d88-525400184a0a:1-81, 81887515-6ded-11e9-a307-000c29508dcb:1-10, b2f0bfdc-87db-11e9-b83d-000c298bc91b:1-11 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0 Network_Namespace: 1 row in set (0.00 sec) ERROR: No query specified
Retrieved_Gtid_Set: b2f0bfdc-87db-11e9-b83d-000c298bc91b:9-12 # 這一行很重要 Executed_Gtid_Set: 58f6e65e-9309-11e9-9d88-525400184a0a:1-81, # 這一行很重要 81887515-6ded-11e9-a307-000c29508dcb:1-10, b2f0bfdc-87db-11e9-b83d-000c298bc91b:1-11 Auto_Position: 1
# 處理辦法:
# 準備工做:在主庫中解析對應的binlog日誌,查找pos點位;(可選)
mysqlbinlog --base64-output=decode-rows -vvv mysql-bin.000013 >13_binlog
vim 13_binlog
# at 1664
#191230 14:31:12 server id 1453306 end_log_pos 1737 GTID last_committed=6 sequence_number=7 rbr_only=no original_committed_timestamp=1577687472295487 immediate_commit_timestamp=1577687472295487 transaction_length=173
# original_commit_timestamp=1577687472295487 (2019-12-30 14:31:12.295487 CST)
# immediate_commit_timestamp=1577687472295487 (2019-12-30 14:31:12.295487 CST)
/*!80001 SET @@session.original_commit_timestamp=1577687472295487*//*!*/;
/*!80014 SET @@session.original_server_version=80018*//*!*/;
/*!80014 SET @@session.immediate_server_version=80018*//*!*/;
SET @@SESSION.GTID_NEXT= 'b2f0bfdc-87db-11e9-b83d-000c298bc91b:12'/*!*/; # 這一行很重要(實際上和推想的保持一致)
# at 1737
#191230 14:31:12 server id 1453306 end_log_pos 1837 Query thread_id=30 exec_time=0 error_code=0 Xid = 47
# 具體處理:
STOP SLAVE;
SET SESSION GTID_NEXT='b2f0bfdc-87db-11e9-b83d-000c298bc91b:12';
# 這個就是根據上面得出的,簡單的能夠認爲就是:Retrieved_Gtid_Set的值,截取冒號取橫槓後面的值。
BEGIN; COMMIT;SET SESSION GTID_NEXT = AUTOMATIC;START SLAVE;SHOW SLAVE STATUS\G;