【MySQL】MySQL構築マスターレプリケーション(デュアルマスターレプリケーション/DUAL Master)環境


マスターレプリケーションとは、マスター1とマスター2の相互マスターであり、以下にその構築手順を具体的に示す.
ソフトウェア環境
  • Mysql 5.7.21
  • Oracle Linux 7.1

  • 2ホストの設定
  • Master1 IP:10.24.33.186
  • Master2 IP:10.24.33.188

  • トリプルマスター構成
    1、Master 1からMaster 2までの主従レプリケーション環境構築プロセス参考:MySQL主従レプリケーション環境構築
    注意:互いに主従するため、自動的に生成されるシーケンスの設定に注意します.
    Master 1のプロファイル:
    [root@strong mysql]# more /etc/my.cnf 
    [mysqld]
    
    character-set-server=utf8
    
    basedir=/usr/local/mysql
    datadir=/usr/local/mysql/data
    
    log-bin=/usr/local/mysql/binlog/mysql-bin
    server-id=1
    
    gtid_mode=ON
    enforce-gtid-consistency=true
    
    auto_increment_increment=2
    auto_increment_offset=1
    
    
    [mysql]
    
    default-character-set=utf8

    2、マスター2からマスター2までのマスターコピー環境は以下のように構築されている.
    1)複製ユーザの作成
    mysql> create user 'repl'@'10.24.33.186' identified by 'repl';
    Query OK, 0 rows affected (0.04 sec)
    
    mysql> grant replication slave on *.* to 'repl'@'10.24.33.186';
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> 

    2)Master 1構成の設定
    mysql> change master to
        -> master_host='10.24.33.188',
        -> master_port=3306,
        -> master_user='repl',
        -> master_password='repl',
        -> master_auto_position=1;--  GTID  
    Query OK, 0 rows affected, 2 warnings (0.05 sec)
    mysql> start slave;
    Query OK, 0 rows affected (0.03 sec)
    
    mysql> show slave status\G;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 10.24.33.188
                      Master_User: repl
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000018
              Read_Master_Log_Pos: 194
                   Relay_Log_File: strong-relay-bin.000016
                    Relay_Log_Pos: 407
            Relay_Master_Log_File: mysql-bin.000018
                 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: 194
                  Relay_Log_Space: 1339
                  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
                      Master_UUID: fb42b433-06fc-11e8-b91a-0800275b8da0
                 Master_Info_File: /usr/local/mysql/data/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: fb42b433-06fc-11e8-b91a-0800275b8da0:1-5
                Executed_Gtid_Set: 2a3e3fd9-0587-11e8-bdb8-0800272325a8:1-21,
    fb42b433-06fc-11e8-b91a-0800275b8da0:1-5
                    Auto_Position: 1
             Replicate_Rewrite_DB: 
                     Channel_Name: 
               Master_TLS_Version: 
    1 row in set (0.00 sec)
    
    ERROR: 
    No query specified

    3)Master 2のプロファイル
    [root@strong ~]# more /etc/my.cnf 
    [mysqld]
    
    character-set-server=utf8
    
    basedir=/usr/local/mysql
    datadir=/usr/local/mysql/data
    
    log-bin=/usr/local/mysql/binlog/mysql-bin
    server-id=2
    
    gtid_mode=ON
    enforce_gtid_consistency=true
    
    auto_increment_increment=2
    auto_increment_offset=2

    四主主レプリケーションの検証
    1、Master 1操作
    mysql> use test;
    Database changed
    mysql> insert into t_mm (name) values('Alen'),('Jack'),('Summer');
    Query OK, 3 rows affected (0.01 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> select *from t_mm;
    +----+--------+---------------------+
    | id | name   | cdate               |
    +----+--------+---------------------+
    |  1 | Alen   | 2018-02-28 17:26:08 |
    |  3 | Jack   | 2018-02-28 17:26:08 |
    |  5 | Summer | 2018-02-28 17:26:08 |
    +----+--------+---------------------+
    3 rows in set (0.00 sec)

    2、Master 2操作
    mysql> use test;
    Database changed
    mysql>  insert into t_mm (name) values('China'),('Japon'),('USA');
    Query OK, 3 rows affected (0.04 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> select *from t_mm;
    +----+-------+---------------------+
    | id | name  | cdate               |
    +----+-------+---------------------+
    |  2 | China | 2018-02-28 17:26:26 |
    |  4 | Japon | 2018-02-28 17:26:26 |
    |  6 | USA   | 2018-02-28 17:26:26 |
    +----+-------+---------------------+
    3 rows in set (0.00 sec)

    3、Master 1とMaster 2を別々に見て、同じ結果になる
    mysql> select *from t_mm;
    +----+--------+---------------------+
    | id | name   | cdate               |
    +----+--------+---------------------+
    |  1 | Alen   | 2018-02-28 17:26:08 |
    |  2 | China  | 2018-02-28 17:26:26 |
    |  3 | Jack   | 2018-02-28 17:26:08 |
    |  4 | Japon  | 2018-02-28 17:26:26 |
    |  5 | Summer | 2018-02-28 17:26:08 |
    |  6 | USA    | 2018-02-28 17:26:26 |
    +----+--------+---------------------+
    6 rows in set (0.00 sec)

    これで、プライマリ・レプリケーション環境の構築に成功しました.