Three parameter analysis of MySQL replication

Time:2021-10-14

This Tuesday morning, I got up late and was late for work. It was… Not much nonsense. In yesterday’s article, we mentioned three parameters, namely:

  • slave_ exec_ Mode parameter;
  • sql_ slave_ skip_ Counter = n parameter;
  • Slave skip errors = n parameter.

These three parameters can solve some specified errors in parallel replication, such as duplicate key 1062 error. Today, let’s briefly test the differences between these three parameters:

01 sql_ slave_ skip_ Counter parameter

This parameter is mainly set to skip some wrong “events”. Note that the word here is event rather than transaction because its essence is to skip one event. Note that this parameter needs to be used in the offset replication mode. If the gtid replication mode is used, this parameter cannot be used. Let’s look at an example. First, build a set of replication relationships:

master   10.30.124.68

slave     10.30.124.128

These two examples are master-slave to each other. We create the test table test.yeyz and insert some data. The ID is the primary key, which is unique, as follows:

On master


mysql:(none) 22:25:56>>select * from test.yeyz;
+----+------+
| id | age  |
+----+------+
|  1 |    1 |
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
+----+------+
4 rows in set (0.00 sec)

On slave


mysql:(none) 22:25:38>>select * from test.yeyz;
+----+------+
| id | age  |
+----+------+
|  1 |    1 |
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
|  5 |    5 |
+----+------+
5 rows in set (0.00 sec)

We can find that the slave node has one more record with id = 5 than the master node, and then we insert data on the master node:


mysql:(none) 22:26:06>>insert into test.yeyz values (5,5),(6,6);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Observe the slave node at this time:


mysql:(none) 22:26:34>>show slave status\G
                  Master_Host: 10.30.124.68
                  Master_User: dba_repl
                  Master_Port: 4306
                Connect_Retry: 60
              Master_Log_File: mysqlbin.000002
          Read_Master_Log_Pos: 523
               Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 319
        Relay_Master_Log_File: mysqlbin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
                   Last_Errno: 1062
                   Last_Error: Coordinator stopped because there were error(s) 
in the worker(s). The most recent failure being:
 Worker 0 failed executing transaction 'ANONYMOUS' at
 master log mysqlbin.000002, end_log_pos 492.
 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

It can be found that the SQL thread has been disconnected from the slave node. At this time, query the binlog at the error position 492 on the master node, and you can see:


mysql:(none) 22:30:28>>show binlog events in 'mysqlbin.000002' from 194;  
+-----------------+-----+----------------+-----------+-------------+--------------------------------------------+
| Log_name        | Pos | Event_type     | Server_id | End_log_pos | Info                                       |
+-----------------+-----+----------------+-----------+-------------+--------------------------------------------+
| mysqlbin.000002 | 194 | Anonymous_Gtid |       192 |         259 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'       |
| mysqlbin.000002 | 259 | Query          |       192 |         327 | BEGIN                                      |
| mysqlbin.000002 | 327 | Rows_query     |       192 |         391 | # insert into test.yeyz values (5,5),(6,6) |
| mysqlbin.000002 | 391 | Table_map      |       192 |         439 | table_id: 108 (test.yeyz)                  |
| mysqlbin.000002 | 439 | Write_rows     |       192 |         492 | table_id: 108 flags: STMT_END_F            |
| mysqlbin.000002 | 492 | Xid            |       192 |         523 | COMMIT /* xid=38 */                        |
+-----------------+-----+----------------+-----------+-------------+--------------------------------------------+
6 rows in set (0.00 sec)

From the binlog above, we can see that one of our insert operations actually generates five events, and the corresponding POS ranges from 259 to 492. We’ll talk about events later.

Because a record with id = 5 is inserted into the master node, it conflicts with the records on the slave node. Check the error log and you can find:


Duplicate entry '5' for key 'PRIMARY',
 Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; 
the event's master log FIRST, 
end_log_pos 492 | 2019-07-16 22:26:25

