mysql互为主从的环境为什么会出现数据不一致
admin
2023-05-19 08:42:57
0

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

m1m2

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

相关内容

热门资讯

马杜罗最新发声 新华社北京8月3日电 遭美国强行控制的委内瑞拉总统尼古拉斯·马杜罗2日借助社交媒体发声,对委内瑞拉政...
时间自有答案!通派龙湖中原颂提... 历经匠心营造,2026年8月1日至3日,通派龙湖中原颂正式集中交付。这不仅是项目与业主之间的一次履约...
液晶电视一边黑屏一边图像正常什... 1、电视显示屏的数据线存在接触不良,需要重新连接或焊接。2、电视数字机顶盒故障,维修或更换。3、视频...
液晶电视进水了黑屏了怎么办 千万不要通电,可以先打开电视机的后板,用吹风机先将内部的水渍吹干。如果一不小心通了电就要先断电,然后...
维修洗衣机的常见故障 洗衣机在日常生活中是一个经常使用的电器,然而在经常使用的过程中,也会出现各种故障,下面将介绍一些经常...
宗馥莉的“同父异母兄弟”成立新... 据天眼查显示,7月31日,杭州天杞优品食品有限公司成立,注册资本为300万元,经营范围包含互联网销售...
智能马桶常见故障与维修 喷水故障。马桶盖不喷水,需检查水管是否坏了,如水管有问题则需去维修点维修;烘干故障。如果清洗和照明都...
燃气热水器主板坏了还有必要修吗 1、热水器的主板坏了是可以修的,如果不是特别严重的情况,修起来也比较容易,大概需要半个小时左右就可以...
Meta与贝莱德联手在德州埃尔... Meta Platforms今日宣布计划在德克萨斯州埃尔帕索建设一座人工智能数据中心园区,此次项目与...
lora网关lora网关制造厂... 在物联网项目的实施过程中,lora网关lora网关制造厂所对应的产品选型往往受到关注。围绕lora网...