MySQLマスターコピーおよび関連ピット


MySQLマスターコピーおよび関連ピット
プライマリ・レプリケーションの本質は、2台のMySQLサーバが互いにプライマリ・スレーブであることです.しかし、このような構成では、データの不一致によるプライマリ・キーの競合や、他のエラーなどの問題が発生しやすい.プライマリ・キーの競合を減らすためには、2つのノードのidにそれぞれテクノロジーと偶数を使用させることが考えられ、2つのサーバ・オプションで構成する必要があります.
auto_increment_offset       #  id    
auto_increment_increment    #  id   

プライマリ・プライマリ・レプリケーションでは推奨されません.実際に使用する必要がある場合は、プライマリ・レプリケーションとして使用します.
プライマリ・レプリケーションの構築
2台のホストを使用してプライマリ・レプリケーションを構成
ホスト
ip
Master1
192.168.73.110
Master2
192.168.73.111
マスター1の設定
1.プロファイルの変更
[root@Master1 ~]# vim /etc/my.cnf
[mysqld]
log-bin
server-id=1
auto_increment_offset=1
auto_increment_increment=2

2.MySQLサービスの起動
[root@Master1 ~]# systemctl start mariadb

3.バイナリ・ログの場所の表示
[root@Master1 ~]# mysql -e "SHOW MASTER LOGS;"
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |       245 |
+--------------------+-----------+

4.データをコピーするユーザーを作成する
[root@Master1 ~]# mysql -e "GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.73.%' IDENTIFIED BY 'centos';"

マスター2をマスター1のスレーブノードに設定
1.プロファイルの変更
[root@Master2 ~]# vim /etc/my.cnf
[mysqld]
log-bin
server-id=2
auto_increment_offset=1
auto_increment_increment=2

2.CHANGE MASTER TOの設定
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.73.110', MASTER_USER='repluser',MASTER_PASSWORD='centos',MASTER_PORT=3306,MASTER_LOG_FILE='mariadb-bin.000001',MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.01 sec)

3.スレーブノードの状態を確認し、エラーがないことを確認する
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.73.110
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No

4.スレッドの開始
MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.01 sec)

5.スレーブノードのステータスを再度表示する
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.73.110
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 407
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 693
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes          #        

6.バイナリ・ログの場所を表示バイナリ・ログの場所を表示する2次ログの場所を表示します.マスター1をスレーブ・ノードとして使用します.Master 2にデータのないバイナリ・ログはクリーン・ログであるため、Master 1で直接使用できます.
MariaDB [(none)]> SHOW MASTER LOGS;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |       245 |
+--------------------+-----------+
1 row in set (0.00 sec)

マスター1をマスター2のスレーブノードに設定
1.CHANGE MASTER TOの情報入力
CHANGE MASTER TO MASTER_HOST='192.168.73.111', MASTER_USER='repluser',MASTER_PASSWORD='centos',MASTER_PORT=3306,MASTER_LOG_FILE='mariadb-bin.000001',MASTER_LOG_POS=245;

2.スレーブ状態を確認し、エラーがないことを確認する
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.73.111
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No

3.スレッドの開始
MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.01 sec)

4.slave statusを再度確認する
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.73.111
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 531
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

マスターコピーの構築完了
テスト
テスト一、Master 1入力データを確認し、Master 2がコピーできるかどうかを確認する
1.マスター1からhellodbデータベースをインポートする
[root@Master1 ~]# mysql -e "SHOW DATABASES;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
[root@Master1 ~]# mysql < hellodb_innodb.sql 
[root@Master1 ~]# mysql -e "SHOW DATABASES;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+

2.ノードからのデータベースの表示
[root@Master2 ~]# mysql -e "SHOW DATABASES;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+

