博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql互为主从的环境,更新一条语句同时提交,为什么会出现数据不一致?
阅读量:6897 次
发布时间:2019-06-27

本文共 3911 字,大约阅读时间需要 13 分钟。

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错误。

本文转自 Darren_Chen 51CTO博客,原文链接:http://blog.51cto.com/darrenmemos/1924073,如需转载请自行联系原作者

你可能感兴趣的文章
流域水文模拟
查看>>
Linux 中 SVN 重启关闭
查看>>
Vue系列:在vux的popup组件中使用百度地图遇到显示不全的问题
查看>>
音频特征提取——librosa工具包使用
查看>>
基于八叉树的区域增长点云分割算法【转】
查看>>
SERVER2012 FTP服务器和客户端配置
查看>>
C++11 带来的新特性 (3)—— 关键字noexcept
查看>>
《iBoard 是什么》之简介
查看>>
中间代码生成器-5-编译原理
查看>>
ORACLE 11G EXP导出空表方法
查看>>
公用代码实现两个表的拼接(部分代码)
查看>>
【液晶模块系列基础视频】4.4.X-GUI图形界面库-画tab函数简介
查看>>
147. Insertion Sort List
查看>>
Delphi中DLL的其他应用
查看>>
Node.js nvshens图片批量下载爬虫 1.00
查看>>
[转]Android中的一个TextView中的字体设置不同大小
查看>>
Linux系统搭建负载均衡环境
查看>>
mvn deploy命令上传包
查看>>
C# 中的多线程
查看>>
如何在Mac上放大
查看>>