实验:基于语句复制的不安全性《MySQL排错指南Page83-85》

本文通过实例演示了MySQL主从复制环境下,当binlog_format设置为statement且隔离级别为RC时,特定SQL语句(如使用LIMIT)可能导致主从数据不一致的现象及原因。

环境:

 主、从库的binlog_format = statement

  隔离级别: RC


主库会话1主库会话2主库会话3从库会话


use test;

create table f1(char(2))

engine=InnoDB;


begin;


insert into t1 select 1;

insert into t1 select 2;

insert into t1 select 3;

insert into t1 select 4;

insert into t1 select 5;





begin;


insert into t1 select 'a';

insert into t1 select 'b';

insert into t1 select 'c';

insert into t1 select 'd';

insert into t1 select 'e';

insert into t1 select 'f';


commit;





select * from t1;

+------+

| f1   |

+------+

| a    |

| b    |

| c    |

| d    |

| e    |

| f    |

+------+

spacer.gif

select * from t1;

+------+

| f1   |

+------+

| a    |

| b    |

| c    |

| d    |

| e    |

| f    |

+------+

commit




select * from t1;

+------+

| f1   |

+------+

| 1    |

| 2    |

| 3    |

| 4    |

| 5    |

| a    |

| b    |

| c    |

| d    |

| e    |

| f    |

+------+



spacer.gif

select * from t1;

+------+

| f1   |

+------+

| a    |

| b    |

| c    |

| d    |

| e    |

| f    |

| 1    |

| 2    |

| 3    |

| 4    |

| 5    |

+------+


可以看到上图中,最后一行,主从库的数据排列顺序变化了。如果这时候我们在主库再执行一个类似 update t1 set f1='A' limit 4; 这种操作,则从库的数据彻底乱了。


下面是执行update时候 主库提示的warnings告警。

master [localhost] {root} (test) > master [localhost] {root} (test) > show warnings \G

*************************** 1. row ***************************

  Level: Note

   Code: 1592

Message: Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. The statement is unsafe because it uses a LIMIT clause. This is unsafe because the set of rows included cannot be predicted.

1 row in set (0.00 sec)











本文转自 lirulei90 51CTO博客,原文链接:http://blog.51cto.com/lee90/1977109,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值