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 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| +----+------+ |
例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 |
【mysql互为主从的环境,更新一条语句同时提交,为什么会出现数据不一致()】转载于:https://blog.51cto.com/darrenmemos/1924073
推荐阅读
- Docker应用:容器间通信与Mariadb数据库主从复制
- py连接mysql
- 2019-01-18Mysql中主机名的问题
- MySql数据库备份与恢复
- mysql|InnoDB数据页结构
- mysql中视图事务索引与权限管理
- MYSQL主从同步的实现
- MySQL数据库的基本操作
- javaweb|基于Servlet+jsp+mysql开发javaWeb学生成绩管理系统
- Python3|Python3 MySQL 数据库连接