髒讀 兩個事務,第一個事務修改了數據可是沒有提交,這時第二個事務讀取了第一個事務沒有提交的數據,稱之爲髒讀。mysql
不可重複讀 兩個事務,第一個事務讀取一條數據,這時第二個事務修改了或者提交了第一事務讀取的數據。再第一個事務內再次讀取與第一次讀取數據不一致,稱之爲不可重複讀。sql
幻讀 兩個事務,第一個事務修改了表全部行並無提交,這時第二個事務往數據表中新增了一條數據並提交結果就會發現數據裏還有沒有修改的數據行,稱之爲幻讀。shell
------------根據以上問題解決方案------------session
MySQL配置隔離級別app
讀未提交(read-uncommitted)會出現髒讀、不可重複讀、幻讀;命令行
不可重複讀or讀提交(read-committed)會出現不可重複讀、幻讀;不會出現髒讀;code
可重複讀(repeatable-read)會出現幻讀;不會出現髒讀、不可重複讀;接口
串行化(serializable)不會出現髒讀、不可重複讀、幻讀;事務
實例it
root@dongyue-virtual-machine:/var# mysql -V mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper //查看MySQL服務狀態,主要是是否啓動 $ /etc/init.d/mysql status //同過(命令行)接口登陸 $ mysql -uroot -proot //查看事務默認級別 $ mysql> select @@tx_isolation; +-----------------+ | @@tx_isolation | +-----------------+ | REPEATABLE-READ | +-----------------+ 1 row in set, 1 warning (0.70 sec) //修改當前事務隔離級別 //用戶A mysql> set session transaction isolation level read uncommitted; Query OK, 0 rows affected (0.00 sec) mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update account set money=money-1 where id = 1; Query OK, 1 row affected (0.09 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> rollback; Query OK, 0 rows affected (0.01 sec) //用戶B //事務A前 mysql> select * from account where id = 1; +----+-------+ | id | money | +----+-------+ | 1 | 1106 | +----+-------+ 1 row in set (0.00 sec) //事務A進行中並未提交 mysql> select * from account where id = 1; +----+-------+ | id | money | +----+-------+ | 1 | 1105 | +----+-------+ 1 row in set (0.00 sec)
總結:MySQL隔離級別分爲當前會話(session)和全局(global);MySQL默認事務隔離級別爲可重複讀(repeatable-read),根據業務不一樣;能夠作相關調整。
文獻來自書籍;