MySQL多源複製


MySQL多源複製使slave可以同時從多個源master接收事務。 多源複製可用於將多個服務器備份到單個服務器,合併表分片,以及未來自多個服務器的數據合併到單個服務器。 應用事務時,多源複製不會實現任何衝突檢測或解決,若是須要,這些任務將留給應用程序。 在多源複製拓撲中,slave服務器應從每一個接收事務的主服務器建立複製通道。 如下部分介紹如何設置多源複製。

1. 配置多源複製

介紹如何配置多源複製拓撲,並提供有關配置master和slave的詳細信息。這種拓撲須要至少兩個master設備和一個slave設備配置。mysql

1.1 配置環境以下

類型 IP 端口號 server-id 複製通道
master 192.168.56.100 3306 1003306 master1
master 192.168.56.100 3307 1003307 master2
slave 192.168.56.200 3308 2003308

1.2 從庫的重要參數配置

在多源複製中,slave須要基於TABLE的存儲庫。 多源複製與基於FILE的存儲庫不兼容。mysqld使用的存儲庫類型能夠在啓動時配置,也能夠動態配置。web

  • 啓動時配置my.cnf
    master-info-repository=TABLE 
    relay-log-info-repository=TABLE
  • 在線動態配置
    STOP SLAVE;
    SET GLOBAL master_info_repository = 'TABLE';
    SET GLOBAL relay_log_info_repository = 'TABLE';

1.3 在Master上導出須要同步的數據庫

利用mysqldump進行備份及恢復,mysqldump的使用方法見博客MySQL備份恢復sql

  • 在master1上
    mysqldump -S /tmp/mysql3306.sock --master-data=2 --single-transaction --add-drop-database --databases employees > /tmp/empdb.sql
  • 在master2上
    mysqldump -S /tmp/mysql3307.sock --master-data=2 --single-transaction --add-drop-database --databases test > /tmp/testdb.sql
  • 把備份傳輸到slave上
    scp /tmp/empdb.sql /tmp/testdb.sql mysqldb2:/data/backup/

1.4 在master上建立複製帳號

  • 在master1上shell

    mysql> create user 'repl'@'%' identified by 'wanbin'; mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
  • 在master2上數據庫

    mysql> create user 'repl'@'%' identified by 'wanbin'; mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';

1.5 備份數據導入

mysql -S /tmp/mysql3308.sock < empdb.sql 

ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.

#出現錯誤
#解決方法:
mysql> reset master;
mysql -S /tmp/mysql3308.sock < empdb.sql 
mysql -S /tmp/mysql3308.sock < testdb.sql

1.6 查看備份文件中Master1與Master2的binlog Pos位置

cat empdb.sql |grep "CHANGE MASTER"
-- CHANGE MASTER TO MASTER_LOG_FILE='my3306_binlog.000004', MASTER_LOG_POS=194;

cat testdb.sql |grep "CHANGE MASTER"
-- CHANGE MASTER TO MASTER_LOG_FILE='my3307_binlog.000011', MASTER_LOG_POS=194;

1.7 Slave進行change master操做

#建立複製帳號
create user 'repl'@'%' identified by 'wanbin'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'; #change master CHANGE MASTER TO MASTER_HOST='192.168.56.100', MASTER_USER='repl', MASTER_PASSWORD='wanbin', MASTER_PORT=3306, MASTER_LOG_FILE='my3306_binlog.000004', MASTER_LOG_POS=194 FOR CHANNEL 'master1'; CHANGE MASTER TO MASTER_HOST='192.168.56.100', MASTER_USER='repl', MASTER_PASSWORD='wanbin', MASTER_PORT=3307, MASTER_LOG_FILE='my3307_binlog.000011', MASTER_LOG_POS=194 FOR CHANNEL 'master2';

1.8 開啓主從複製

能夠經過start slave命令開啓全部複製,也能夠經過 start slave for channel 來分別開啓。bash

mysql> start slave for CHANNEL 'master1';

mysql> start slave for CHANNEL 'master2';

2. 監控多源複製

要監視複製通道的狀態,須要如下選項:
使用複製Performance Schema表。 這些表的第一列是Channel_Name。 這使您能夠基於Channel_Name做爲鍵來編寫複雜查詢。服務器

使用SHOW SLAVE STATUS FOR CHANNEL Channel_Name。 默認狀況下,若是未使用FOR CHANNEL通道子句,則此語句顯示全部通道的從站狀態,每一個通道一行。 標識符Channel_name將添加爲結果集中的列。 若是提供了FOR CHANNEL通道子句,則結果僅顯示指定複製通道的狀態。ide

root@localhost [test] 15:46:02>SHOW SLAVE STATUS FOR CHANNEL 'master1'\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.56.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: my3306_binlog.000004
          Read_Master_Log_Pos: 973
               Relay_Log_File: mysqldb2-relay-bin-master1.000002
                Relay_Log_Pos: 1103
        Relay_Master_Log_File: my3306_binlog.000004
             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: 973
              Relay_Log_Space: 1321
              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: 1003306
                  Master_UUID: 7390a401-b705-11e8-9ed9-080027b0b461
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 7390a401-b705-11e8-9ed9-080027b0b461:10-13
            Executed_Gtid_Set: 3a068bf8-cdeb-11e8-8176-080027b0b461:1-16,
7390a401-b705-11e8-9ed9-080027b0b461:10-13,
d934e7cb-d1d1-11e8-85ce-0800275b8a9a:1-3
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: master1
           Master_TLS_Version: 
1 row in set (0.00 sec)

使用Performance Schema.replication_connection_status 表監控svg

root@localhost [test] 15:49:21>use performance_schema
Database changed
root@localhost [performance_schema] 15:49:43>select * from replication_connection_status\G
*************************** 1. row ***************************
             CHANNEL_NAME: master1
               GROUP_NAME: 
              SOURCE_UUID: 7390a401-b705-11e8-9ed9-080027b0b461
                THREAD_ID: 44
            SERVICE_STATE: ON
COUNT_RECEIVED_HEARTBEATS: 64
 LAST_HEARTBEAT_TIMESTAMP: 2018-10-17 15:49:41
 RECEIVED_TRANSACTION_SET: 7390a401-b705-11e8-9ed9-080027b0b461:10-13
        LAST_ERROR_NUMBER: 0
       LAST_ERROR_MESSAGE: 
     LAST_ERROR_TIMESTAMP: 0000-00-00 00:00:00
*************************** 2. row ***************************
             CHANNEL_NAME: master2
               GROUP_NAME: 
              SOURCE_UUID: 3a068bf8-cdeb-11e8-8176-080027b0b461
                THREAD_ID: 47
            SERVICE_STATE: ON
COUNT_RECEIVED_HEARTBEATS: 58
 LAST_HEARTBEAT_TIMESTAMP: 2018-10-17 15:49:23
 RECEIVED_TRANSACTION_SET: 3a068bf8-cdeb-11e8-8176-080027b0b461:14-16
        LAST_ERROR_NUMBER: 0
       LAST_ERROR_MESSAGE: 
     LAST_ERROR_TIMESTAMP: 0000-00-00 00:00:00
2 rows in set (0.00 sec)
相關文章
相關標籤/搜索