mysql互为主从的环境,更新一条语句同时提交,为什么会出现数据不一致?

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: mysql互为主从的环境,更新一条语句同时提交,为什么会出现数据不一致?m1:begin;update t1 set c2='b1' where c1=2;commit;m2:begin;update t1 set c2='b2' where c1=2;commit;m1和m2同时提交,复制不会报错,但是m1和m2的数据不一致,为什么?因为sql_thread线程根据主键更新数据,不会校验行数据如何避免这种问题:只在单节点进行写入,如 keepalived+双主,MGR,PXC如果多节点写入都有这种问题发生。

mysql互为主从的环境,更新一条语句同时提交,为什么会出现数据不一致?


m1:


begin;

update t1 set c2='b1' where c1=2;

commit;


m2:


begin;

update t1 set c2='b2' where c1=2;

commit;


m1和m2同时提交,复制不会报错,但是m1和m2的数据不一致,为什么?

因为sql_thread线程根据主键更新数据,不会校验行数据


如何避免这种问题:

只在单节点进行写入,如 keepalived+双主,MGR,PXC如果多节点写入都有这种问题发生。


例1:

表有主键和自增的情况:

root@localhost [testdb]>show create table t1\G

*************************** 1. row ***************************

       Table: t1

Create Table: CREATE TABLE `t1` (

  `c1` int(11) NOT NULL AUTO_INCREMENT,

  `c2` varchar(10) DEFAULT NULL,

  PRIMARY KEY (`c1`)

) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8


m1: m2:

root@localhost [testdb]>select * from t1;

+----+------+

| c1 | c2   |

+----+------+

|  1 | aaa  |

|  2 | bbb  |

|  3 | ccc  |

|  4 | ccc  |

|  6 | ddd  |

|  8 | eee  |

+----+------+

root@localhost [testdb]>select * from t1;

+----+------+

| c1 | c2   |

+----+------+

|  1 | aaa  |

|  2 | bbb  |

|  3 | ccc  |

|  4 | ccc  |

|  6 | ddd  |

|  8 | eee  |

+----+------+

root@localhost [testdb]>begin; root@localhost [testdb]>begin;
root@localhost [testdb]>update t1 set c2='b1' where c1=2; root@localhost [testdb]>update t1 set c2='b2' where c1=2;

root@localhost [testdb]>select * from t1;

+----+------+

| c1 | c2   |

+----+------+

|  1 | aaa  |

|  2 | b1   |

|  3 | ccc  |

|  4 | ccc  |

|  6 | ddd  |

|  8 | eee  |

+----+------+

root@localhost [testdb]>select * from t1;

+----+------+

| c1 | c2   |

+----+------+

|  1 | aaa  |

|  2 | b2   |

|  3 | ccc  |

|  4 | ccc  |

|  6 | ddd  |

|  8 | eee  |

+----+------+

root@localhost [testdb]>commit; root@localhost [testdb]>commit;

root@localhost [testdb]>select * from t1;

+----+------+

| c1 | c2   |

+----+------+

|  1 | aaa  |

|  2 | b2   |

|  3 | ccc  |

|  4 | ccc  |

|  6 | ddd  |

|  8 | eee  |

+----+------+

root@localhost [testdb]>select * from t1;

+----+------+

| c1 | c2   |

+----+------+

|  1 | aaa  |

|  2 | b1   |

|  3 | ccc  |

|  4 | ccc  |

|  6 | ddd  |

|  8 | eee  |

+----+------+

总结:update一条记录同时提交,有主键的情况下,sql_thread是根据主键匹配行记录,不会校验行数据,所以m1更新了m2中表的记录,m2更新了m1中表的记录。


例2:有没有主键同时更新一行数据的情况:


root@localhost [testdb]>show create table t2\G

*************************** 1. row ***************************

       Table: t2

Create Table: CREATE TABLE `t2` (

  `c1` int(11) DEFAULT NULL,

  `c2` varchar(20) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8


m1 m2

root@localhost [testdb]>select * from t2;

+------+------+

| c1   | c2   |

+------+------+

|    1 | aaa  |

|    2 | bbb  |

+------+------+

root@localhost [testdb]>select * from t2;

+------+------+

| c1   | c2   |

+------+------+

|    1 | aaa  |

|    2 | bbb  |

+------+------+

root@localhost [testdb]>begin; root@localhost [testdb]>begin;
root@localhost [testdb]>update t2 set c2='b1' where c1=2; root@localhost [testdb]>update t2 set c2='b2' where c1=2;

root@localhost [testdb]>select * from t2;

+------+------+

| c1   | c2   |

+------+------+

|    1 | aaa  |

|    2 | b1   |

+------+------+

root@localhost [testdb]>select * from t2;

+------+------+

| c1   | c2   |

+------+------+

|    1 | aaa  |

|    2 | b2   |

+------+------+

root@localhost [testdb]>commit; root@localhost [testdb]>commit;

root@localhost [testdb]>select * from t2;

+------+------+

| c1   | c2   |

+------+------+

|    1 | aaa  |

|    2 | b1   |

+------+------+

root@localhost [testdb]>select * from t2;

+------+------+

| c1   | c2   |

+------+------+

|    1 | aaa  |

|    2 | b2   |

+------+------+

root@localhost [testdb]>show slave status\G

Last_Errno: 1032

                   Last_Error: Could not execute Update_rows event on table testdb.t2; Can't find record in 't2', Error_code: 1032; handler error HA_ERR_END_OF_FILE; the event's master log mysql-bin.000013, end_log_pos 759

root@localhost [testdb]>show slave status\G 

Last_Errno: 1032

                   Last_Error: Could not execute Update_rows event on table testdb.t2; Can't find record in 't2', Error_code: 1032; handler error HA_ERR_END_OF_FILE; the event's master log mysql-bin.000026, end_log_pos 3064

总结:update一条记录同时提交,有没有主键的情况下,sql_thread是根据全表扫描匹配行记录,所以m1更新在m2中找不到需要更新的行,报1032错误,m2更新在m1中找不到需要更新的行,也报1032错误。


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
关系型数据库 MySQL 数据安全/隐私保护
mysql 5.7安装详细步骤(图片+文字,图片为主)【软件安装+环境配置】
mysql 5.7安装详细步骤(图片+文字,图片为主)【软件安装+环境配置】
120 0
mysql 5.7安装详细步骤(图片+文字,图片为主)【软件安装+环境配置】
|
关系型数据库 MySQL Linux
CentOS7.X搭建MySQL集群(互为主从)
CentOS7.X搭建MySQL集群(互为主从)
3264 0
|
MySQL 关系型数据库 数据库
|
监控 Oracle 关系型数据库
|
关系型数据库 MySQL 应用服务中间件
|
8天前
|
关系型数据库 MySQL 数据库
mysql卸载、下载、安装(window版本)
mysql卸载、下载、安装(window版本)
|
1月前
|
关系型数据库 MySQL 数据库连接
关于MySQL-ODBC的zip包安装方法
关于MySQL-ODBC的zip包安装方法

推荐镜像

更多