MySQL學習筆記10復制錯誤處理(一)表已存在的問題
(1)錯誤情況
在slave上已經有數據表test,而master上並沒有這張表,現在在master上新建test表,則slave上的復制過程會出錯。
MySQL的log記錄中相關信息如下:
2017-08-15T04:24:30.337730Z 11 [ERROR] Slave SQL for channel ‘‘: Error ‘Table ‘test‘ already exists‘ on query. Default database: ‘test‘. Query: ‘create table test(name2 varchar(100))‘, Error_code: 1050
2017-08-15T04:24:30.337809Z 11 [Warning] Slave: Table ‘test‘ already exists Error_code: 1050
2017-08-15T04:24:30.337819Z 11 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log ‘mysql-bin.000007‘ position 1289
(2)重現出錯場景
(a)在slave上提前建立數據表test。
mysql> create table test (name varchar(100));
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| data |
| data2 |
| t1 |
| tablename |
| test |
+----------------+
5 rows in set (0.00 sec)
(b)在master上建立數據表test。
mysql> create table test(name2 varchar(100));
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| data |
| data2 |
| t1 |
| tablename |
| test |
+----------------+
5 rows in set (0.00 sec)
mysql> insert into test (name2) values (‘001‘), (‘002‘) , (‘003‘);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
(c)查看slave上的數據表。
mysql> show create table test;
+-------+-------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------+
| test | CREATE TABLE `test` (
`name` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from test;
Empty set (0.00 sec)
說明master上的數據表test並沒有復制成功,包括數據表結構和數據表的記錄集。
(d)查看slave復制狀態。
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: mysql101.coe2coe.me
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 1465
Relay_Log_File: mysql103-relay-bin.000016
Relay_Log_Pos: 502
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table: mysql.%,information_schema.%,performance_schema.%,sys.%
Last_Errno: 1050
Last_Error: Error ‘Table ‘test‘ already exists‘ on query. Default database: ‘test‘. Query: ‘create table test(name2 varchar(100))‘
Skip_Counter: 0
Exec_Master_Log_Pos: 1289
Relay_Log_Space: 1568
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: 1050
Last_SQL_Error: Error ‘Table ‘test‘ already exists‘ on query. Default database: ‘test‘. Query: ‘create table test(name2 varchar(100))‘
Replicate_Ignore_Server_Ids:
Master_Server_Id: 101
Master_UUID: a2392929-6dfb-11e7-b294-000c29b1c101
Master_Info_File: /opt/mysql/data/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: 170815 12:24:30
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
可以看到Last_SQL_Error的錯誤代碼1050和錯誤信息:表已存在。
(3)解決辦法。
對於數據表已經存在導致的復制錯誤,可以直接在slave上手工刪除該數據表,然後重新啟動復制。
mysql> drop table test;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| data |
| data2 |
| t1 |
| tablename |
+----------------+
4 rows in set (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: mysql101.coe2coe.me
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 1732
Relay_Log_File: mysql103-relay-bin.000016
Relay_Log_Pos: 502
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table: mysql.%,information_schema.%,performance_schema.%,sys.%
Last_Errno: 1050
Last_Error: Error ‘Table ‘test‘ already exists‘ on query. Default database: ‘test‘. Query: ‘create table test(name2 varchar(100))‘
Skip_Counter: 0
Exec_Master_Log_Pos: 1289
Relay_Log_Space: 1835
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: 1050
Last_SQL_Error: Error ‘Table ‘test‘ already exists‘ on query. Default database: ‘test‘. Query: ‘create table test(name2 varchar(100))‘
Replicate_Ignore_Server_Ids:
Master_Server_Id: 101
Master_UUID: a2392929-6dfb-11e7-b294-000c29b1c101
Master_Info_File: /opt/mysql/data/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: 170815 12:24:30
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
在手工刪除數據表test後,導致錯誤的原因已經解除了,但是復制過程是不知道的。重新啟動slave上的復制過程,即可復制成功。
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: mysql101.coe2coe.me
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 1732
Relay_Log_File: mysql103-relay-bin.000017
Relay_Log_Pos: 320
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: mysql.%,information_schema.%,performance_schema.%,sys.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1732
Relay_Log_Space: 1321
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: 101
Master_UUID: a2392929-6dfb-11e7-b294-000c29b1c101
Master_Info_File: /opt/mysql/data/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: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
此時的復制狀態是正常狀態。查看數據表的結構和內容,可以看到跟master上的數據相同。
mysql> show create table test;
+-------+--------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------+
| test | CREATE TABLE `test` (
`name2` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from test;
+-------+
| name2 |
+-------+
| 001 |
| 002 |
| 003 |
+-------+
3 rows in set (0.00 sec)
至此,表已存在導致的復制錯誤已經被成功排除掉了。
MySQL學習筆記10復制錯誤處理(一)表已存在的問題