MySQL5.7配置GTID双主

wanjichun 2020-06-02

环境规划
192.168.31.178Master/Slave
192.168.31.179Master/Slave

192.168.31.178 配置

gtid-mode=ON
enforce_gtid_consistency=on
log-slave-updates=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
relay_log = /data/mysql/relay.log
relay_log_index = /data/mysql/relay_log.index
report-port = 3306

sync-master-info=1
slave_parallel_workers=5
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
auto-increment-increment = 2   
auto-increment-offset = 1
slave-skip-errors = all
skip-slave-start = 1   #防止复制随着mysql启动而自动启动
slave-parallel-type = LOGICAL_CLOCK
server-id = 1

192.168.31.179 配置

gtid-mode=ON
enforce_gtid_consistency=on
log-slave-updates=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
relay_log = /data/mysql/relay.log
relay_log_index = /data/mysql/relay_log.index
report-port = 3306

sync-master-info=1
slave_parallel_workers=5
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
auto-increment-increment = 2   
auto-increment-offset = 2
slave-parallel-type = LOGICAL_CLOCK
slave-skip-errors = all
skip-slave-start = 1  #防止复制随着mysql启动而自动启动
server-id = 11

创建同步用户和备份最新数据的数据库

两台数据库都要执行
grant replication slave on *.* to ‘ops‘@‘192.168.10.%‘ identified by ‘ops123‘;
flush privileges; 
    最新数据库执行备份最新的数据
mysqldump -uroot -p --single-transaction --default-character-set=utf8 --master-data=2 --flush-logs --triggers --routines --events  --all-databases > all.sql
在对应的从库执行导入sql
scp all.sql 192.168.1.179:/data/
切换到179服务器导入sql文件
source  /data/all.sql

连接主从

192.168.1.178
CHANGE MASTER TO
   MASTER_HOST = ‘192.168.31.179‘,
   MASTER_PORT = 3306,
   MASTER_USER = ‘ops‘,
   MASTER_PASSWORD = ‘ops123‘,
   MASTER_AUTO_POSITION = 1;
192.168.1.179
CHANGE MASTER TO
   MASTER_HOST = ‘192.168.31.178‘,
   MASTER_PORT = 3306,
   MASTER_USER = ‘ops‘,
   MASTER_PASSWORD = ‘ops123‘,
   MASTER_AUTO_POSITION = 1;

测试环境

192.168.31.178
 MySQL [(none)]> show slave status\G
 *************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: 192.168.31.179
                   Master_User: ops
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mysql-bin.000007
           Read_Master_Log_Pos: 38729
                Relay_Log_File: relay.000006
                 Relay_Log_Pos: 367
         Relay_Master_Log_File: mysql-bin.000007
              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: 38729
               Relay_Log_Space: 954
               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: 11
                   Master_UUID: 64d5d956-a3e7-11ea-9725-000c29e5421a
              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: c6617d6e-a3e4-11ea-bb0d-000c29f6218f:1-8
                 Auto_Position: 1
          Replicate_Rewrite_DB: 
                  Channel_Name: 
            Master_TLS_Version: 
192.168.31.179
    MySQL [(none)]> show slave status\G
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.31.178
                      Master_User: ops
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000012
              Read_Master_Log_Pos: 38769
                   Relay_Log_File: relay.000006
                    Relay_Log_Pos: 38942
            Relay_Master_Log_File: mysql-bin.000012
                 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: 38769
                  Relay_Log_Space: 39529
                  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: c6617d6e-a3e4-11ea-bb0d-000c29f6218f
                 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: c6617d6e-a3e4-11ea-bb0d-000c29f6218f:3-8
                Executed_Gtid_Set: c6617d6e-a3e4-11ea-bb0d-000c29f6218f:1-8
                    Auto_Position: 1
             Replicate_Rewrite_DB: 
                     Channel_Name: 
               Master_TLS_Version: 

Retrieved_Gtid_Set: c6617d6e-a3e4-11ea-bb0d-000c29f6218f:3-8 #表示收到的事务数
Executed_Gtid_Set: c6617d6e-a3e4-11ea-bb0d-000c29f6218f:1-8  #表示执行完的事务数

如果主从报错 需要从这两个参数去重新启动规划主从

假如主从复制出现错误
当在 slave 上执行 show slave status\G
Retrieved_Gtid_Set: $UUID:3-9
Executed_Gtid_Set: $UUID:1-8
此时 Slave_SQL_Running: No
上面的信息表明:
slave 收到了 UUID:3-9 个事务,执行成功 UUID:1-8,1-8表示已经执行完成了。在这里出现了错误,也就是说执行 UUID:1-9时出现了错误。所以我们应该要跳过下一个事务即 9;
解决方法:
按照下列步骤执行

mysql> stop slave;
mysql> set gtid_next=‘$UUID:9‘;
mysql> begin;
mysql> commit;
mysql> set gtid_next=‘automatic‘;
mysql> start slave;
在跳过之前,分析一下 Binlog 并且记录下来,分析是否可以跳过。跳过之后,看一下主从数据是否一致,是否需要修复数据等等,请谨慎操作。可以使用mysqlcheck来修复数据

相关推荐