再将binlog日志格式改为STATAMENT格式(全局及会话级都改一下,或者修改全局变量后重新登录也行,当然 只改会话级别的也可以测试),然后 再次进行测试。
步骤说明如下:
步骤1 - 分别查看两个会话中的事务隔离级别及binlog格式(隔离级别均为RR,binlog为STATENENT格式)
步骤2 - SESSION A 开启事务,更新users 表中c_id字段存在于class表中的记录,结果为5条记录均更新,并将c_note内容更新为 t1
步骤3- SESSION B 开启事务,准备删除class表中 c_id等于2的记录,此时无法更新,处于阻塞状态,立即进行步骤4
步骤4- SESSION A 在SESSION B执行commit的动作,则SESSION B的删除操作可以执行通过,但注意class表的数据两个SESSION中查看到的是不一样的
步骤5- 此时SESSION B执行commit,否则后面session A 更新数据时也会阻塞。此时如果SESSION A不执行commit,查看class表的结果也是不一样的,如步骤中的情况
步骤6- SESSION A 开启事务,更新users 表中c_id字段存在于class表中的记录,结果为3条记录更新成功,并将c_note内容更新为 t2,另外2条记录虽然本此时查看class表中存在对应的c_id,但是不会更新,此时提交事务,然后再次查看class的内容,结果和SESSION B 查看的结果一致了(幻读)
步骤7- 在从库查看users、class表中的内容,数据与主库一致
步 骤 SESSION A SESSION B1
mysql>show variables like '%iso%';
+-----------------------+-----------------+
| Variable_name | Value |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
| tx_isolation | REPEATABLE-READ |
+-----------------------+-----------------+
2 rows in set (0.01 sec)
mysql>show variables like '%binlog_format%';
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.01 sec)
mysql>show variables like '%iso%';
+-----------------------+-----------------+
| Variable_name | Value |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
| tx_isolation | REPEATABLE-READ |
+-----------------------+-----------------+
2 rows in set (0.01 sec)
mysql>show variables like '%binlog_format%';
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.01 sec)
2
root@testdb:3306 12:37:04>set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
root@testdb:3306 12:37:17>update users set c_note='t1' where c_id in (select c_id from class);
Query OK, 5 rows affected, 1 warning (0.00 sec)
Rows matched: 5 Changed: 5 Warnings: 1
3
root@testdb:3306 12:28:25>set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
root@testdb:3306 12:38:06>delete from class where c_id=2;
Query OK, 1 row affected (4.74 sec)
4
root@testdb:3306 12:38:09>commit;
Query OK, 0 rows affected (0.00 sec)
root@testdb:3306 12:38:13>select * from users;
+----+-----------+------+--------+
| id | user_name | c_id | c_note |
+----+-----------+------+--------+
| 1 | 刘备 | 2 | t1 |
| 2 | 曹操 | 1 | t1 |
| 3 | 孙 权 | 3 | t1 |
| 4 | 关羽 | 2 | t1 |
| 5 | 司马懿 | 1 | t1 |
+----+-----------+------+--------+
5 rows in set (0.00 sec)
root@testdb:3306 12:39:07>select * from class;
+------+--------+--------+
| c_id | c_name | c_note |
+------+--------+--------+
| 1 | 魏 | NULL |
| 2 | 蜀 | NULL |
| 3 | 吴 | NULL |
| 4 | 晋 | |
+------+--------+--------+
4 rows in set (0.00 sec)
5
root@testdb:3306 12:38:13>commit;
Query OK, 0 rows affected (0.00 sec)
root@testdb:3306 12:39:56>select * from class ;
+------+--------+--------+
| c_id | c_name | c_note |
+------+--------+--------+
| 1 | 魏 | NULL |
| 3 | 吴 | NULL |
| 4 | 晋 | |
+------+--------+--------+
3 rows in set (0.00 sec)
6
root@testdb:3306 12:52:23>update users set c_note='t2' where c_id in (select c_id from class);
Query OK, 3 rows affected, 1 warning (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 1
root@testdb:3306 12:52:45>select * from class;
+------+--------+--------+
| c_id | c_name | c_note |
+------+--------+--------+
| 1 | 魏 | NULL |
| 2 | 蜀 | NULL |
| 3 | 吴 | NULL |
| 4 | 晋 | |
+------+--------+--------+
4 rows in set (0.00 sec)
root@testdb:3306 12:52:49>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.01 sec)
root@testdb:3306 12:53:03>commit;
Query OK, 0 rows affected (0.00 sec)
root@testdb:3306 12:53:06>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)
root@testdb:3306 12:53:11>select * from class;
+------+--------+--------+
| c_id | c_name | c_note |
+------+--------+--------+
| 1 | 魏 | NULL |
| 3 | 吴 | NULL |
| 4 | 晋 | |
+------+--------+--------+
3 rows in set (0.00 sec)
7
查看从库数据
root@testdb:3307 12:44:22>select * from class;
+------+--------+--------+
| c_id | c_name | c_note |
+------+--------+--------+
| 1 | 魏 | NULL |
| 3 | 吴 | NULL |
| 4 | 晋 | |
+------+--------+--------+
3 rows in set (0.01 sec)
root@testdb:3307 12:57:07>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)