mysql 主主+主從筆記

環境

Ubuntu 14.04.4 LTS *3 分別是master1(192.168.42.28), master2(192.168.42.29), slave1(192.168.42.33)測試下只有master1有從

配置

vim /etc/mysql/my.cnf 
#必要的配置
[mysqld]
...
bind-address            = 0.0.0.0
#3臺主機的id分別是 1,2,3
server-id               = 1 

binlog-do-db=test #需要記錄二進制日誌的數據庫.如果有多個數據庫可用逗號分隔,或者使用多個binlog-do-db選項 
#需要同步的數據庫 
replicate-do-db=test #需要進行同步的數據庫.如果有多個數據庫可用逗號分隔,或者使用多個binlog-do-db選項 

log_bin                 = /var/log/mysql/mysql-bin.log
#這很主要,不然沒辦法做主從
log-slave-updates
...

同步數據

我使用的是 innobackupex 同步,也可以mysqldump。
master1 master2 slave1都需要同步

master1 配置

#給master2權限
grant replication slave,file on *.* to 'slave'@'192.168.42.29' identified by '123123';
#給slave1權限
grant replication slave,file on *.* to 'slave'@'192.168.42.33' identified by '123123';

stop slave; 
show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000007 |      723 | test         |                  |
+------------------+----------+--------------+------------------+

change master to master_host='192.168.42.29',master_user='slave',master_password='123123',master_log_file='mysql-bin.000008', master_log_pos=267,master_connect_retry=10; #mysql-bin.000008和267是master2的狀態 
#參數
#master_connect_retry默認60秒,重連的時間,如果斷開就60秒從試,直到鏈接上
#master_log_pos,master_log_file爲複製主機的show master status的值


#啓動複製
start slave;
#查看狀態
show slave status\G; 

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.42.29
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000008
          Read_Master_Log_Pos: 267
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000008
             Slave_IO_Running: Yes #顯示YES配置成功
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          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: 267
              Relay_Log_Space: 410
              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: 2
1 row in set (0.00 sec)

master2 配置

#給master2權限
grant replication slave,file on *.* to 'slave'@'192.168.42.28' identified by '123123';

stop slave; 
show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000008 |      267 | test         |                  |
+------------------+----------+--------------+------------------+

change master to master_host='192.168.42.28',master_user='slave',master_password='123123',master_log_file='mysql-bin.000007', master_log_pos=723; 
#啓動複製
start slave;
#查看狀態
show slave status\G; 

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.42.28
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 723
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000007
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          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: 723
              Relay_Log_Space: 410
              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

slave1 配置


stop slave; 

change master to master_host='192.168.42.28',master_user='slave',master_password='123123',master_log_file='mysql-bin.000007', master_log_pos=723; 

show slave status\G  

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.42.28
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 883
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000007
             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: 883
              Relay_Log_Space: 410
              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

測試

分別在master1,master2 插入數據,在查看3個主機的數據

未解決的問題

當前狀態最多能有2臺是主

發佈了54 篇原創文章 · 獲贊 13 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章