【MySQL】MySQLマスター2レプリケーション環境からマスタースレーブライブラリを切り替える


一主二従の環境があると仮定し、一主二従の環境では、一主二従のライブラリMに障害が発生した場合、そのうちの1つをライブラリS 1からメインライブラリに切り替えるとともに、S 2を新しいメインライブラリS 1に向け、可能であれば、障害のメインライブラリMを修復して新しいスレーブライブラリにリセットする必要がある.
構築一主二従レプリケーション環境参考:MySQL構築主従レプリケーション環境.
以下に、レプリケーション環境のプライマリとセカンダリのライブラリからの切り替えを示します.
1、環境情報;
Mater:192.168.1.110
Slave1:192.168.1.111
Slave2:192.168.1.112

2、メイン・スタンバイ状態を表示する;
Masterライブラリ:
mysql> show processlist;
+----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+
| Id | User | Host                | db   | Command     | Time | State                                                         | Info             |
+----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+
|  2 | root | localhost           | NULL | Query       |    0 | starting                                                      | show processlist |
|  3 | repl | 192.168.1.112:49819 | NULL | Binlog Dump |  207 | Master has sent all binlog to slave; waiting for more updates | NULL             |
|  4 | repl | 192.168.1.111:53017 | NULL | Binlog Dump |  165 | Master has sent all binlog to slave; waiting for more updates | NULL             |
+----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+
3 rows in set (0.00 sec)

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select *from t_repl;
+------+-------+---------------------+
| id   | name  | cdate               |
+------+-------+---------------------+
|    1 | Alen  | 2018-03-04 17:56:57 |
|    2 | Repl  | 2018-03-04 20:10:45 |
|    3 | USA   | 2018-03-04 22:19:48 |
|    4 | China | 2018-03-04 22:19:48 |
|    5 | Japan | 2018-03-04 22:23:28 |
|    6 | UK    | 2018-03-04 22:23:28 |
+------+-------+---------------------+
6 rows in set (0.00 sec)

mysql> insert into t_repl(id,name) values(7,'Jacky'),(8,'Tom');
Query OK, 2 rows affected (0.03 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> 

Slave 1ライブラリ:
mysql> show processlist;
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| Id | User        | Host      | db   | Command | Time | State                                                  | Info             |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
|  1 | system user |           | NULL | Connect |  299 | Waiting for master to send event                       | NULL             |
|  2 | system user |           | NULL | Connect |  173 | Slave has read all relay log; waiting for more updates | NULL             |
|  4 | root        | localhost | NULL | Query   |    0 | starting                                               | show processlist |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
3 rows in set (0.00 sec)

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select *from t_repl;
+------+-------+---------------------+
| id   | name  | cdate               |
+------+-------+---------------------+
|    1 | Alen  | 2018-03-04 17:56:57 |
|    2 | Repl  | 2018-03-04 20:10:45 |
|    3 | USA   | 2018-03-04 22:19:48 |
|    4 | China | 2018-03-04 22:19:48 |
|    5 | Japan | 2018-03-04 22:23:28 |
|    6 | UK    | 2018-03-04 22:23:28 |
|    7 | Jacky | 2018-03-05 18:55:32 |
|    8 | Tom   | 2018-03-05 18:55:32 |
+------+-------+---------------------+
8 rows in set (0.00 sec)

mysql> 

Slave 2ライブラリ:
mysql> show processlist;
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| Id | User        | Host      | db   | Command | Time | State                                                  | Info             |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
|  1 | system user |           | NULL | Connect |  356 | Waiting for master to send event                       | NULL             |
|  2 | system user |           | NULL | Connect |  291 | Slave has read all relay log; waiting for more updates | NULL             |
|  4 | root        | localhost | test | Query   |    0 | starting                                               | show processlist |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
3 rows in set (0.00 sec)

mysql> select *from t_repl;
+------+-------+---------------------+
| id   | name  | cdate               |
+------+-------+---------------------+
|    1 | Alen  | 2018-03-04 17:56:57 |
|    2 | Repl  | 2018-03-04 20:10:45 |
|    3 | USA   | 2018-03-04 22:19:48 |
|    4 | China | 2018-03-04 22:19:48 |
|    5 | Japan | 2018-03-04 22:23:28 |
|    6 | UK    | 2018-03-04 22:23:28 |
|    7 | Jacky | 2018-03-05 18:55:32 |
|    8 | Tom   | 2018-03-05 18:55:32 |
+------+-------+---------------------+
8 rows in set (0.00 sec)

mysql> 

3、模擬マスターマスターダウン;
mysql> shutdown;
Query OK, 0 rows affected (0.01 sec)

mysql> system service mysql.server status;
 ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql) exists