We use SQL_ slave_ skip_ To solve this problem, set the counter parameter as follows:


mysql:(none) 22:29:32>>stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql:(none) 22:32:45>>set global sql_slave_skip_counter=1;
Query OK, 0 rows affected (0.00 sec)

mysql:(none) 22:33:06>>start slave;

In yesterday’s article, we said that SQL_ slave_ skip_ The value followed by counter is the number of events, so here we are equivalent to skipping an event. MySQL stipulates that if an event is still in a transaction after skipping an event, we will continue to skip the transaction.

After skipping an event with this parameter, let’s look at the data and replication in the library table. You can see:

Slave table:


mysql:(none) 22:33:10>>show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.30.124.68
                  Master_User: dba_repl
                  Master_Port: 4306
                Connect_Retry: 60
              Master_Log_File: mysqlbin.000002
          Read_Master_Log_Pos: 523
               Relay_Log_File: slave-relay-bin.000003
                Relay_Log_Pos: 319
        Relay_Master_Log_File: mysqlbin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes


mysql:(none) 22:33:16>>select * from test.yeyz;
+----+------+
| id | age  |
+----+------+
|  1 |    1 |
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
|  5 |    5 |
+----+------+
5 rows in set (0.00 sec)

Look at the master table:


mysql:(none) 22:33:36>>select * from test.yeyz;
+----+------+
| id | age  |
+----+------+
|  1 |    1 |
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
|  5 |    5 |
|  6 |    6 |
+----+------+
6 rows in set (0.00 sec)

It can be found that the data insertion in the master is successful, but the data insertion in the slave fails, that is:

When this parameter skips an error, the master-slave data will be inconsistent.

02 slave_ skip_ Errors parameter

This parameter is an error to skip the formulation, that is, we need to set the corresponding error_ Code, as can be seen from the content in the following log, error_ The value of code is 1062


Duplicate entry '5' for key 'PRIMARY',
 Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; 
the event's master log FIRST, 
end_log_pos 492 | 2019-07-16 22:26:25

We need to manually set the value of this parameter to 1062. It should be noted that the change of this parameter requires restarting the MySQL service, because this parameter is a read-only parameter.

The revised situation is as follows:


[email protected]:(none) 22:38:55>>show variables like '%errors%';
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_connect_errors | 1000000 |
| slave_skip_errors  | 1062    |
+--------------------+---------+
2 rows in set (0.01 sec)

At this time, we update the data of the master table and the slave table. The updated situation is as follows:

master:


mysql:(none) 22:39:15>>select * from test.yeyz;
+----+------+
| id | age  |
+----+------+
|  1 |    1 ||  2 |    2 |
|  3 |    3 ||  4 |    4 |
|  5 |    5 ||  6 |    6 |
+----+------+
6 rows in set (0.00 sec)

On Slave:


mysql:(none) 22:40:15>>select * from test.yeyz;
+----+------+
| id | age  |
+----+------+
|  1 |    1 |
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
|  5 |    5 |
|  6 |    6 |
|  7 |    7 |
+----+------+
7 rows in set (0.00 sec)

We find that the slave table has one more piece of data than the master table, that is, the record with id = 7. At this time, we execute the following on the master table:


mysql:(none) 22:34:15>>insert into test.yeyz values (7,7),(8,8);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

View the replication and data on the slave as follows:


mysql:(none) 22:39:05>>show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.30.124.68
                  Master_User: dba_repl
                  Master_Port: 4306
                Connect_Retry: 60
              Master_Log_File: mysqlbin.000002
          Read_Master_Log_Pos: 852
               Relay_Log_File: slave-relay-bin.000005
                Relay_Log_Pos: 648
        Relay_Master_Log_File: mysqlbin.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: 


mysql:(none) 22:40:15>>select * from test.yeyz;
+----+------+
| id | age  |
+----+------+
|  1 |    1 |
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
|  5 |    5 |
|  6 |    6 |
|  7 |    7 |
+----+------+
7 rows in set (0.00 sec)

