RedHat Linux 9下MySQL雙向同步複製

來源:互聯網
上載者:User

設定MySQL資料雙向同步

一、準備伺服器
由於MySQL不同版本之間的(二進位日誌)binlog格式可能會不一樣,因此最好的搭配組合是Master的MySQL版本和Slave的版本相同或者更低,Master的版本肯定不能高於Slave版本。
more.. | less.. | 本文中,我們假設主伺服器(以下簡稱Master)和從伺服器(以下簡稱Slave)的版本都是5.0.27,作業系統是RedHat Linux 9。
假設同步Master的主機名稱為:master(IP:192.168.1.123),Slave主機名稱為:slave(IP:192.168.1.124),2個MySQL的basedir目錄都是/usr/local/mysql,datadir都是:/var/lib/mysql。

二、設定同步伺服器
1、設定同步Master
修改 my.cnf 檔案,在
# Replication Master Server (default)
# binary logging is required for replication
添加如下內容:
#log-bin=/var/log/mysql/updatelog
server-id = 1
binlog-do-db=discuz
binlog-ignore-db=mysql
重啟MySQL,建立一個MySQL帳號為同步專用
# /usr/local/mysql/bin/mysql -u root -p
mysql> GRANT REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
如果想要在Slave上有許可權執行 "LOAD TABLE FROM MASTER" 或 "LOAD DATA FROM MASTER" 語句的話,必須授予全域的 FILE 和 SELECT 許可權:
mysql>GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO   [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
mysql> FLUSH PRIVILEGES ;

2、設定同步Slave
修改my.cnf檔案,添加
server-id = 2
master-host = 192.168.1.123
master-user = back
master-password = back
master-port = 3306
replicate-ignore-db=mysql
replicate-do-db=discuz

重啟MySQL

3、啟動同步
在主伺服器master MySQL命令符下:
# /usr/local/mysql/bin/mysql -u root -p
mysql> show master status;
顯示(當然這個是我機器的情況,你的不可能跟我一樣哈,只是個例子):
+------------------+----------+-------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+-------------------+------------------+
| mysql-bin.000009 | 98 | discuz | mysql |
+------------------+----------+-------------------+------------------+

在從伺服器master MySQL命令符下:
# /usr/local/mysql/bin/mysql -u root -p
mysql> slave stop;
mysql> change master to master_host='192.168.1.123', master_user='back', master_password='back', master_log_file='mysql-bin.000009', master_log_pos=98;
mysql> slave start;

用show slave status\G;看一下從伺服器的同步情況
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果都是yes,那代表已經在同步
往表裡面寫點資料測試一下看是否同步成功,如果不成功,絕對不是你的RP問題,再檢查一下操作步驟!

4、設定雙向同步

修改slave伺服器的my.cnf,添加
log-bin=/var/log/mysql/updatelog
binlog-do-db=discuz
binlog-ignore-db=mysql

重啟MySQL,建立一個MySQL帳號為同步專用
mysql> GRANT REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
mysql> GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO   [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
mysql> FLUSH PRIVILEGES ;

修改master伺服器的my.cnf,添加
master-host = 192.168.1.124
master-user = back
master-password = back
master-port = 3306
replicate-ignore-db=mysql
replicate-do-db=discuz

重啟MySQL

在主伺服器slave MySQL命令符下:
show master status;
+------------------+----------+-------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+-------------------+------------------+
| mysql-bin.000013 | 98 | discuz | mysql |
+------------------+----------+-------------------+------------------+

在伺服器A MySQL命令符下:
mysql> slave stop;
mysql> change master to master_host='192.168.1.124', master_user='back', master_password='back', master_log_file='mysql-bin.000013', master_log_pos=98;
mysql> slave start;

其實也就是A->B單向同步的反向操作!雙向同步,就這麼簡單啦!


提示:如果修改了主伺服器的配置,記得刪除從伺服器上的master.info檔案。否則從伺服器使用的還是老配置,可能會導致錯誤。
-----------------------------------------------------------------------------------
注意:關於要複製多個資料庫時,binlog-do-db和replicate-do-db選項的設定,網上很多人說是用半形逗號分隔,經過測試,這樣的說法是錯誤的,MySQL官方文檔也明確指出,如果要備份多個資料庫,只要重複設定相應選項就可以了。
比如:
binlog-do-db=a
binlog-do-db=b
replicate-do-db=a
replicate-do-db=b

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.