查看“命令行验证MySQL的隔离级别”的源代码
←
命令行验证MySQL的隔离级别
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
https://www.bilibili.com/video/BV1eJ411c7rf?p=40 === 准备 === #Windows 下 cmd 进入命令行:<syntaxhighlight lang="powershell"> C:\Users\Administrator>mysql -uroot -p123456 </syntaxhighlight> #创建一个新用户登录MySQL Server:<syntaxhighlight lang="powershell"> mysql> create user tom identified by 'abc123'; Query OK, 0 rows affected (0.03 sec) </syntaxhighlight>用户名:tom 密码:abc123 #登录tom:<syntaxhighlight lang="powershell"> C:\Users\Administrator>mysql -utom -pabc123 </syntaxhighlight> #查看 root 用户可以操作的数据库:<syntaxhighlight lang="powershell"> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db2019 | | guns | | jdbc_test | | ke | | m3u8_download | | mysql | | nacos_config | | performance_schema | | sakila | | seata | | seata_account | | seata_order | | seata_storage | | sql | | sys | | world | +--------------------+ 17 rows in set (0.00 sec) </syntaxhighlight> #查看 tom 用户可以操作的数据库:<syntaxhighlight lang="powershell"> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | +--------------------+ 1 row in set (0.00 sec) </syntaxhighlight> #为 tom 用户授权:<syntaxhighlight lang="powershell"> mysql> grant select,insert,update,delete on jdbc_test.* to tom@localhost identified by 'abc123'; Query OK, 0 rows affected, 1 warning (0.00 sec) </syntaxhighlight> #验证 tom 用户得到的授权:<syntaxhighlight lang="powershell"> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | jdbc_test | +--------------------+ 2 rows in set (0.00 sec) </syntaxhighlight> === 验证事务隔离级别为 REPEATABLE-READ 时的情况 === ==== 查看初始数据 ==== <syntaxhighlight lang="powershell"> mysql> select * from user_table; +------+----------+---------+ | user | password | balance | +------+----------+---------+ | AA | 123456 | 1000 | | BB | 654321 | 1000 | +------+----------+---------+ 2 rows in set (0.00 sec) </syntaxhighlight> ==== 设置事务不自动提交 ==== tom 和 root 都设置<syntaxhighlight lang="powershell"> mysql> set autocommit =false; Query OK, 0 rows affected (0.00 sec) </syntaxhighlight> ==== 查看 tom 用户当前的事务隔离级别 ==== <syntaxhighlight lang="powershell"> mysql> select @@tx_isolation; +-----------------+ | @@tx_isolation | +-----------------+ | REPEATABLE-READ | +-----------------+ 1 row in set, 1 warning (0.00 sec) </syntaxhighlight> ==== root 用户查看当前数据 ==== <syntaxhighlight lang="powershell"> mysql> select * from user_table where user = 'AA'; +------+----------+---------+ | user | password | balance | +------+----------+---------+ | AA | 123456 | 1000 | +------+----------+---------+ 1 row in set (0.00 sec) </syntaxhighlight> ==== tom 用户查看当前数据 ==== <syntaxhighlight lang="powershell"> mysql> select * from user_table where user = 'AA'; +------+----------+---------+ | user | password | balance | +------+----------+---------+ | AA | 123456 | 1000 | +------+----------+---------+ 1 row in set (0.00 sec) </syntaxhighlight> ==== tom 用户修改数据 ==== <syntaxhighlight lang="powershell"> mysql> update user_table set balance = 3000 where user = 'AA'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 </syntaxhighlight> ==== tom 用户查看修改后的数据 ==== <syntaxhighlight lang="powershell"> mysql> select * from user_table where user = 'AA'; +------+----------+---------+ | user | password | balance | +------+----------+---------+ | AA | 123456 | 3000 | +------+----------+---------+ 1 row in set (0.00 sec) </syntaxhighlight> ==== root 用户查看数据 ==== <syntaxhighlight lang="powershell"> mysql> select * from user_table where user = 'AA'; +------+----------+---------+ | user | password | balance | +------+----------+---------+ | AA | 123456 | 1000 | +------+----------+---------+ 1 row in set (0.00 sec) </syntaxhighlight> ==== tom 用户提交事务 ==== <syntaxhighlight lang="powershell"> mysql> commit; Query OK, 0 rows affected (0.00 sec) </syntaxhighlight> ==== root 用户再次查看数据 ==== <syntaxhighlight lang="powershell"> mysql> select * from user_table where user = 'AA'; +------+----------+---------+ | user | password | balance | +------+----------+---------+ | AA | 123456 | 1000 | +------+----------+---------+ 1 row in set (0.00 sec) </syntaxhighlight>balance 的值仍然是 1000,因为 默认事务的隔离级别是 REPEATABLE-READ(可重复读), 所以用户 root 在 <u>用户 tom 更新 user='AA' 的用户 balance 为 3000 之后且提交事务之前</u> 读取的 balance 值为 1000;(屏蔽了'''<big>脏读</big>'''的情况,不会读取到用户 tom 未提交事务变更的数据) 所以用户 root 在 <u>用户 tom 更新 user='AA' 的用户 balance 为 3000 且提交事务之后</u>,在用户 root 自己的当前事务中读取到的 balance 值仍为 1000;(屏蔽了'''<big>不可重复读</big>'''的情况,在用户 root 自己的当前事务 commit 或 rollback 之前可以重复读取同一条数据,得到的值始终是一致的)。 ==== root 用户提交事务后查看数据 ==== <syntaxhighlight lang="powershell"> mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql> select * from user_table where user = 'AA'; +------+----------+---------+ | user | password | balance | +------+----------+---------+ | AA | 123456 | 3000 | +------+----------+---------+ 1 row in set (0.00 sec) </syntaxhighlight>现在看到了 tom 用户提交更新的数据。 === 验证事务隔离级别为 READ-COMMITTED 时的情况 === ==== root 用户修改全局事务隔离级别 ==== <syntaxhighlight lang="powershell"> mysql> set global transaction isolation level read committed; Query OK, 0 rows affected (0.00 sec) mysql> select @@tx_isolation; +-----------------+ | @@tx_isolation | +-----------------+ | REPEATABLE-READ | +-----------------+ 1 row in set, 1 warning (0.00 sec) </syntaxhighlight>注意:现在修改完全局的事务隔离级别之后查询事务隔离级别仍然显示为“REPEATABLE-READ”,这是因为需要把当前连接断开重连之后才会生效。<syntaxhighlight lang="powershell"> mysql> exit Bye C:\Users\Administrator>mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 114 Server version: 5.7.35-log MySQL Community Server (GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select @@tx_isolation; +----------------+ | @@tx_isolation | +----------------+ | READ-COMMITTED | +----------------+ 1 row in set, 1 warning (0.00 sec) </syntaxhighlight>其他连接也是,也需要断开连接重连后,才能应用新的全局事务隔离级别<syntaxhighlight lang="powershell"> mysql> exit Bye C:\Users\Administrator>mysql -utom -pabc123 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 115 Server version: 5.7.35-log MySQL Community Server (GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select @@tx_isolation; +----------------+ | @@tx_isolation | +----------------+ | READ-COMMITTED | +----------------+ 1 row in set, 1 warning (0.00 sec) </syntaxhighlight> ==== 重新设置 root 和 tom 禁用自动提交 ==== 因为断开重连 MySQL Server,所以需要再次设置禁用自动提交<syntaxhighlight lang="powershell"> mysql> show variables like 'autocommit'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | autocommit | ON | +---------------+-------+ 1 row in set, 1 warning (0.00 sec) mysql> set autocommit=false; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'autocommit'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | autocommit | OFF | +---------------+-------+ 1 row in set, 1 warning (0.00 sec) </syntaxhighlight> ==== 在 tom 的事务中更新数据 ==== <syntaxhighlight lang="powershell"> mysql> update user_table set balance = 4000 where user = 'AA'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from user_table where user = 'AA'; +------+----------+---------+ | user | password | balance | +------+----------+---------+ | AA | 123456 | 4000 | +------+----------+---------+ 1 row in set (0.00 sec) </syntaxhighlight> ==== 在 root 的事务中查询数据 ==== <syntaxhighlight lang="powershell"> mysql> select * from user_table where user = 'AA'; +------+----------+---------+ | user | password | balance | +------+----------+---------+ | AA | 123456 | 3000 | +------+----------+---------+ 1 row in set (0.00 sec) </syntaxhighlight> ==== 提交 tom 的事务 ==== <syntaxhighlight lang="powershell"> mysql> commit; Query OK, 0 rows affected (0.01 sec) </syntaxhighlight> ==== 再在 root 的事务中查询数据 ==== <syntaxhighlight lang="powershell"> mysql> select * from user_table where user = 'AA'; +------+----------+---------+ | user | password | balance | +------+----------+---------+ | AA | 123456 | 4000 | +------+----------+---------+ 1 row in set (0.00 sec) </syntaxhighlight>因为当前 root 的事务隔离级别是 READ-COMMITTED, 所以在 root 的事务 commit 或 rollback 之前读取不到 tom 的事务中更新了、但未执行事务提交的数据;(屏蔽了'''<big>脏读</big>''') 但在 tom 的事务提交之后,在 root 的事务 commit 或 rollback 之前,在 root 的事务中就读取到了 tom 的事务中更新且进行事务提交了的数据。(未屏蔽'''<big>不可重复读</big>'''的问题,在 root 的同一个事务中两次读取到的 user='AA' 的 balance 值不一致) === 验证事务隔离级别为 READ-UNCOMMITTED 时的情况 === ==== root 设置全局事务隔离级别为 READ-UNCOMMITTED ==== <syntaxhighlight lang="powershell"> mysql> set global transaction isolation level read uncommitted; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye C:\Users\Administrator>mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 116 Server version: 5.7.35-log MySQL Community Server (GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select @@tx_isolation; +------------------+ | @@tx_isolation | +------------------+ | READ-UNCOMMITTED | +------------------+ 1 row in set, 1 warning (0.00 sec) </syntaxhighlight> ==== tom 重新登录确认当前事务的隔离级别 ==== <syntaxhighlight lang="powershell"> mysql> exit Bye C:\Users\Administrator>mysql -utom -pabc123 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 117 Server version: 5.7.35-log MySQL Community Server (GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select @@tx_isolation; +------------------+ | @@tx_isolation | +------------------+ | READ-UNCOMMITTED | +------------------+ 1 row in set, 1 warning (0.00 sec) </syntaxhighlight> ==== tom、root 配置禁用事务自动提交 ==== <syntaxhighlight lang="powershell"> mysql> show variables like 'autocommit'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | autocommit | ON | +---------------+-------+ 1 row in set, 1 warning (0.00 sec) mysql> set autocommit = false; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'autocommit'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | autocommit | OFF | +---------------+-------+ 1 row in set, 1 warning (0.00 sec) </syntaxhighlight> ==== root 更新数据但不执行事务提交 ==== <syntaxhighlight lang="powershell"> mysql> update user_table set balance = 5000 where user = 'AA'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from user_table where user = 'AA'; +------+----------+---------+ | user | password | balance | +------+----------+---------+ | AA | 123456 | 5000 | +------+----------+---------+ 1 row in set (0.00 sec) </syntaxhighlight> ==== tom 查询数据 ==== <syntaxhighlight lang="powershell"> mysql> use jdbc_test; Database changed mysql> select * from user_table where user = 'AA'; +------+----------+---------+ | user | password | balance | +------+----------+---------+ | AA | 123456 | 5000 | +------+----------+---------+ 1 row in set (0.00 sec) </syntaxhighlight>因为当前 tom 的事务隔离级别是 READ-UNCOMMITTED,所以读取到了 root 事务中未提交的数据更新。(未屏蔽'''<big>脏读</big>''') ==== root 回滚事务 ==== <syntaxhighlight lang="powershell"> mysql> rollback; Query OK, 0 rows affected (0.01 sec) mysql> select * from user_table where user = 'AA'; +------+----------+---------+ | user | password | balance | +------+----------+---------+ | AA | 123456 | 4000 | +------+----------+---------+ 1 row in set (0.00 sec) </syntaxhighlight> ==== tom 再查询数据 ==== <syntaxhighlight lang="powershell"> mysql> select * from user_table where user = 'AA'; +------+----------+---------+ | user | password | balance | +------+----------+---------+ | AA | 123456 | 4000 | +------+----------+---------+ 1 row in set (0.00 sec) </syntaxhighlight>脏读, tom 在自己的事务中如果以 root 中未提交的事务影响的数据(且之后进行了回滚)作为基础进行其他操作就会对数据的正确性产生影响。
返回至
命令行验证MySQL的隔离级别
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息