Docker环境下MySQL5.7双主配置

1,运行环境

OS 版本:CentOS Linux release 7.5.1804
MySQL 版本:MySQL Community Server 5.7.26
Docker 版本:ce 19.03.3

2,启动MySQL Container

#!/usr/bin/env bash
docker run -it \
--name mysql57-1 \
-h mysql57-1 \
--privileged=true \
--cap-add SYS_PTRACE \
--restart=always \
--net=host \
-e TZ=Asia/Hong_Kong \
-v /data/mysql57-1:/usr/local/mysql \
-v /data/mysql57-data-1:/data/mysql \
-v /data/mysql57-log-1:/data/logs \
-d  \
reg.XXX.com.cn/XXc-devops/centos7-mysql5.7.26 \
mysqld --user=mysql --port=33306 --bind-address=0.0.0.0 --server-id=5 --relay-log=mysql1-relay-bin
#!/usr/bin/env bash
docker run -it \
--name mysql57-2 \
-h mysql57-2 \
--privileged=true \
--cap-add SYS_PTRACE \
--restart=always \
--net=host \
-e TZ=Asia/Hong_Kong \
-v /data/mysql57-2:/usr/local/mysql \
-v /data/mysql57-data-2:/data/mysql \
-v /data/mysql57-log-2:/data/logs \
-d  \
reg.XXX.com.cn/XXc-devops/centos7-mysql5.7.26 \
mysqld --user=mysql --port=43306 --bind-address=0.0.0.0 --server-id=10 --relay-log=mysql1-relay-bin

注意
同一宿主机的mysql container之间,双主或主从的信息交互,docker的网络模式必须选择host。

3,创建双主同步用户

在双主节点上,各自创建REPLICATION SLAVE和REPLICATION CLIENT权限的同步用户。

mysql> grant REPLICATION SLAVE, REPLICATION CLIENT on *.* to 'replication'@'192.168.2.%' identified by 'slave';
Query OK, 0 rows affected, 1 warning (0.00 sec)

4,双主配置命令

change master to 
master_host='192.168.2.27',
master_port=43306,
master_user='replication',
master_password='slave',
master_auto_position=1;
change master to 
master_host='192.168.2.27',
master_port=33306,
master_user='replication',
master_password='slave',
master_auto_position=1;

5,初始化GTID信息

5.1,检查master GTID信息

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000020
         Position: 502
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: bcbe288c-f175-11e9-a273-0242ac11000a:1-8078
1 row in set (0.00 sec)
mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000027
         Position: 502
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 32bf28e2-f562-11e9-8250-0242ac110003:1-8069
1 row in set (0.00 sec)

5.2,重置master GTID信息

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

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 150
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

注意观察,GTID信息被重置前后的变化。

6,双主配置

6.1,master1执行结果

mysql> change master to 
    -> master_host='192.168.2.27',
    -> master_port=43306,
    -> master_user='replication',
    -> master_password='slave',
    -> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

6.2,master2执行结果

mysql> change master to 
    -> master_host='192.168.2.27',
    -> master_port=33306,
    -> master_user='replication',
    -> master_password='slave',
    -> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec

7,启动双主Slave进程

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

8,检查双主成功配置状态

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.27
                  Master_User: replication
                  Master_Port: 43306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 150
               Relay_Log_File: mysql1-relay-bin.000002
                Relay_Log_Pos: 355
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: newlaw,history
          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: 150
              Relay_Log_Space: 555
              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: 10
                  Master_UUID: 32bf28e2-f562-11e9-8250-0242ac110003
             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: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

主从配置常见问题

slave状态报错和解决方案

slave状态报错

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.27
                  Master_User: replication
                  Master_Port: 43306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000027
          Read_Master_Log_Pos: 692
               Relay_Log_File: mysql1-relay-bin.000002
                Relay_Log_Pos: 355
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB: newlaw,history
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1007
                   Last_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 3 failed executing transaction '32bf28e2-f562-11e9-8250-0242ac110003:3' at master log mysql-bin.000002, end_log_pos 762. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 150
              Relay_Log_Space: 6808332228
              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: 1007
               Last_SQL_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 3 failed executing transaction '32bf28e2-f562-11e9-8250-0242ac110003:3' at master log mysql-bin.000002, end_log_pos 762. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 10
                  Master_UUID: 32bf28e2-f562-11e9-8250-0242ac110003
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 191025 11:13:47
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 32bf28e2-f562-11e9-8250-0242ac110003:1-8069
            Executed_Gtid_Set: 32bf28e2-f562-11e9-8250-0242ac110003:1,
bcbe288c-f175-11e9-a273-0242ac11000a:1-8078
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

mysql报错日志

2019-10-25T03:07:50.960115Z 3 [Note] 'CHANGE MASTER TO FOR CHANNEL '' executed'. Previous state master_host='', master_port= 3306, master_log_file='', master_log_pos= 4, master_bind=''. New state master_host='192.168.2.27', master_port= 43306, master_log_file='', master_log_pos= 4, master_bind=''.
2019-10-25T03:13:47.794850Z 4 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
2019-10-25T03:13:47.795922Z 4 [Note] Slave I/O thread for channel '': connected to master '[email protected]:43306',replication started in log 'FIRST' at position 4
2019-10-25T03:13:47.799284Z 5 [Note] Slave SQL thread for channel '' initialized, starting replication in log 'FIRST' at position 0, relay log './mysql1-relay-bin.000001' position: 4
2019-10-25T03:13:47.810191Z 9 [ERROR] Slave SQL for channel '': Worker 4 failed executing transaction '32bf28e2-f562-11e9-8250-0242ac110003:2' at master log mysql-bin.000002, end_log_pos 571; Error 'Can't create database 'newlaw'; database exists' on query. Default database: 'newlaw'. Query: 'CREATE DATABASE `newlaw` DEFAULT CHARACTER SET gbk', Error_code: 1007
2019-10-25T03:13:47.810248Z 8 [ERROR] Slave SQL for channel '': Worker 3 failed executing transaction '32bf28e2-f562-11e9-8250-0242ac110003:3' at master log mysql-bin.000002, end_log_pos 762; Error 'Can't create database 'history'; database exists' on query. Default database: 'history'. Query: 'CREATE DATABASE `history` DEFAULT CHARACTER SET gb2312', Error_code: 1007
2019-10-25T03:13:47.810283Z 5 [Warning] Slave SQL for channel '': ... The slave coordinator and worker threads are stopped, possibly leaving data in inconsistent state. A restart should restore consistency automatically, although using non-transactional storage for data or info tables or DDL queries could lead to problems. In such cases you have to examine your data (see documentation for details). Error_code: 1756
2019-10-25T03:13:47.810416Z 5 [Note] Slave SQL thread for channel '' exiting, replication stopped in log 'mysql-bin.000002' at position 150
2019-10-25T03:13:56.011523Z 10 [Note] Start binlog_dump to master_thread_id(10) slave_server(10), pos(, 4)

故障原因

GTID的信息干扰,配置双主或主从前,GTID信息应该为空。

解决方案

停止slave进程–》重置GTID信息–》重新配置主从或双主–》启动slave进程。

你可能感兴趣的:(DBA)