曾多次听到“MySQL为什么选择RR为默认断绝级别”的问题,其实这是个汗青遗留问题,当前以及办理,可是MySQL的各个版本沿用了原有习惯。汗青版本中的问题是什么,本次就通过简朴的测试来说明一下。
1、 筹备事情 1.1 陈设主从陈设一套主从架构的集群,建设进程较简朴,可以参考汗青文章陈设 MySQL主从复制搭建 陈设一主一从即可。
1.2 建设测试表及数据在主库中建设表及测试数据
mysql> create table users(id int primary key auto_increment,user_name varchar(20),c_id tinyint(4),c_note varchar(50),key c_id(c_id)) engine=innodb; Query OK, 0 rows affected (0.01 sec) mysql> insert into users values(1,'刘备',2,null),(2,'曹操',1,null),(3,'孙权',3,null),(4,'关羽',2,null),(5,'司马懿',1,null); Query OK, 5 rows affected (0.00 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> create table class(c_id int primary key ,c_name varchar(1),c_note varchar(50)) engine=innodb; Query OK, 0 rows affected (0.00 sec) mysql> insert into class values(1,'魏',null),(2,'蜀',null),(3,'吴',null),(4,'晋',''); Query OK, 4 rows affected (0.00 sec) Records: 4 Duplicates: 0 Warnings: 0
2、 RR断绝级别MySQL默认的断绝级别为 RR(Repeatable Read),在此断绝级别下,比拟binlog名目为ROW、STATEMENT是否会造成主从数据纷歧致
2.1 ROW名目其实不消测试各人也应该对RR级别下ROW名目标binlog有信心,可是,万事皆需实践检讨。
步调说明如下:
步调1 - 别离查察两个会话中的事务断绝级别及binlog名目(断绝级别均为RR,binlog为ROW名目)
步调2 - SESSION A 开启事务,更新users 表中c_id字段存在于class表中的记录,功效为5笔记录均更新,并将c_note内容更新为 t1
步调3- SESSION B 开启事务,筹备删除class表中 c_id便是2的记录,此时无法更新,处于阻塞状态,因为在RR级别下需要担保反复读。到达所期待超时时间后将会报错。
步调4- SESSION A 提交事务(此步调也可以在步调3时操纵,功效纷歧样,后续步调中将回收此方法)
步调5- SESSION B 重启事务,再次删除class表中 c_id便是2的记录,此时提交可以乐成了,乐成删除了一笔记录
步调6- SESSION A 开启事务,更新users 表中c_id字段存在于class表中的记录,功效为3笔记录更新乐成,并将c_note内容更新为 t2,有2笔记录因为c_id不存在与class表中,因此不会更新
步调7- 别离在SESSON A和SESSION B查察users表中的内容,功效一致
步调8- 在从库查察users表中的内容,数据与主库一致
详细步调如下:
步调 SESSION ASESSION B
1
mysql>show variables like '%iso%';
+-----------------------+-----------------+
| Variable_name | Value |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
| tx_isolation | REPEATABLE-READ |
+-----------------------+-----------------+
2 rows in set (0.00 sec)
mysql>show variables like '%binlog_format%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW |
+---------------+-------+
1 row in set (0.00 sec)
mysql>show variables like '%iso%';
+-----------------------+-----------------+
| Variable_name | Value |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
| tx_isolation | REPEATABLE-READ |
+-----------------------+-----------------+
2 rows in set (0.00 sec)
mysql>show variables like '%binlog_format%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW |
+---------------+-------+
1 row in set (0.01 sec)
2
mysql>set autocommit=0;
mysql>update users set c_note='t1' where c_id in (select c_id from class);
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5 Changed: 5 Warnings: 0
3
mysql>set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql>delete from class where c_id=2;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
4
mysql>commit;
Query OK, 0 rows affected (0.00 sec)
5
mysql>set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql>delete from class where c_id=2;
Query OK, 1 row affected (0.00 sec)
mysql>commit;
Query OK, 0 rows affected (0.00 sec)
6
mysql>update users set c_note='t2' where c_id in (select c_id from class);
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql>commit;
Query OK, 0 rows affected (0.00 sec)
7
mysql>select * from users;
+----+-----------+------+--------+
| id | user_name | c_id | c_note |
+----+-----------+------+--------+
| 1 | 刘备 | 2 | t1 |
| 2 | 曹操 | 1 | t2 |
| 3 | 孙权 | 3 | t2 |
| 4 | 关羽 | 2 | t1 |
| 5 | 司马懿 | 1 | t2 |
+----+-----------+------+--------+
5 rows in set (0.00 sec)
mysql>select * from users;
+----+-----------+------+--------+
| id | user_name | c_id | c_note |
+----+-----------+------+--------+
| 1 | 刘备 | 2 | t1 |
| 2 | 曹操 | 1 | t2 |
| 3 | 孙权 | 3 | t2 |
| 4 | 关羽 | 2 | t1 |
| 5 | 司马懿 | 1 | t2 |
+----+-----------+------+--------+
5 rows in set (0.00 sec)
8
在从库查察数据
root@testdb:3307 12:02:20>select * from users;
+----+-----------+------+--------+
| id | user_name | c_id | c_note |
+----+-----------+------+--------+
| 1 | 刘备 | 2 | t1 |
| 2 | 曹操 | 1 | t2 |
| 3 | 孙权 | 3 | t2 |
| 4 | 关羽 | 2 | t1 |
| 5 | 司马懿 | 1 | t2 |
+----+-----------+------+--------+
5 rows in set (0.00 sec)
2.2 STATEMENT名目
为了和之前的步调一致,先初始化数据