As you can see, there is no error in replication, even if there is a record with id = 7 in the library. In addition, it is found that the data from the slave database is consistent with that before, that is, the records with id = 8 inserted from the master database are not synchronized.

To sum up: when skipping replication errors, this parameter needs to restart the MySQL service, which may lead to inconsistent master-slave data.

03 slave skip errors = n parameter

Let’s look at the last parameter. This parameter represents the slave database replication mode in the parallel replication process. The default value is strict mode. As above, let’s look at the data of the master database and slave database first:

Master data:


mysql:(none) 22:39:20>>select * from test.yeyz;                 
+----+------+
| id | age  |
+----+------+
|  1 |    1 |
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
|  5 |    5 |
|  6 |    6 |
|  7 |    7 |
|  8 |    8 |
+----+------+
8 rows in set (0.00 sec)

Slave data:


mysql:(none) 22:42:46>>select * from test.yeyz;
+----+------+
| id | age  |
+----+------+
|  1 |    1 |
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
|  5 |    5 |
|  6 |    6 |
|  7 |    7 |
|  8 |    8 |
|  9 |    9 |
+----+------+
9 rows in set (0.00 sec)

At this time, we modify the parameters from the library as follows:


mysql:(none) 22:42:59>>show variables like '%exec%';
+----------------------------------+--------+
| Variable_name                    | Value  |
+----------------------------------+--------+
| gtid_executed_compression_period | 1000   |
| max_execution_time               | 0      |
| rbr_exec_mode                    | STRICT |
| slave_exec_mode                  | STRICT |
+----------------------------------+--------+
4 rows in set (0.00 sec)

mysql:(none) 22:44:05>>set global slave_exec_mode='IDEMPOTENT';
Query OK, 0 rows affected (0.00 sec)

mysql:(none) 22:44:10>>show variables like '%exec%';           
+----------------------------------+------------+
| Variable_name                    | Value      |
+----------------------------------+------------+
| gtid_executed_compression_period | 1000       |
| max_execution_time               | 0          |
| rbr_exec_mode                    | STRICT     |
| slave_exec_mode                  | IDEMPOTENT |
+----------------------------------+------------+
4 rows in set (0.00 sec)

After modifying the parameters, we perform the insert operation on the main database:

insert into test.yeyz values (9,9),(10,10);

View the replication status and data from the library as follows:


mysql:(none) 22:44:14>>show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.30.124.68
                  Master_User: dba_repl
                  Master_Port: 4306
                Connect_Retry: 60
              Master_Log_File: mysqlbin.000002
          Read_Master_Log_Pos: 1183
               Relay_Log_File: slave-relay-bin.000007
                Relay_Log_Pos: 650
        Relay_Master_Log_File: mysqlbin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

1 row in set (0.00 sec)

mysql:(none) 22:44:38>>select * from test.yeyz;
+----+------+
| id | age  |
+----+------+
|  1 |    1 |
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
|  5 |    5 |
|  6 |    6 |
|  7 |    7 |
|  8 |    8 |
|  9 |    9 |
| 10 |   10 |
+----+------+
10 rows in set (0.00 sec)

It can be found that there are no replication errors, and the data inserted on the main database is synchronized.

To sum up:

  • slave_ exec_ Mode parameter;
  • sql_ slave_ skip_ Counter = n parameter;
  • Slave skip errors = n parameter.

These three parameters can resolve inconsistencies during replication. The differences are as follows:

slave_ exec_ The mode parameter can ensure the consistency of master-slave data, but the other two cannot.

The slave skip errors parameter can skip the specified errors, but the instance needs to be restarted, and the data consistency cannot be guaranteed.

sql_ slave_ skip_ The counter parameter needs to be used in the offset copy mode, and data consistency cannot be guaranteed.

The above is the details of the three parameter analysis of MySQL replication. For more information about MySQL replication, please pay attention to other related articles of developeppaer!