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)

至此,表已存在导致的复制错误已经被成功排除掉了。

时间: 2024-08-02 02:49:41

MySQL学习笔记10复制错误处理(一)表已存在的问题的相关文章

MySQL学习笔记11复制错误处理(二)删除不存在的行的问题

(1)问题情况 在master上删除某个数据表的某一行,而该行在slave上并不存在,则slave上的复制过程会出错. MySQL的log文件中发现如下错误信息: 2017-08-15T04:52:19.529509Z 13 [ERROR] Slave SQL for channel '': Could not execute Delete_rows event on table test.test; Can't find record in 'test', Error_code: 1032;

MySQL学习笔记10(MySQL函数)

MySQL学习笔记10 MySQL函数 MySQL数据库中提供了很丰富的函数.MySQL函数包括数学函数.字符串函数.日期和时间函数.条件判断函数.系统信息函数.加密函数.格式化函数等.通过这些函数,可以简化用户的操作.SELECT语句及其条件表达式都可以使用这些函数.同时,INSERT.UPDATE.DELECT语句及其条件表达式也可以使用这些函数. 1:数学函数 数学函数是M有SQL中常用的一类函数.主要用于处理数字,包括整型.浮点数等.数学函数包括绝对值函数.正弦函数.余弦函数.获取随机数

MySQL学习笔记—视图

MySQL学习笔记-视图 视图是查看基础表数据的一种方式,其作用有 - 简化开发难度,可以运用视图执行多表操作 - 数据安全,开发人员不能直接对表操作,也不能进行删除,修改操作 - 数据重构,在有限的表中,以不同的角度生成所需的视图,简化业务 CREATE VIEW语法 CREATE VIEW语法: CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] VIEW view_name [(column_list)] AS

MySQL学习笔记-子查询和连接

MySQL学习笔记-子查询和连接 使客户端进入gbk编码方式显示: mysql> SET NAMES gbk; 1.子查询 子查询的定义: 子查询(Subquery)是指出现在其他SQL语句内的SELECT子句. 例如:  SELECT * FROM t1 WHERE col1 = (SELECT col2 FROM t2); 其中SELECT * FROM t1 称为Outer Query / Outer Statement (外部查询) SELECT col2 FROM t2 , 被称为Su

MySql学习笔记(转载)

/* 启动MySQL */net start mysql /* 连接与断开服务器 */mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL */mysqld --skip-grant-tables-- 修改root密码密码加密函数password()update mysql.user set password=password('root'); SHOW PROCESSLIST -- 显示哪些线程正在运行SHOW VARIABLES -- /* 数据库操

MySQL学习笔记—SQL服务器模式汇总

MySQL学习笔记-SQL服务器模式汇总 MySQL服务器可以以不同的SQL模式来操作,并且可以为不同客户端应用不同模式.这样每个应用程序可以根据自己的需求来定制服务器的操作模式. 模式定义MySQL应支持哪些SQL语法,以及应执行哪种数据验证检查.这样可以更容易地在不同的环境中使用MySQL,并结合其它数据库服务器使用MySQL. 你可以用–sql-mode="modes"选项启动mysqld来设置默认SQL模式.如果你想要重设,该值还可以为空(–sql-mode ="&q

MySQL学习笔记之三 表类型

你能用的数据库引擎取决于MySQL在安装时候是如何被编译的.要添加一个新的引擎,就必须编译MySQL.仅仅为了添加一个特性而编译应用程序的想法对于Windows的开发人员来说可能有点小题大做,得不偿失,但是在Unix的世界里,这已经成为了标准.在缺省的情况下,MySQL支持三个引擎:ISAM.MyISAM和HEAP.另外两种类型InnoDB和Berkley(BDB),也常常可以使用. ISAM ISAM是一个定义明确且历经时间考验的数据表格管理方法,它在设计之初就考虑到数据库被查询的次数远远大于

Mysql学习笔记(十一)临时表+视图

原文:Mysql学习笔记(十一)临时表+视图 学习内容: 临时表和视图的基本操作... 临时表与视图的使用范围... 1.临时表   临时表:临时表,想必大家都知道这个概念的存在...但是我们什么时候应该使用到临时表呢?当一个数据库存在着大量的数据的时候,我们想要获取到这个数据集合的一个子集,那么我们就可以使用临时表来保存我们想要的数据..然后对临时表进行操作就可以了...使用临时表必然是有原因的..使用临时表会加快数据库的查询性能.... create temporary table tmp_

MySQL学习笔记—触发程序

MySQL学习笔记-触发程序 触发程序是与表有关的命名数据库对象,当表上发生特定事件时,将触发执行相应的触发程序. CREATE TRIGGER语法 CREATE TRIGGER语法: CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_stmt 触发程序是与表有关的命名数据库对象,当表上出现特定事件时,将激活该对象. 触发程序与命名为tbl_name的表相关.tbl_na