mysql互为主从的环境,更新一条语句同时提交,为什么会出现数据不一致?
2017-05-10 13:22
429 查看
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
总结: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
总结:update一条记录同时提交,有没有主键的情况下,sql_thread是根据全表扫描匹配行记录,所以m1更新在m2中找不到需要更新的行,报1032错误,m2更新在m1中找不到需要更新的行,也报1032错误。
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 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例
- Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例
- Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例
- Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例
- 用一条SQL语句,同时更新两个表中的数据
- Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例
- 用一条SQL语句将数据表中某列更新到另一个数据表里
- 用一条SQL语句将数据表中某列更新到另一个数据表里
- mysql主从数据不一致的解决方法(转)
- MySql中使用INSERT INTO语句更新多条数据的例子
- Oracle更新一条数据然后提交的整个经过(包括undo,redo,后台进程)
- MYSQL 数据表中行存在时更新,不存在时插入的SQL语句
- MEMCACHED在集群环境下对并发更新是否保持数据一致
- MySQL IFNULL中可以有select语句;查询一个表中的数据,同时查看另一个表中是否有符合条件的额数据
- mysql更新语句执行的时候,如果没有数据不会报错
- MySql中使用INSERT INTO语句更新多条数据的例子
- mysql 常用命令,连接数据库,查看建表语句,批量导入数据,批量更新数据,连接查询
- 用一条SQL语句根据条件将数据表中某几列更新到另一个数据表对应有列
- MYSQL 多个DML语句同时提交
- MySq之一条mysql语句来更新(插入)查询结果