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

相关内容

热门资讯

湖北恩施旅游客车侧翻致1死3伤 记者从湖北省相关部门获悉,5月17日14时56分许,一辆旅游客车在湖北省恩施市境内,发生侧翻事故。事...
因乘客咬伤空乘人员,澳洲航空一... 【环球网报道】据英国《卫报》、法新社等媒体报道,因一名男性乘客涉嫌咬伤空乘人员,澳大利亚澳洲航空公司...
记者手记:从“聪明的路”到“智... 新华社北京5月16日电 记者手记:从“聪明的路”到“智慧的海”,触摸未来交通科技脉搏 新华社记者王聿...
没有明星大导的潮汕方言电影凭什... 五一档最大黑马《给阿嬷的情书》正在持续热映。这部全素人阵容、成本仅1400万的地方电影,像木棉花一样...
80后地产女王自杀,倒在楼市黎... 楼市回暖之际,房地产界却传出了一个噩耗:5月12日,香港乐风集团创始人周佩贤在住所内自杀身亡,年仅4...
苏州能模维申请服务器液冷散热器... 国家知识产权局信息显示,苏州能模维系统有限公司申请一项名为“一种服务器液冷散热器”的专利,公开号CN...
华为申请通信方法、设备和可读存... 国家知识产权局信息显示,华为技术有限公司申请一项名为“通信方法、设备和可读存储介质”的专利,公开号C...
批赖清德当局误判“台美关系”,... 海峡导报综合报道 美国总统特朗普结束访华行程后,外媒陆续报道其涉台相关说法。中国国民党17日批评民进...
家用冰箱温度调多少度合适 冰箱温度调节根据季节不同、需求不同来设定。通常来说夏季室外温度较高,控温可以调节在3-4档位,同时温...
智能冰箱温度调到多少合适 一般的食物适合的温度,新鲜食品区域推荐的温度是2.7-4.4℃,冷冻室的温度是-15℃。如果有单独的...