LoginSignup
5
4

More than 5 years have passed since last update.

 先日、mysql5.7を触って始めて、インストール方法を投稿しました。その後、5.7からリリースされたマルチソースレプリケーションをちょっと触ってみました。本日、設定方法について、紹介したいと思います。

マルチソースレプリケーションとは

 マルチソース(Multi-source)レプリケーション  マルチマスター(multi-master)レプリケーション ではありません。それぞれの構成はこちらの記事を見ると、一目瞭然だと思います。

 マルチマスターレプリケーションは、サークル上にリレー的なネットワークを組み込むことになり、循環型のレプリケーションです。一方、マルチソースレプリケーションは、今までのレプリケーションの1つのスレーブは1つのマスターしか持てない仕組みと違い、1つのスレーブは複数のマスターを持つことができる仕組みです

構成

 Master01:test-db01(192.168.56.13)
 Master02:test-db02(192.168.56.14)
 Slave:test-db03(192.168.56.15)

 では、マルチソースレプリケーションを設定します。

マルチソースレプリケーション設定

 初期設定は今までと同じです。そして、二台のマスターの設定が同じです。

my.cnfの設定

  • Master01、Master02:
 [root@test-db01 ~]# diff /etc/my.cnf.org /etc/my.cnf
 27a28,30
 > 
 > server-id=1
 > log-bin=mysql-bin

 [root@test-db01 ~]# /etc/init.d/mysqld restart
 Stopping mysqld:                                           [  OK  ]
 Starting mysqld:                                           [  OK  ]
 [root@test-db02 ~]# diff /etc/my.cnf.org /etc/my.cnf
 27a28,30
 > 
 > server-id=1
 > log-bin=mysql-bin

 [root@test-db02 ~]# /etc/init.d/mysqld restart
 Stopping mysqld:                                           [  OK  ]
 Starting mysqld:                                           [  OK  ]
  • Slave:
 [root@test-db03 ~]# diff /etc/my.cnf.org /etc/my.cnf
 27a28,33
 > 
 > server-id=3
 > log-bin=mysql-bin
 > 
 > # マルチソースレプリケーションを行うために、スレーブをクラッシュセーフにする
 > master-info-repository=TABLE
 > relay-log-info-repository=TABLE

 [root@test-db03 ~]# /etc/init.d/mysqld restart
 Stopping mysqld:                                           [  OK  ]
 Starting mysqld:                                           [  OK  ]

レプリケーションユーザー作成

Master01 と Master02 が同じ設定です。

mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%' IDENTIFIED BY 'ReplPass00!';
Query OK, 0 rows affected, 1 warning (0.01 sec)

マスター・ステータス確認

Master01:

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      437 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

Master02:

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      437 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

スレーブ側でCHANGE MASTER

Slave:

mysql> CHANGE MASTER TO MASTER_HOST='192.168.56.13', MASTER_USER='repl', MASTER_PASSWORD='ReplPass00!', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=437 FOR CHANNEL 'master01';
Query OK, 0 rows affected, 2 warnings (0.05 sec)

mysql> CHANGE MASTER TO MASTER_HOST='192.168.56.14', MASTER_USER='repl', MASTER_PASSWORD='ReplPass00!', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=437 FOR CHANNEL 'master02';
Query OK, 0 rows affected, 2 warnings (0.04 sec)

 CHANGE MASTER のコマンドは普通のレプリケーションを構築する時と同じようですが、最後に「FOR CHANNEL」につけています。これはどのチャネル(マスター)のスレーブになるかを指定する引数です。
 そして、「FOR CHANNEL」の前にはカンマ入れてはいけません。ご注意してください。

スレーブを起動

Slave:

mysql> START SLAVE FOR CHANNEL 'master01';
Query OK, 0 rows affected (0.01 sec)

mysql> START SLAVE FOR CHANNEL 'master02';
Query OK, 0 rows affected (0.01 sec)

レプリケーションステータス確認

Slave:

mysql> SHOW SLAVE STATUS FOR CHANNEL 'master01'\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.56.13
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 437
               Relay_Log_File: test-db03-relay-bin-master01.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             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: 437
              Relay_Log_Space: 540
              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: 133e604f-a3bd-11e5-858e-080027079e3d
             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: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: master01
           Master_TLS_Version: 
1 row in set (0.00 sec)

mysql> SHOW SLAVE STATUS FOR CHANNEL 'master02'\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.56.14
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 437
               Relay_Log_File: test-db03-relay-bin-master02.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             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: 437
              Relay_Log_Space: 540
              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: a1ef5091-a3c2-11e5-8b14-080027079e3d
             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: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: master02
           Master_TLS_Version: 
1 row in set (0.00 sec)

 次は、データベースを作成して、レプリケーションを検証します。

各マスターでデータベースを作成

Master01:

mysql> create database master01_db01;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| master01_db01      |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

Master02:

mysql> create database master02_db01;
Query OK, 1 row affected (0.02 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| master02_db01      |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

Slave:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| master01_db01      |        ← Master01のDB
| master02_db01      |        ← Master02のDB
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

 スレーブ側が各マスターから同期を取れたことを確認できました。
 では、最低限のマルチソースレプリケーションの構築が完了しました。

マルチソースレプリケーションのデメリット

  • サーバーの台数を減らします。
  • 異なるDBサーバーのデータベースのテーブルをJOIN可能です。

 今度、mysql5.7の処理速度をどのぐらいアップしたかを検証したいと思います。
 では、今回はこれで。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4