mysql主主複製(雙主複製)配置步驟

MySQL主主複製結構區別於主從複製結構。在主主複製結構中,兩臺服務器的任何一臺上面的數據庫存發生了改變都會同步到另外一臺服務器上,這樣兩臺服務器互爲主從,而且都能向外提供服務。
有了上一節的主從複製,那麼主主複製就很容易了。mysql

1、先修改配置文件sql

服務器A(192.168.1.254)配置以下數據庫

 
log-bin   = mysql-bin
server-id = 1 
expire-logs-days  = 100 
replicate-do-db   = test
binlog-ignore-db  = mysql
binlog-ignore-db  = information_schema
auto-increment-increment = 2 
auto-increment-offset = 1
服務器B(192.168.1.252)配置服務器

 
log-bin   = mysql-bin
server-id = 2
expire-logs-days  = 100
replicate-do-db   = test
binlog-ignore-db  = mysql
binlog-ignore-db  = information_schema
auto-increment-increment = 2
auto-increment-offset = 2
兩臺服務器都重啓ide

 
mysql> service mysqld restart
注:二都只有server-id不一樣和 auto-increment- offset不一樣
auto-increment-offset是用來設定數據庫中自動增加的起點的,回爲這兩能服務器都設定了一次自動增加值2,因此它們的起點必須得不一樣,這樣才能避免兩臺服務器數據同步時出現主鍵衝突
replicate-do-db 指定同步的數據庫,咱們只在兩臺服務器間同步test數據庫
另:auto-increment-increment的值應設爲整個結構中服務器的總數,本案例用到兩臺服務器,因此值設爲2測試

2、同步數據spa

本文是用test作的實驗,導出將test.sql文件從254服務器拷貝到252服務器
備份數據前先鎖表,保證數據一致性rest


 
mysql> FLUSH TABLES WITH READ LOCK;orm

 
# mysqldump -uroot -p123456 test> /tmp/test.sql;server

 
mysql> UNLOCK TABLES;

 scp /tmp/test.sql root@192.168.1.252:/tmp
3、相互受權用戶(在A服務器受權一個容許B訪問的用戶,反之亦然)

在服務器A(192.168.1.254)上


 
mysql> GRANT REPLICATION SLAVE ON *.* TO 'mysync'@'192.168.1.252' IDENTIFIED BY PASSWORD '123456';
mysql> flush privileges;
在服務器B(192.168.1.252)上

 
mysql> GRANT REPLICATION SLAVE ON *.* TO 'mysync'@'192.168.1.254' IDENTIFIED BY PASSWORD '123456';
mysql> flush privileges;
4、互告bin-log信息

在服務器A(192.168.1.254)


 
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File     | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000006 |      106 |      | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
在服務器A(192.168.1.252)

 
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File     | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000008 |      192 |      | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
在A服務器(192.168.1.254)上執行


 
mysql> change master to master_host='192.168.1.252',master_user='mysync',master_password='123456',master_log_file='mysql-bin.000008',master_log_pos=192;
在B服務器(192.168.1.252)上執行


 
mysql> change master to master_host='192.168.1.254',master_user='mysync',master_password='123456',master_log_file='mysql-bin.000006',master_log_pos=106;
5、在兩服務器都執行如下命令

mysql> start slave;

6、查看狀態

mysql> show slave status\G
A服務器(192.168.1.254)狀態以下:

 Slave_IO_State: Waiting for master to send event
  Master_Host: 192.168.1.252
  Master_User: mysync
  Master_Port: 3306
Connect_Retry: 60
      Master_Log_File: mysql-bin.000008
  Read_Master_Log_Pos: 192
       Relay_Log_File: mysqld-relay-bin.000009
Relay_Log_Pos: 337
Relay_Master_Log_File: mysql-bin.000008
     Slave_IO_Running: Yes
    Slave_SQL_Running: Yes
      Replicate_Do_DB: test
B服務器(192.168.1.252)狀態以下:


  Slave_IO_State: Waiting for master to send event
  Master_Host: 192.168.1.254
  Master_User: mysync
  Master_Port: 3306
Connect_Retry: 60
      Master_Log_File: mysql-bin.000006
  Read_Master_Log_Pos: 106
       Relay_Log_File: mysqld-relay-bin.000014
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000006
     Slave_IO_Running: Yes
    Slave_SQL_Running: Yes
      Replicate_Do_DB: test
當看到了兩個yes,即:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
說明已經配置成功了

接下來看能夠作一下實驗,測試一下是否同步

PS:在測試的過程中,我也遇到一些問題主要是兩臺機器互相通訊的問題請注意,必定要保持兩臺的服務器的mysql端口都向對方打開,要否則是不能成功同步的。

相關文章
相關標籤/搜索