MySQL 主主復(fù)制

環(huán)境

服務(wù)器

兩臺(tái) centos6.5 (ip:192.168.1.121、192.168.1.212)

軟件

  • mysql5.6
  • haproxy-1.5.14
  • keepalived-1.2.19

步驟

  1. 在兩臺(tái)服務(wù)器上分別安裝mysql環(huán)境(完全相同的安裝即可)
  1. 配置mysql主主復(fù)制(參考 http://www.cnblogs.com/phpstudy2015-6/p/6485819.html#_label7
    何為主主復(fù)制:就是兩個(gè)mysql都能讀能寫,數(shù)據(jù)記錄通過(guò)二進(jìn)制傳達(dá)給對(duì)方從而保持?jǐn)?shù)據(jù)的一致性。
    實(shí)現(xiàn):
    192.168.1.121(主)+192.168.1.212(從) 主從復(fù)制 +
    192.168.1.212(主)+192.168.1.121(從) 主從復(fù)制 =
    192.168.1.121+192.168.1.212 主主復(fù)制。
    2.1. 修改mysql配置文件
shell> sudo vim /etc/my.cnf

192.168.1.121 mysql配置文件內(nèi)容:

[mysqld]
server-id=1   #任意自然數(shù)n,只要保證兩臺(tái)MySQL主機(jī)不重復(fù)就可以了。
log-bin=mysql-bin   #開(kāi)啟二進(jìn)制日志
auto_increment_increment=2   #步進(jìn)值auto_imcrement。一般有n臺(tái)主MySQL就填n
auto_increment_offset=1   #起始值。一般填第n臺(tái)主MySQL。此時(shí)為第一臺(tái)主MySQL
binlog-ignore=mysql   #忽略mysql庫(kù)【我一般都不寫】
binlog-ignore=information_schema   #忽略information_schema庫(kù)【我一般都不寫】
#replicate-do-db=aa   #要同步的數(shù)據(jù)庫(kù),默認(rèn)所有庫(kù)

192.168.1.212 mysql配置文件內(nèi)容:

[mysqld]
#mysql負(fù)載均衡配置
server-id=2
log-bin=mysql-bin
auto_increment_increment=2 #步進(jìn)值auto_imcrement。一般有n臺(tái)主MySQL就填n
auto_increment_offset=2 #起始值。一般填第n臺(tái)主MySQL。此時(shí)為第二臺(tái)主MySQL
#replicate-do-db=aa #要同步的數(shù)據(jù)庫(kù),默認(rèn)所有庫(kù)

兩臺(tái)服務(wù)器的配置文件修改好后均重啟mysql服務(wù)。
2.2. 配置192.168.1.121(主)+192.168.1.212(從) 主從復(fù)制
使用root用戶登錄192.168.1.121主機(jī)的mysql,創(chuàng)建一個(gè)可以從192.168.1.212登錄的mysql用戶并授權(quán)。

mysql> create user 'mysql212'@'192.168.1.212' identified by 'password';
mysql> GRANT REPLICATION SLAVE ON *.* TO ‘mysql212’@’192.168.1.121’ IDENTIFIED BY 'password';
mysql> FLUSH PRIVILEGES;

查看192.168.1.121 mysql服務(wù)器二進(jìn)制日志

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000076
         Position: 1173222
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

告知從服務(wù)器主服務(wù)器的二進(jìn)制文件名和位置
使用root用戶登錄192.168.1.212主機(jī)的mysql,在192.168.1.121中執(zhí)行:

mysql> change master to 
    -> master_host='192.168.1.121',
    -> master_user='mysql212',
    -> master_password='password',
    -> master_log_file='mysql-bin.000076',
    -> master_log_pos=1173222;

192.168.1.212服務(wù)器開(kāi)啟復(fù)制:

mysql> start slave;

查看主從復(fù)制是否配置成功

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.121
                  Master_User: mysql212
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000076
          Read_Master_Log_Pos: 1173222
               Relay_Log_File: bogon-relay-bin.000006
                Relay_Log_Pos: 1173385
        Relay_Master_Log_File: mysql-bin.000076
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          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: 1173222
              Relay_Log_Space: 1173721
              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
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: e69cc9bd-109e-11e5-b9e7-00e066233823
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)

當(dāng)看到Slave_IO_Running: YES、Slave_SQL_Running: YES才表明狀態(tài)正常。

2.3. 配置192.168.1.212(主)+192.168.1.121(從) 主從復(fù)制
步驟同2.2。

2.4. 測(cè)試主主復(fù)制配置是否成功。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容