GTID (Global Transaction ID) = source_id:transaction_id,source_id 為 server_uuid,transaction_id 為遞增序號。例:e6954592-8dba-11e6-af0e-fa163e1cf111:1-5 表示連續事務範圍。
gtid_executed:實例上已執行事務集合(含 binlog 事務)gtid_purged:已清理 binlog 的事務集合(gtid_executed 子集)gtid_next:會話級,控制下一事務 GTID 生成方式(AUTOMATIC/ANONYMOUS/顯式指定)MySQL 5.6:binlog 事件 Previous_gtids_log_event(文件頭)+ Gtid_log_event(每事務前)。
MySQL 5.7+:新增系統表 mysql.gtid_executed,無需 log_bin/log_slave_updates 也能持久化。
# my.cnf log_bin = /mysql/binlog/mysql_bin log_slave_updates = true gtid_mode = ON enforce_gtid_consistency = true relay_log_info_repository = TABLE relay_log_recovery = ON
從庫執行:
CHANGE MASTER TO MASTER_HOST='node1', MASTER_USER='repl', MASTER_PASSWORD='repl', MASTER_AUTO_POSITION=1;
slave_gtid_executed = UNION(gtid_executed, Retrieved_gtid_set)slave_gtid_executed ⊆ gtid_executed 且 gtid_purged ⊆ slave_gtid_executedPrevious_gtids ⊆ slave_gtid_executed 的文件slave_gtid_executed 中的事務gtid_next)SET GTID_NEXT='uuid:err_txn_id'; BEGIN; COMMIT; SET GTID_NEXT='AUTOMATIC'; START SLAVE;
gtid_purged)RESET MASTER; SET GLOBAL gtid_purged='uuid:1-10'; START SLAVE;
前提: 主從數據已一致,僅需跳過復制報錯。
SET @@GLOBAL.GTID_PURGED='...',恢復前 RESET MASTERxtrabackup_binlog_info 含 GTID,恢復後手動 SET GLOBAL gtid_purged 再 CHANGE MASTER ... AUTO_POSITION=1滿足三條件即為 GTID failover:所有節點 gtid_mode=1、Executed_Gtid_Set 非空、至少一節點 Auto_Position=1。
--wait_until_gtid_in_sync=1 加速切換| 模式 | 必要配置 |
|---|---|
| 傳統 | relay_log_info_repository=TABLE, relay_log_recovery=ON, (MTS 需 sync_relay_log=1) |
| GTID | sync_binlog=1, innodb_flush_log_at_trx_commit=1, relay_log_recovery=ON(雙1是核心) |
MySQL 5.7 可關閉 log_slave_updates,利用 mysql.gtid_executed 實時持久化避免雙1性能損耗。
MTS (slave_parallel_workers>1) 下 5.6 仍有 relay log 截斷導致啟動失敗 Bug (#83713),臨時方案:RESET SLAVE; CHANGE MASTER ...; START SLAVE;。
同樣需雙1。Crash 後檢查 Executed_Gtid_Set 一致性,一致則 AUTO_POSITION=1 接入新主;不一致則從新主拉備份重建。