Configure GTID dual master for MySQL 5.7

Environmental planning

192.168.31.178 Master/Slave
192.168.31.179 Master/Slave

192.168.31.178 configuration

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   #Prevent replication from starting automatically with mysql
slave-parallel-type = LOGICAL_CLOCK
server-id = 1

192.168.31.179 configuration

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  #Prevent replication from starting automatically with mysql
server-id = 11    

Create a database to synchronize users and back up the latest data

Both databases need to execute
grant replication slave on *.* to 'ops'@'192.168.10.%' identified by 'ops123';
flush privileges; 
    The latest database performs backup of the latest data
mysqldump -uroot -p --single-transaction --default-character-set=utf8 --master-data=2 --flush-logs --triggers --routines --events  --all-databases > all.sql
 Execute import sql from the corresponding database
scp all.sql 192.168.1.179:/data/
Switch to 179 server to import sql file
source  /data/all.sql

Connecting master and slave

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;        

testing environment

 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 #Indicates the number of transactions received
Executed_Gtid_Set: c6617d6e-a3e4-11ea-bb0d-000c29f6218f:1-8  #Indicates the number of completed transactions

If the master-slave reports an error, you need to restart the master-slave planning from these two parameters

If there is an error in master-slave replication
 When show slave status is executed on slave \ G
Retrieved_Gtid_Set: $UUID:3-9
Executed_Gtid_Set: $UUID:1-8
 Now Slave_SQL_Running: No
 The above information indicates:
slave got it UUID:3-9  Transactions, executed successfully UUID:1-8 , 1-8 indicates that the execution has been completed. There's a mistake here, which is execution UUID:1-9 An error occurred while. So we should skip the next transaction, 9;
resolvent:
Follow these steps

mysql> stop slave;
mysql> set gtid_next='$UUID:9';
mysql> begin;
mysql> commit;
mysql> set gtid_next='automatic';
mysql> start slave;
Before skipping, analyze Binlog and record it to see if it can be skipped. After skipping, check whether the master-slave data is consistent, whether the data needs to be repaired, and so on. Please operate carefully. You can use mysqlcheck to fix the data

Tags: Linux MySQL SQL Database mysqldump

Posted on Tue, 02 Jun 2020 11:05:08 -0400 by root