| 角色 | OS | MySQL | IP |
|---|---|---|---|
| Master | CentOS 5.2 | 5.1.35 (源碼編譯) | 192.168.1.22 |
| Slave | CentOS 5.2 | 5.1.35 (源碼編譯) | 192.168.1.33 |
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.33' IDENTIFIED BY 'repl_pass';
注意:宿主應為 Slave IP(192.168.1.33),原文誤寫為 Master IP。
/etc/my.cnf[mysqld]
server-id = 1
log-bin = mysql-bin
# 註釋掉預設的 server-id=1(若有)
mysqld_safe --user=mysql &
mysql> FLUSH TABLES WITH READ LOCK;
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 106 | | |
+------------------+----------+--------------+------------------+
mysqldump test > test.sql
mysql> UNLOCK TABLES;
/etc/my.cnf
[mysqld]
server-id = 2
# 註釋掉預設 server-id=1
mysqld_safe --user=mysql &
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.1.22',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='repl_pass',
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=106;
mysql> START SLAVE;
mysql> SHOW PROCESSLIST\G
...
Id: 2
User: system user
State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
出現 system user 且 State 顯示等待 I/O 執行緒更新,表示 Slave 已連接 Master 並開始接收 Relay Log。
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.22
Master_User: repl
Master_Port: 3306
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 106
Relay_Log_File: centos-relay-bin.000002
Relay_Log_Pos: 529
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
...
成功標誌:Slave_IO_Running: Yes且Slave_SQL_Running: Yes同時為 Yes。
-- Master 執行
mysql> USE test;
mysql> CREATE TABLE user (id INT);
-- Slave 驗證
mysql> USE test;
mysql> SHOW TABLES LIKE 'user';
+-------------------+
| Tables_in_test(user) |
+-------------------+
| user |
+-------------------+
表結構同步成功,主從複寫部署完成。
GRANT 宿主必須是 Slave IP,非 Master IPserver-id 主從必須不同,且非 0log-bin