mysql互为主从的环境为什么会出现数据不一致


本篇内容介绍了“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 t1G*************************** 1. row *************************** Table: t1Create 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=utf8root@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]>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 [testd开发云主机域名b]>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 t2G*************************** 1. row *************************** Table: t2Create Table: CREATE TABLE `t2` ( `c1` int(11) DEFAULT NULL, `c2` varchar(20) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8root@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]>select * from t2;+——+——+| c1 | c2 |+——+——+| 1 | aaa || 2 | b1 |+——+——+root@localhost [testdb]>select * from t2;+——+——+| c1 | c2 |+——+——+| 1 | aaa || 2 | b2 |+——+——+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 statusGLast_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 759root@localhost [testdb]>show slave statusGLast_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错误。“mysql互为主从的环境为什么会出现数据不一致”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注开发云网站,小编将为大家输出更多高质量的实用文章!

相关推荐: mysql中如何查看表是否被锁

本文主要给大家简单讲讲mysql中如何查看表是否被锁,相关专业术语大家可以上网查查或者找一些相关书籍补充一下,这里就不涉猎了,我们就直奔主题吧,希望mysql中如何查看表是否被锁这篇文章可以给大家带来一些实际帮助。查看表是否被锁:直接在mysql命令行执行:s…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

(0)
打赏 微信扫一扫 微信扫一扫
上一篇 06/21 17:15
下一篇 06/21 17:44

相关推荐