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

你可能感兴趣的:(mysql)