MySQL学习笔记10复制错误处理表已存在的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL学习笔记10复制错误处理表已存在的问题相关的知识,希望对你有一定的参考价值。

(1)错误情况

slave上已经有数据表test,而master上并没有这张表,现在在master上新建test表,则slave上的复制过程会出错。

mysqllog记录中相关信息如下:

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复制错误处理表已存在的问题的主要内容,如果未能解决你的问题,请参考以下文章

django 做 migrate 时 表已存在的处理

mysql-学习-10-20170605-复制的修复

将表迁移到 db 时出错...表已存在

迁移数据库(Laravel)时出错:表已存在

MySQL学习笔记13分组复制相关概念

使用 Terraform 更改 hash_key 会导致表已存在错误