mysql> 

4、リポジトリからrelay logのすべての更新が実行されていることを確認し、各ライブラリでstop slave io_を実行するthread、show processlistの出力をチェックします.ステータスはSlave has read all relay logです.waiting for more updatesは、更新が完了したことを示します.
mysql> stop slave io_thread;
Query OK, 0 rows affected (0.01 sec)

mysql> show processlist;
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| Id | User        | Host      | db   | Command | Time | State                                                  | Info             |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
|  2 | system user |           | NULL | Connect | 7500 | Slave has read all relay log; waiting for more updates | NULL             |
|  4 | root        | localhost | test | Query   |    0 | starting                                               | show processlist |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
2 rows in set (0.00 sec)

5.スレーブSlave 1でstop slaveを実行してスレーブサービスを停止し、reset masterを実行してマスターライブラリにリセットする.
mysql> show master status;
Empty set (0.00 sec)

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> reset master;
Query OK, 0 rows affected (0.00 sec)

6、ライブラリSlave 1のlog-binが開いているかどうかをチェックし、開いていない場合は開く.
7.ライブラリSlave 1からのマスターを削除する.infoとrelay-log.info、そうでなければ次の再起動はライブラリから起動します.
8、Slave 2でstop slaveを実行してスレーブサービスを停止し、その後change master toを実行してメインライブラリslave 1を再び指し示し、start slaveを実行してスレーブライブラリを起動する.
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)

mysql> change master to 
    -> master_host='192.168.1.111';
Query OK, 0 rows affected (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected (0.02 sec)

mysql> 

9、Slave 2の状態を検査し、新しいマスターSlave 1を指していることを発見する.
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.111
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql111-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000003
                Relay_Log_Pos: 373
        Relay_Master_Log_File: mysql111-bin.000002
             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: 154
              Relay_Log_Space: 799
              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: 111
                  Master_UUID: c8368e4a-1fa4-11e8-aa25-000c299f40a9
             Master_Info_File: /usr/local/mysql-5.7.21-linux-glibc2.12-x86_64/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: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

10、アプリケーションを新しいマスターライブラリSlave 1に向け、Slave 1のすべての更新をSlave 1のBinlogに書き込み、それによって新しいスレーブライブラリSlave 2に同期する.
新しいマスターSlave 1:
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select *from t_repl;
+------+-------+---------------------+
| id   | name  | cdate               |
+------+-------+---------------------+
|    1 | Alen  | 2018-03-04 17:56:57 |
|    2 | Repl  | 2018-03-04 20:10:45 |
|    3 | USA   | 2018-03-04 22:19:48 |
|    4 | China | 2018-03-04 22:19:48 |
|    5 | Japan | 2018-03-04 22:23:28 |
|    6 | UK    | 2018-03-04 22:23:28 |
|    7 | Jacky | 2018-03-05 18:55:32 |
|    8 | Tom   | 2018-03-05 18:55:32 |
+------+-------+---------------------+
8 rows in set (0.00 sec)

mysql> insert into t_repl(id,name) values(9,'Slave1-->Master');
Query OK, 1 row affected (0.01 sec)

mysql> 

新しいスレーブSlave 2:
mysql> use test ;
Database changed
mysql> select *from t_repl;
+------+-----------------+---------------------+
| id   | name            | cdate               |
+------+-----------------+---------------------+
|    1 | Alen            | 2018-03-04 17:56:57 |
|    2 | Repl            | 2018-03-04 20:10:45 |
|    3 | USA             | 2018-03-04 22:19:48 |
|    4 | China           | 2018-03-04 22:19:48 |
|    5 | Japan           | 2018-03-04 22:23:28 |
|    6 | UK              | 2018-03-04 22:23:28 |
|    7 | Jacky           | 2018-03-05 18:55:32 |
|    8 | Tom             | 2018-03-05 18:55:32 |
|    9 | Slave1-->Master | 2018-03-05 21:28:54 |
+------+-----------------+---------------------+
9 rows in set (0.00 sec)

11、最後に、マスターマスターマスターマスターが修復された場合、Slave 1のスレーブライブラリに再構成される.