Mysql 8查看并修改事务隔离级别

Mysql 8的默认事务隔离级别为REPEATABLE-READ(重复读)

官方文档13.3.7 SET TRANSACTION Statement

查看事务隔离级别

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也可以修改事务
[mysqld]
transaction-isolation = REPEATABLE-READ
transaction-read-only = OFF
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值