首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

Linux MySQL 主從同步配置實戰:GTID 模式與傳統 Binlog 位點模式完整步驟

     編輯:MySQL 資料庫相關知識

MySQL 主從複製架構部署指南 (一主一從)

主從同步核心:主庫寫入 Binlog → 從庫 I/O 線程拉取 Relay Log → 從庫 SQL 線程重放。支援非同步 (預設)、半同步 (rpl_semi_sync)、GTID (全域事務 ID) 三種模式,生產建議 GTID + 半同步 確保資料一致性與高可用。

環境資訊

角色IPServer IDMySQL 版本
Master192.168.1.13415.7+/8.0
Slave192.168.1.13825.7+/8.0

一、主庫配置

# /etc/mysql/mysql.conf.d/mysqld.cnf (或 /etc/my.cnf)
[mysqld]
server-id = 1
log-bin = mysql-bin
binlog-format = ROW
gtid-mode = ON
enforce-gtid-consistency = ON
log-slave-updates = ON
binlog-do-db = business_db
binlog-ignore-db = mysql,sys,performance_schema,information_schema
sync_binlog = 1
innodb_flush_log_at_trx_commit = 1
# 半同步 (可選)
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 1000

重啟:systemctl restart mysqld

二、建立同步帳號

CREATE USER 'repl_user'@'192.168.1.%' IDENTIFIED BY 'StrongPassw0rd!';
GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'192.168.1.%';
FLUSH PRIVILEGES;

三、主庫鎖表、備份、記錄位點 (傳統模式) / 直接 GTID

傳統 Binlog 位點模式:

# 主庫鎖表
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
# 記錄 File (如 mysql-bin.000001) 和 Position (如 106)
# 導出資料
mysqldump --all-databases --single-transaction --master-data=2 > master_dump.sql
# 解鎖
UNLOCK TABLES;

GTID 模式 (推薦):無需鎖表記錄位點,直接導出資料,從庫用 MASTER_AUTO_POSITION=1 自動定位。

四、從庫配置

# /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
server-id = 2
log-bin = mysql-bin
binlog-format = ROW
gtid-mode = ON
enforce-gtid-consistency = ON
log-slave-updates = ON
relay-log = relay-bin
read-only = ON
replicate-do-db = business_db
replicate-ignore-db = mysql,sys,performance_schema,information_schema
# 半同步
rpl_semi_sync_slave_enabled = 1

重啟:systemctl restart mysqld

五、導入資料並啟動同步

# 從庫導入主庫備份
mysql < master_dump.sql

# 傳統模式啟動
CHANGE MASTER TO
  MASTER_HOST='192.168.1.134',
  MASTER_USER='repl_user',
  MASTER_PASSWORD='StrongPassw0rd!',
  MASTER_LOG_FILE='mysql-bin.000001',
  MASTER_LOG_POS=106;

# GTID 模式啟動 (推薦)
CHANGE MASTER TO
  MASTER_HOST='192.168.1.134',
  MASTER_USER='repl_user',
  MASTER_PASSWORD='StrongPassw0rd!',
  MASTER_AUTO_POSITION=1;

START SLAVE;

六、驗證同步狀態

SHOW SLAVE STATUS\G
# 關鍵指標
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
# GTID 模式額外檢查
Retrieved_Gtid_Set: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:1-100
Executed_Gtid_Set: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:1-100

七、常見故障排查

現象可能原因處理
IO_Running=No網路不通/帳號密碼錯/防火牆/主庫 Binlog 未開檢查連通性、帳號、主庫 show master status
SQL_Running=No重放錯誤 (重複鍵/外鍵/字元集/版本不兼容)SHOW SLAVE STATUS\G 看 Last_SQL_Error,修正資料後 STOP SLAVE; SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE; 或重做從庫
Seconds_Behind_Master 偏大從庫硬體弱/大事務/長 SQL/鎖衝突升級從庫規格、優化慢 SQL、啟用平行複製 slave_parallel_workers=4
GTID 不一致從庫誤寫入/手動執行 DDL重做從庫或用 gtid_purged 修正 (高風險)

運維最佳實踐

  • 監控 Seconds_Behind_Master、Slave_*_Running、Relay Log 空間、Binlog 磁碟用量。
  • 定期 pt-table-checksum / pt-table-sync 校驗主從一致性。
  • 從庫啟用 read_only=ON、super_read_only=ON (8.0) 防止誤寫入。
  • 備份在從庫執行,避免影響主庫效能。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved