MySQL 8.0查看并修改事务隔离级别
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MySQL 8.0查看并修改事务隔离级别,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4281字,纯文字阅读大概需要7分钟。
内容图文
![MySQL 8.0查看并修改事务隔离级别](/upload/InfoBanner/zyjiaocheng/859/3340cf5e7a98448bbe64d5462401fbd2.jpg)
MySQL 8.0默认事务隔离级别为REPEATABLE-READ(重复读)
官方文档13.3.7 SET TRANSACTION Statement https://dev.mysql.com/doc/refman/8.0/en/set-transaction.html
查看事务隔离级别
MySQL8查询事务应该使用transaction_isolation,tx_isolation在MySQL 5.7.20后被弃用。
-- 登录MySQL
mysql -uroot -p
-- 输入以下命令查看事务隔离级别,其中transaction_isolation就是隔离级别
mysql> show variables like 'transaction%';
+----------------------------------+-----------------+
| Variable_name | Value |
+----------------------------------+-----------------+
| transaction_alloc_block_size | 8192 |
| transaction_allow_batching | OFF |
| transaction_isolation | REPEATABLE-READ |
| transaction_prealloc_size | 4096 |
| transaction_read_only | OFF |
| transaction_write_set_extraction | XXHASH64 |
+----------------------------------+-----------------+
-- 或使用SQL查看
mysql> select @@transaction_isolation;
+-------------------------+
| @@transaction_isolation |
+-------------------------+
| REPEATABLE-READ |
+-------------------------+
修改事务隔离级别
数据库事务的隔离级别有4种,由低到高分别为read uncommitted(读未提交)、read committed(读提交)、repeatable read(重复读)、Serializable(序列化)。
语法
SET [GLOBAL | SESSION] TRANSACTION
transaction_characteristic [, transaction_characteristic] ...
transaction_characteristic: {
ISOLATION LEVEL level
| access_mode
}
level: {
REPEATABLE READ
| READ COMMITTED
| READ UNCOMMITTED
| SERIALIZABLE
}
access_mode: {
READ WRITE
| READ ONLY
}
使用说明
This statement sets the transaction isolation level globally, for the current session, or for the next transaction:
·With the GLOBAL keyword, the statement sets the default transaction level globally for all subsequent sessions. Existing sessions are unaffected.
·With the SESSION keyword, the statement sets the default transaction level for all subsequent transactions performed within the current session.
·Without any SESSION or GLOBAL keyword, the statement sets the isolation level for the next (not started) transaction performed within the current session.
使用例子
设置本次会话的事务隔离级别,只在本会话有效,不会影响到其它会话
-- 设置本次会话的事务隔离级别,只在本会话有效,不会影响到其它会话
mysql> set session transaction isolation level read committed;
Query OK, 0 rows affected (0.00 sec)
-- 再次查看发现已改成了read committed
mysql> select @@transaction_isolation;
+-------------------------+
| @@transaction_isolation |
+-------------------------+
| READ-COMMITTED |
+-------------------------+
-- 再登录其它窗口,再查看,发现还是repeatable read
mysql> select @@transaction_isolation;
+-------------------------+
| @@transaction_isolation |
+-------------------------+
| REPEATABLE-READ |
+-------------------------+
设置全局的事务隔离级别,该设置不会影响当前已经连接的会话,设置完毕后,新打开的会话,将使用新设置的事务隔离级别
-- 设置全局的事务隔离级别
mysql> set global transaction isolation level read committed;
Query OK, 0 rows affected (0.00 sec)
-- 不影响本次已连接会话的事务,所以查到的还是修改前的
mysql> select @@transaction_isolation;
+-------------------------+
| @@transaction_isolation |
+-------------------------+
| REPEATABLE-READ |
+-------------------------+
-- 再打开一个新会话,则变成了修改后的read committed
mysql> select @@transaction_isolation;
+-------------------------+
| @@transaction_isolation |
+-------------------------+
| READ-COMMITTED |
+-------------------------+
设置下一次事务操作的隔离级别,该设置会随着下一次事务的提交而失效
-- 查看自动提交是否开启
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
-- 关闭自动提交
mysql> set autocommit = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | OFF |
+---------------+-------+
-- 设置本次事务
set transaction isolation level read uncommitted;
-- 提交事务
commit;
通过配置文件my.ini也可以修改事务
vim /etc/my.cnf
[mysqld]
transaction-isolation = REPEATABLE-READ
transaction-read-only = OFF
原文链接:https://blog.csdn.net/q283614346/article/details/103938542
内容总结
以上是互联网集市为您收集整理的MySQL 8.0查看并修改事务隔离级别全部内容,希望文章能够帮你解决MySQL 8.0查看并修改事务隔离级别所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。