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

Windows Server 2003 上 MySQL 主從同步備份完整部署步驟

     編輯:MySQL 資料庫相關知識

環境規劃

角色IPOSMySQL 版本
Master (A)192.168.100.1Win 2003 Server4.1.20
Slave (B)192.168.100.2Win 2003 Server4.1.20

目標:A 寫入變更 → B 即時同步,達到熱備份效果。

步驟 1:Master 建立複寫帳號

GRANT REPLICATION SLAVE, RELOAD, SUPER ON *.*
TO 'backup'@'192.168.100.2' IDENTIFIED BY '1234';

-- 修正舊版密碼演算法相容性
SET PASSWORD FOR 'backup'@'192.168.100.2' = OLD_PASSWORD('1234');

步驟 2:同步初始資料

  1. 停止 Master 寫入(FLUSH TABLES WITH READ LOCK)。
  2. 備份 Master 資料目錄(或 mysqldump --master-data=1 --all-databases)。
  3. 將資料複製到 Slave 資料目錄,確保兩端完全一致。
  4. 解鎖 Master(UNLOCK TABLES)。

步驟 3:Master 設定 my.ini

[mysqld]
server-id = 1
log-bin = C:/mysql/logs/binlog
# 可選:只同步特定資料庫
binlog-do-db = mydb
binlog-ignore-db = test

重啟 Master,開始產生二進制日誌。

步驟 4:Slave 設定 my.ini

[mysqld]
server-id = 2
master-host = 192.168.100.1
master-user = backup
master-password = 1234
master-port = 3306
# 可選:只同步特定資料庫
replicate-do-db = mydb

重啟 Slave,自動連線 Master 開始同步。

步驟 5:驗證同步狀態

-- Master
SHOW MASTER STATUS;
-- 記下 File、Position

-- Slave
SHOW SLAVE STATUS\G
-- 關鍵欄位:
-- Slave_IO_Running: Yes
-- Slave_SQL_Running: Yes
-- Seconds_Behind_Master: 0

常見問題排查

  • Slave 不同步:查 SHOW SLAVE STATUS 的 Last_Error、Last_IO_Error;檢查防火牆 3306、帳號密碼、server-id 唯一性。
  • 修改 Slave 設定不生效:資料目錄下的 master.info 紀錄舊參數,需停止 Slave → 刪除 master.info → 重啟 Slave。
  • 版本不相容:Slave 版本可高於 Master,不可低於 Master。

重要警告

嚴禁兩台伺服器同時對同一資料寫入!若作業系統鎖機制失效,會導致資料損毀。單向主從架構下,唯有 Master 可寫入,Slave 僅接收同步。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved