目錄
- 01 sql_slave_skip_counter參數(shù)
- 02 slave_skip_errors參數(shù)
- 03 slave-skip-errors=N參數(shù)
- 總結(jié)一下:
今天星期二,早上居然起晚了,上班遲到了,簡直是。。。廢話不多說,在昨天的文章中,我們提到了三個(gè)參數(shù),分別是:
- slave_exec_mode參數(shù);
- sql_slave_skip_counter=N參數(shù);
- slave-skip-errors=N參數(shù)。
這三個(gè)參數(shù)都可以解決并行復(fù)制中的一些指定的錯(cuò)誤,例如duplicate key 1062錯(cuò)誤等,今天我們簡單試驗(yàn)一下,這三個(gè)參數(shù)的區(qū)別:
01 sql_slave_skip_counter參數(shù)
這個(gè)參數(shù)的設(shè)置主要是為了跳過某些錯(cuò)誤的"event",注意這里的用詞是event而不是事務(wù),是因?yàn)樗谋举|(zhì)是跳過一個(gè)一個(gè)事件,需要注意的是,這個(gè)參數(shù)需要在偏移量復(fù)制模式中使用,如果使用的是gtid的復(fù)制模式,則不可以使用這個(gè)參數(shù)。我們來看例子,首先搭建一套復(fù)制關(guān)系:
master 10.30.124.68
slave 10.30.124.128
這倆實(shí)例互為主從。我們創(chuàng)建測試表test.yeyz,并插入一些數(shù)據(jù),其中id為主鍵,具有唯一性,如下:
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)
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)
我們可以發(fā)現(xiàn),從節(jié)點(diǎn)的數(shù)據(jù)比主節(jié)點(diǎn)多一條,多了id=5的記錄,然后我們?cè)谥鞴?jié)點(diǎn)上插入數(shù)據(jù):
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
此時(shí)觀察從節(jié)點(diǎn):
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
可以發(fā)現(xiàn),從節(jié)點(diǎn)已經(jīng)SQL線程斷開了, 這個(gè)時(shí)候,在主節(jié)點(diǎn)上查詢這個(gè)錯(cuò)誤position 492處的binlog,可以看到:
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)
從上面的binlog可以看出來,我們的一個(gè)insert操作實(shí)際上生成了5個(gè)enent,分別對(duì)應(yīng)的pos是從259~492,關(guān)于event,待會(huì)兒再說。
因?yàn)橹鞴?jié)點(diǎn)上插入了id=5的記錄,跟從節(jié)點(diǎn)上的記錄沖突了,查看錯(cuò)誤日志,可以發(fā)現(xiàn):
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
我們通過sql_slave_skip_counter參數(shù)的設(shè)置來解決這個(gè)問題,步驟如下:
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;
在昨天的文章中我們說過,sql_slave_skip_counter后面跟的值是event的個(gè)數(shù),所以這里我們相當(dāng)于跳過了一個(gè)event,mysql中規(guī)定,如果跳過一個(gè)event之后,還在某一個(gè)事務(wù)里面,那么會(huì)繼續(xù)跳過這個(gè)事務(wù)。
使用這個(gè)參數(shù)跳過一個(gè)event之后,我們?cè)賮砜磸膸毂碇械臄?shù)據(jù)和復(fù)制情況,可以看到:
slave表:
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)
看看master表:
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)
可以發(fā)現(xiàn),master中數(shù)據(jù)插入成功,而slave中數(shù)據(jù)插入失敗,也就是說:
該參數(shù)跳過錯(cuò)誤的時(shí)候,會(huì)導(dǎo)致主從的數(shù)據(jù)不一致。
02 slave_skip_errors參數(shù)
這個(gè)參數(shù)是跳過制定的錯(cuò)誤,也就是說,需要我們?cè)O(shè)置對(duì)應(yīng)的error_code,從下面的日志中的內(nèi)容可以看出,error_code的值為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
我們需要手動(dòng)將這個(gè)參數(shù)的值也該為1062,需要注意的是,這個(gè)參數(shù)的改動(dòng)需要重啟mysql服務(wù),因?yàn)檫@個(gè)參數(shù)是一個(gè)只讀的參數(shù)。
修改后的情況如下:
mysql--dba_admin@127.0.0.1:(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)
此時(shí)我們更新master表和slave表的數(shù)據(jù),更新后的情況如下:
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)
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)
我們發(fā)現(xiàn),slave表比master表多一條數(shù)據(jù),也就是id=7的記錄,此時(shí)我們?cè)趍aster上執(zhí)行:
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
查看slave上面的復(fù)制情況和數(shù)據(jù)情況,如下:
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)
可以看到,復(fù)制沒有出現(xiàn)錯(cuò)誤,即使從庫上已經(jīng)有id=7的記錄。而且發(fā)現(xiàn),從庫的數(shù)據(jù)跟之前保持一致,也就是說,主庫插入的id=8的記錄沒有被同步過來。
總結(jié)一下:該參數(shù)在跳過復(fù)制錯(cuò)誤的時(shí)候,需要重啟mysql服務(wù),然后可能導(dǎo)致主從數(shù)據(jù)不一致。
03 slave-skip-errors=N參數(shù)
再看最后一個(gè)參數(shù),這個(gè)參數(shù)表示的是并行復(fù)制過程中的從庫復(fù)制模式,默認(rèn)值是strict嚴(yán)格模式,和上面一樣,我們先看主庫和從庫的數(shù)據(jù)情況:
master數(shù)據(jù):
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數(shù)據(jù):
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)
此時(shí)我們?cè)趶膸焐闲薷膮?shù)如下:
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)
修改完參數(shù),我們?cè)谥鲙焐线M(jìn)行insert操作:
insert into test.yeyz values (9,9),(10,10);
查看從庫的復(fù)制狀態(tài)和數(shù)據(jù)情況,如下:
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)
可以發(fā)現(xiàn),既沒有出現(xiàn)復(fù)制錯(cuò)誤,主庫上插入的數(shù)據(jù)也同步過來了。
總結(jié)一下:
- slave_exec_mode參數(shù);
- sql_slave_skip_counter=N參數(shù);
- slave-skip-errors=N參數(shù)。
這三個(gè)參數(shù)都能解決復(fù)制過程中的不一致情況,區(qū)別如下:
slave_exec_mode參數(shù)可以保證主從數(shù)據(jù)一致,其他兩個(gè)不可以。
slave-skip-errors參數(shù)可以跳過制定的錯(cuò)誤,但是需要重啟實(shí)例,不能保證數(shù)據(jù)一致。
sql_slave_skip_counter參數(shù)需要在偏移量的復(fù)制模式下使用,不能保證數(shù)據(jù)一致。
以上就是MySQL復(fù)制問題的三個(gè)參數(shù)分析的詳細(xì)內(nèi)容,更多關(guān)于MySQL復(fù)制問題的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:- MySQL5.7并行復(fù)制原理及實(shí)現(xiàn)
- 詳解MySQL主從復(fù)制及讀寫分離
- MySQL主從復(fù)制斷開的常用修復(fù)方法
- MySql主從復(fù)制機(jī)制全面解析
- MySQL系列之十三 MySQL的復(fù)制