MySQL 實現(xiàn)雙向復(fù)制的方法指南
簡介
我知道有很多文章和指南介紹在互聯(lián)網(wǎng)上實現(xiàn)主-從復(fù)制。在主-從復(fù)制中,主機(jī)影響從機(jī)。但從數(shù)據(jù)庫中的任何更改不會影響主數(shù)據(jù)庫,這篇文章將幫助你實現(xiàn)雙向復(fù)制。(即,無論是主機(jī)還是從機(jī)的更改都將影響這兩個服務(wù)器)。
背景
你能參考Aadhar Joshi的這篇文章實現(xiàn)主從復(fù)制,或者您可以按照以下簡單的步驟:
參考一下:
在機(jī)器A配置主機(jī)(192.168.1.30)
在機(jī)器B配置從機(jī)(192.168.1.29)
我們可以使用下面的步驟來實現(xiàn)這一點
步驟1:機(jī)器A設(shè)置主機(jī)
在主機(jī)中打開配置文件 , 默認(rèn)文件位置為C:\Program Files\MySQL\MySQL Server 5.0\my.ini
在服務(wù)端部分用[mysqld]替換[mysqld]
server-id=1
log-bin=mysql-bin
innodb_flush_log_at_trx_commit=1
sync_binlog=1
binlog_do_db= demo
port=3306
保存配置文件然后關(guān)閉
重啟mysql服務(wù)使其生效。
Step 2 : 機(jī)器B設(shè)置從機(jī) :
在從機(jī)中打開mysql配置文件,默認(rèn)位置為C:\Program Files\MySQL\MySQL Server 5.0\my.ini
在服務(wù)端部分用下面代碼替換[mysqld]
[mysqld]
server-id=2
log-bin=mysql-bin
innodb_flush_log_at_trx_commit=1
sync_binlog=1
保存配置文件,關(guān)閉
重啟mysql服務(wù)使之生效。
where :
server-id=1 →服務(wù)的唯一標(biāo)識. 在主從中必須不同。
log-bin=mysql-bin →要在InnoDB復(fù)制設(shè)置中使用事務(wù),保證最好的持久性和一致性, 你應(yīng)該使用 innodb_flush_log_at_trx_commit=1 and sync_binlog=1.
binlog_do_db= demo →要復(fù)制的數(shù)據(jù)庫
port=3306 →默認(rèn)數(shù)據(jù)庫端口
Step 3 : 主機(jī)中創(chuàng)建用戶
打開mysql命令行
現(xiàn)在我們用這個命令‘show master status'來確定當(dāng)前二進(jìn)制日志文件的文件名和位置。記住這個細(xì)節(jié)!在我們的事例中得到以下輸出:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000153 | 106 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
繼續(xù):mysql> FLUSH PRIVILEGES;
選擇數(shù)據(jù)庫 :mysql> USE newdatabase;
鎖數(shù)據(jù)庫防止任何新的更改:FLUSH TABLES WITH READ LOCK;
Step 4: 連接主從 :
打開mysql命令行
停止從機(jī) : Stop slave;
執(zhí)行命令
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.1.30',
-> MASTER_USER='replicator',
-> MASTER_PASSWORD='replication',
-> MASTER_LOG_FILE='mysql-bin.000153',
-> MASTER_LOG_POS=106;
4. 重啟從機(jī)開始復(fù)制: Start Slave;
同樣可以點 Master - Slave Replication.
實現(xiàn)雙向復(fù)制的方法
第一步: 在從機(jī)創(chuàng)建主機(jī)用戶
打開從機(jī)的mysql命令行 (192.168.1.29)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'master_replicator'@'%' IDENTIFIED BY 'master';
現(xiàn)在我們用這個命令‘show master status'來確定當(dāng)前二進(jìn)制日志文件的文件名和位置。記住這個細(xì)節(jié)!在我們的事例中得到以下輸出:
show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000153 | 106 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
繼續(xù):mysql> FLUSH PRIVILEGES;
選擇數(shù)據(jù)庫 :mysql> USE newdatabase;
鎖數(shù)據(jù)庫防止任何新的更改:FLUSH TABLES WITH READ LOCK;
Step 2: 用主機(jī)用戶連接從機(jī)(192.168.1.30):
在主機(jī)上打開mysql命令行
停止從機(jī) : Stop slave;
執(zhí)行命令
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.1.29',
-> MASTER_USER='master_replicator',
-> MASTER_PASSWORD='master',
-> MASTER_LOG_FILE='mysql-bin.000153',
-> MASTER_LOG_POS=106;
4. 重啟從機(jī)開始復(fù)制 : Start Slave;
下面命令檢查系統(tǒng)狀態(tài) :
SHOW SLAVE STATUS\G;
你可以看到 192.168.1.30:
mysql> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.29
Master_User: slave_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000013
Read_Master_Log_Pos: 98
Relay_Log_File: PC-relay-bin.000074
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000013
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: demo
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 98
Relay_Log_Space: 235
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: 0
1 row in set (0.01 sec)
ERROR:
No query specified
感興趣的方面
現(xiàn)在你能做些數(shù)據(jù)庫操作,像插入 刪除 更新 刪表 截斷等,也可以檢查數(shù)據(jù)庫是否正常工作。
重點記住:
1. 在主機(jī)和從機(jī)中server_Id 必須不同
2. 驗證日志文件是正確的設(shè)置
3. 用這些命令檢查
SHOW SLAVE STATUS\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Slave_IO_State: Waiting for master to send event
以上就是本文的全部內(nèi)容了,希望大家能夠喜歡。
請您花一點時間將文章分享給您的朋友或者留下評論。我們將會由衷感謝您的支持!
相關(guān)文章
Mysql插入數(shù)據(jù)方式(insert into 、replace into解析)
這篇文章主要介紹了Mysql插入數(shù)據(jù)方式(insert into 、replace into解析),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