テスト2、Master 2挿入データMaster 1がコピーできるかどうかを確認する
1.Master 2にバーレコードを挿入する
[root@Master2 ~]# mysql -e "INSERT hellodb.teachers(name,age) VALUE ('Ye Fan','25');"
[root@Master2 ~]# mysql -e "INSERT hellodb.teachers(name,age) VALUE ('Shi Hao','20');"
[root@Master2 ~]# mysql -e "SELECT * FROM hellodb.teachers"
+-----+---------------+-----+--------+
| TID | Name          | Age | Gender |
+-----+---------------+-----+--------+
|   1 | Song Jiang    |  45 | M      |
|   2 | Zhang Sanfeng |  94 | M      |
|   3 | Miejue Shitai |  77 | F      |
|   4 | Lin Chaoying  |  93 | F      |
|   5 | Ye Fan        |  25 | NULL   |
|   7 | Shi Hao       |  20 | NULL   |      #             tid  2      。
+-----+---------------+-----+--------+

2.Master 1でのデータの表示
[root@Master1 ~]# mysql -e "SELECT * FROM hellodb.teachers;"
+-----+---------------+-----+--------+
| TID | Name          | Age | Gender |
+-----+---------------+-----+--------+
|   1 | Song Jiang    |  45 | M      |
|   2 | Zhang Sanfeng |  94 | M      |
|   3 | Miejue Shitai |  77 | F      |
|   4 | Lin Chaoying  |  93 | F      |
|   5 | Ye Fan        |  25 | NULL   |
|   7 | Shi Hao       |  20 | NULL   |
+-----+---------------+-----+--------+

テスト3、両方で同じテーブルを同時に作成する
1.両方のホストに対して同時にテーブルを作成する操作2.Master 1のhellodbライブラリの表示
[root@Master1 ~]# mysql -e "SHOW TABLES FROM hellodb"
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| test              |
| toc               |
+-------------------+

3.Master 2のhellodbライブラリの表示
[root@Master2 ~]# mysql -e "SHOW TABLES FROM hellodb"
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| test              |
| toc               |
+-------------------+

ここでは聞いていないようだ.
テスト4、データの挿入を続け、レプリケーションの状況を見る
1.Master 1でhellodbに進む.testテーブルにデータを挿入
[root@Master1 ~]# mysql -e "INSERT hellodb.test VALUE(1,'Tang San');"

2.Master 2でのレプリケーションの表示
[root@Master2 ~]# mysql
MariaDB [(none)]> SELECT * FROM hellodb.test;
Empty set (0.00 sec)

#       

チェックを誤る
マスター1とマスター2ホスト上のSLAVE STATUSをそれぞれ表示します.Master 1ステータス
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.73.111
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 871
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 1018
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1050
                   Last_Error: Error 'Table 'test' already exists' on query. Default database: ''. Query: 'CREATE TABLE hellodb.test(id int auto_increment primary key,name  char(20))'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 732
              Relay_Log_Space: 1453
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 1050
               Last_SQL_Error: Error 'Table 'test' already exists' on query. Default database: ''. Query: 'CREATE TABLE hellodb.test(id int auto_increment primary key,name  char(20))'
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 2
1 row in set (0.00 sec)

ERROR: No query specified

Master 2ステータス
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.73.110
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 8360
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 8308
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1050
                   Last_Error: Error 'Table 'test' already exists' on query. Default database: ''. Query: 'CREATE TABLE hellodb.test(id int auto_increment primary key,name  char(20))'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 8022
              Relay_Log_Space: 8942
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 1050
               Last_SQL_Error: Error 'Table 'test' already exists' on query. Default database: ''. Query: 'CREATE TABLE hellodb.test(id int auto_increment primary key,name  char(20))'
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)

ERROR: No query specified

両方のテーブルが同時に作成されたため、テーブルの作成時にレプリケーションエラーが発生したことが表示されます.
列を間違える
スレッドをプライマリ・スレーブ・ノードでそれぞれ停止
MariaDB [(none)]> STOP SLAVE;

マスタスレーブノードでsql_をそれぞれ使用slave_skip_counter無視エラー
MariaDB [(none)]> SET GLOBAL sql_slave_skip_counter=1;

プライマリ・スレーブ・ノードでスレッドを再起動
MariaDB [(none)]> START SLAVE;

ノードからtestテーブルを再度確認する
[root@Master2 ~]# mysql -e "SELECT * FROM hellodb.test;"
+----+----------+
| id | name     |
+----+----------+
|  1 | Tang San |
+----+----------+

データは正常にコピーされました