MySQL 主从复制异常问题分析(Error_code 1317)

2023年 12月 28日 94.2k 0

由于个人能力有限,文中可能存在错误,并且很多细节没有深入分析,欢迎批评指正。

Table of Contents

  • 问题描述
  • 问题分析
  • 场景模拟
  • 测试总结
  • 问题处理
  • 使用建议
  • 遗留问题

问题描述

某日,接收到生产环境从库复制异常告警信息,数据库版本为 MySQL 5.5.18

登陆数据库,查看复制信息如下(截取部分):

...
Relay_Master_Log_File: mysql-bin.000323
Slave_IO_Running: Yes
Slave_SQL_Running:NO
...
Last_Errno: 1317
Last_Error: Query partially completed on the master (error on master: 1317) and was aborted. There is a chance that your master is inconsistent at this point. If you are sure that your master is ok, run this query manually on the slave and then restart the slave with SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE; . Query: 'update robot_heart_record set status_new='0',create_time=now() where robot_id='760b6ba795ef4b28b4a054136e69d510''
...
Relay_Master_Log_Pos: 647994218
...

检查数据库错误日志,错误信息如下:

231226 9:25:22 [ERROR] Slave SQL: Query partially completed on the master (error on master: 1317) and was aborted. There is a chance that your master is inconsistent at this point. If you are sure that your master is ok, run this query manually on the slave and then restart the slave with SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE; . Query: 'update robot_heart_record set status_new='0',create_time=now() where robot_id='760b6ba795ef4b28b4a054136e69d510'',Error_code: 1317
231226 9:25:22 [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.000323' position 647994218

由上述信息,可以看出从库在回放主库日志到 position 647994218 后,执行中断,错误代码为 1317。同时根据报错信息 Query partially completed on the master (error on master: 1317) and was aborted. 提示,主库执行时中断,但部分执行完成,这里需注意,该现象并不符合事务的原子性。

通过排查发现,robot_heart_record 表存储引擎为 MyISAM,该引擎并不支持事务。

简化表结构如下:

create table `t1` (
`id` int(11) not null auto_increment,
`uid` int(11) default null,
`status` varchar(20) default null,
`create_time` datetime default null,
primary key (`id`),
key `uid` (`uid`)
) engine=MyISAM default

另,通过对主库 binlog 日志解析发现,数据库 binlog_format 设置的是 mixed 模式(这里需要注意,在后续模拟中,不同模式下主从复制表现也存在差异)。

问题分析

1.MyISAM 引擎不支持事务,执行中断后,部分操作仍能执行成功;

2.binlog_format 模式为 mixed 时,实际上是 statement 和 row 模式的混合,针对某些操作,只记录执行的 sql (比如复制报错信息中的 update 语句);

3.通过场景模拟,检查不同存储引擎和日志模式下的执行差异。

场景模拟

1.创建测试表 t1、t2,其中 t1 为 MyISAM 表,t2 为 INNODB 表;

# t1 , MyISAM 表
drop table if exists t1;
create table `t1` (
`id` int(11) not null auto_increment,
`uid` int(11) default null,
`status` varchar(20) default null,
`create_time` datetime default null,
primary key (`id`),
key `uid` (`uid`)
) engine=MyISAM default charset=utf8;

# t2 , INNODB 表
drop table if exists t2;
create table `t2` (
`id` int(11) not null auto_increment,
`uid` int(11) default null,
`status` varchar(20) default null,
`create_time` datetime default null,
primary key (`id`),
key `uid` (`uid`)
) engine=innodb default charset=utf8;

2.写入测试数据,这里通过存储过程分别写入;

# t1 表写入数据
drop procedure if exists idata;

delimiter ;;
create procedure idata()
begin
declare i int;
set i=1;
while (i

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论