MySQL/MariaDB數據庫的事務和隔離級別

     MySQL/MariaDB數據庫的事務和隔離級別node

                             做者:尹正傑 mysql

版權聲明:原創做品,謝絕轉載!不然將追究法律責任。sql

 

 

一.事務概述數據庫

1>.事務Transactionsvim

  一組原子性的SQL語句,或一個獨立工做單元。

2>.事務日誌安全

  因爲直接對源表進行修改當服務器忽然掉電可能會致使源表數據被損壞,所以能夠將修改先記錄事務信息,後根據事務日誌信息再對源表實現undo,redo等故障恢復功能

  redo:
    將事務日誌中已經commit的事務進行復現操做,即對源表進行修改。

  undo:
    將事務日誌中未commit的事務進行rollback(回滾,撤銷)操做,這些操做將不會對源表進行修改。

3>.ACID特性服務器

  A:
    atomicity原子性;整個事務中的全部操做要麼所有成功執行,要麼所有失敗後回滾。
  C:     consistency一致性;數據庫老是從一個一致性狀態轉換爲另外一個一致性狀態。
  I:     Isolation隔離性;一個事務所作出的操做在提交以前,是不能爲其它事務所見;隔離有多種隔離級別,實現併發。
  D:     durability持久性;一旦事務提交,其所作的修改會永久保存於數據庫中。

4>.Transaction生命週期併發

 

二.事務隔離級別概述運維

1>.事務隔離級別socket

從上至下更加嚴格:
  READ UNCOMMITTED 
    可讀取到未提交數據,產生髒讀   
READ COMMITTED
    可讀取到提交數據,但未提交數據不可讀,產生不可重複讀,便可讀取到多個提交數據,致使每次讀取數據不一致   
REPEATABLE READ
    可重複讀,屢次讀取數據都一致,產生幻讀,即讀取過程當中,即便有其它提交的事務修改數據,仍只能讀取到未修改前的舊數據。此爲MySQL默認設置   SERIALIZABILE
    可串行化,未提交的讀事務阻塞修改事務,或者未提交的修改事務阻塞讀事務。致使併發性能差

2>.MVCC

  多版本併發控制,和事務級別相關。

3>.指定事務隔離級別

MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';      #查看默認的事務隔離級別
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| tx_isolation  | REPEATABLE-READ |
+---------------+-----------------+
row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';      #查看默認的事務隔離級別
服務器變量tx_isolation指定,默認爲REPEATABLE-READ,可在GLOBAL和SESSION級進行設置
  SET tx_isolation='READ-UNCOMMITTED|READ-COMMITTED|REPEATABLE-READ|SERIALIZABLE'

服務器選項中指定:(注意,服務器選項和上面的變量名並不一樣名,詳情可參考官官網鏈接)
vim /etc/my.cnf
[mysqld]
transaction-isolation=SERIALIZABLE

博主推薦閱讀:
  https://mariadb.com/kb/en/library/server-system-variables/#tx_isolation
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| tx_isolation  | REPEATABLE-READ |
+---------------+-----------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SET tx_isolation = 'READ-UNCOMMITTED';    #修改隔離級別爲讀未提交,可能產生髒讀。
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+------------------+
| Variable_name | Value            |
+---------------+------------------+
| tx_isolation  | READ-UNCOMMITTED |
+---------------+------------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SET tx_isolation = 'READ-UNCOMMITTED';    #修改隔離級別爲讀未提交,可能產生髒讀。(臨時修改)
[root@node105.yinzhengjie.org.cn ~]# vim /mysql/3306/etc/my.cnf
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf    #根據官網的幫助,對實例默認事務隔離級別修改成SERIALIZABLE
[mysqld]
character-set-server    = utf8mb4
default_storage_engine  = InnoDB
transaction-isolation    = SERIALIZABLE
autocommit        = 1
skip_name_resolve    = 1
userstat        = ON
port            = 3306
datadir            = /mysql/3306/data
socket            = /mysql/3306/socket/mysql.sock


[mysqld_safe]
log-error        = /mysql/3306/log/mariadb.log
pid-file        = /mysql/3306/pid/mariadb.pid

[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3306/mysqld restart
Restarting MySQL...
Stoping MySQL...
Starting MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q               Local Address:Port                              Peer Address:Port              
LISTEN      0      128                              *:22                                           *:*                  
LISTEN      0      80                              :::3306                                        :::*                  
LISTEN      0      128                             :::22                                          :::*                  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mysql/3306/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.19-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> use yinzhengjie
Database changed
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+--------------+
| Variable_name | Value        |
+---------------+--------------+
| tx_isolation  | SERIALIZABLE |
+---------------+--------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf   #對實例默認事務隔離級別修改成SERIALIZABLE

 

三.事務相關操做

1>.自動提交

set autocommit={1|0} 
  默認爲1,爲0時設爲非自動提交
建議:   顯式請求和提交事務,而不要使用「自動提交」功能
MariaDB [yinzhengjie]> SELECT @@AUTOCOMMIT;    #默認開啓了自動提交功能,建議顯式請求和提交事務,而不要使用自動提交功能。
+--------------+
| @@AUTOCOMMIT |
+--------------+
|            1 |
+--------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT @@AUTOCOMMIT;  #默認開啓了自動提交功能,建議顯式請求和提交事務,而不要使用自動提交功能。
MariaDB [yinzhengjie]> SELECT @@AUTOCOMMIT;
+--------------+
| @@AUTOCOMMIT |
+--------------+
|            1 |
+--------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SET AUTOCOMMIT = 0;      #爲0時設爲非自動提交,須要手動顯式提交,這種修改只是臨時設置,推薦修改配置文件永久配置。
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT @@AUTOCOMMIT;
+--------------+
| @@AUTOCOMMIT |
+--------------+
|            0 |
+--------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SET AUTOCOMMIT = 0;      #爲0時設爲非自動提交,須要手動顯式提交,這種修改只是臨時設置,推薦修改配置文件永久配置。
MariaDB [yinzhengjie]> SELECT @@AUTOCOMMIT;
+--------------+
| @@AUTOCOMMIT |
+--------------+
|            1 |
+--------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> QUIT
Bye
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3306/mysqld stop
Stoping MySQL...
[root@node105.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN      0      128                                          *:22                                                       *:*                  
LISTEN      0      128                                         :::22                                                      :::*                  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# vim /mysql/3306/etc/my.cnf
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf      #修改配置文件關閉自動提交功能。
[mysqld]
character-set-server    = utf8mb4
default_storage_engine  = InnoDB
autocommit        = 0
skip_name_resolve    = 1
userstat        = ON
port            = 3306
datadir            = /mysql/3306/data
socket            = /mysql/3306/socket/mysql.sock


[mysqld_safe]
log-error        = /mysql/3306/log/mariadb.log
pid-file        = /mysql/3306/pid/mariadb.pid

[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3306/mysqld start
Starting MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN      0      128                                          *:22                                                       *:*                  
LISTEN      0      80                                          :::3306                                                    :::*                  
LISTEN      0      128                                         :::22                                                      :::*                  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mysql/3306/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.19-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> use yinzhengjie
Database changed
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT @@AUTOCOMMIT;
+--------------+
| @@AUTOCOMMIT |
+--------------+
|            0 |
+--------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf      #修改配置文件關閉自動提交功能。
MariaDB [yinzhengjie]> SELECT @@AUTOCOMMIT;
+--------------+
| @@AUTOCOMMIT |
+--------------+
|            0 |
+--------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> CREATE TABLE students(id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,name VARCHAR(30) NOT NULL,sex ENUM('boy','girl') DEFAULT 'boy',age TINYINT UNSIGNED,mobile CHAR(11),address VARCHAR(50));
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> INSERT INTO students (name,age,mobile,address) VALUES ('Jason Yin',18,10000,'beijing'),('Jay','40',10086,'Taiwan');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+-----------+------+------+--------+---------+
| id | name      | sex  | age  | mobile | address |
+----+-----------+------+------+--------+---------+
|  1 | Jason Yin | boy  |   18 | 10000  | beijing |
|  2 | Jay       | boy  |   40 | 10086  | Taiwan  |
+----+-----------+------+------+--------+---------+
2 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
默認事務隔離級別且關閉自動提交的實例一終端執行DML語句
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mysql/3306/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.2.19-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SELECT * FROM yinzhengjie.students;      #在默認的事務隔離級別沒法看到未提交的數據。
Empty set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> 
在默認的事務隔離級別沒法看到未提交的數據。

2>.啓動事務

如下三條命令都可以啓動事務:
  BEGIN
  BEGIN WORK
  START TRANSACTION
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+-----------+------+------+--------+---------+
| id | name      | sex  | age  | mobile | address |
+----+-----------+------+------+--------+---------+
|  1 | Jason Yin | boy  |   18 | 10000  | beijing |
|  2 | Jay       | boy  |   40 | 10086  | Taiwan  |
+----+-----------+------+------+--------+---------+
2 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> BEGIN;              #終端1開啓事務進行DML語句操做但不COMMIT,此時發現修改在當前會話生效。
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> INSERT students (age,sex,name,mobile,address) VALUES (28,'girl','Gloria Tang Tsz-Kei',null,'Hong Kong');
Query OK, 1 row affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   18 | 10000  | beijing   |
|  2 | Jay                 | boy  |   40 | 10086  | Taiwan    |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
+----+---------------------+------+------+--------+-----------+
3 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> BEGIN;     #終端1開啓事務進行DML語句操做但不COMMIT,此時發現修改在當前會話生效。
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mysql/3306/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.19-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use yinzhengjie
Database changed
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+-----------+------+------+--------+---------+
| id | name      | sex  | age  | mobile | address |
+----+-----------+------+------+--------+---------+
|  1 | Jason Yin | boy  |   18 | 10000  | beijing |
|  2 | Jay       | boy  |   40 | 10086  | Taiwan  |
+----+-----------+------+------+--------+---------+
2 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
終端2默認事務隔離級別看不到終端1未提交的DML語句指令執行的結果。
MariaDB [yinzhengjie]> BEGIN;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> INSERT students (age,sex,name,mobile,address) VALUES (28,'girl','Gloria Tang Tsz-Kei',null,'Hong Kong');
Query OK, 1 row affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   18 | 10000  | beijing   |
|  2 | Jay                 | boy  |   40 | 10086  | Taiwan    |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
+----+---------------------+------+------+--------+-----------+
3 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> DELETE FROM students WHERE id = 2;
Query OK, 1 row affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> UPDATE students SET age = 27 WHERE id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
+----+---------------------+------+------+--------+-----------+
2 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> COMMIT;      #終端1提交事務,全部在改事務中執行的DML語句均會生效.
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> COMMIT;      #終端1提交事務,全部在改事務中執行的DML語句均會生效.
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mysql/3306/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.19-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SELECT * FROM yinzhengjie.students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
+----+---------------------+------+------+--------+-----------+
2 rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> 
終端2默認事務隔離級別能夠看到已經提交的DML語句指令執行的結果。

3>.結束事務

COMMIT
  提交
ROLLBACK:
  回滾
注意:
  只有事務型存儲引擎中的DML語句方能支持此類操做。
MariaDB [yinzhengjie]> SELECT @@AUTOCOMMIT;
+--------------+
| @@AUTOCOMMIT |
+--------------+
|            0 |
+--------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
Empty set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> INSERT INTO students (name,age,mobile,address) VALUES ('Jason Yin',18,10000,'beijing'),('Jay','40',10086,'Taiwan');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+-----------+------+------+--------+---------+
| id | name      | sex  | age  | mobile | address |
+----+-----------+------+------+--------+---------+
|  1 | Jason Yin | boy  |   18 | 10000  | beijing |
|  2 | Jay       | boy  |   40 | 10086  | Taiwan  |
+----+-----------+------+------+--------+---------+
2 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> ROLLBACK;        #回滾,從當前語句到上一條COMMIT以後DML語句均被撤銷執行。
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
Empty set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> INSERT students (age,sex,name,mobile,address) VALUES (28,'girl','Gloria Tang Tsz-Kei',null,'Hong Kong');
Query OK, 1 row affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
+----+---------------------+------+------+--------+-----------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> COMMIT;          #提交,即從當前語句到上一條COMMIT以後DML語句所有執行。
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]>
在默認事務隔離級別且關閉自動提交功能的MySQL實例一中端執行DML語句操做
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mysql/3306/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.2.19-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> use yinzhengjie
Database changed
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
+----+---------------------+------+------+--------+-----------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
在默認的事務隔離級別只能看到已經提交的數據。

4>.事務支持保存點

SAVEPOINT identifier
  建立保存點
ROLLBACK [WORK] TO [SAVEPOINT] identifier
  回滾到指定的保存點 RELEASE SAVEPOINT identifier          
  釋放保存點
MariaDB [yinzhengjie]> SELECT @@AUTOCOMMIT;
+--------------+
| @@AUTOCOMMIT |
+--------------+
|            1 |
+--------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> SELECT *  FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
+----+---------------------+------+------+--------+-----------+
2 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SAVEPOINT point_yin;      #此處咱們建立一個名稱爲point_yin的保存點
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> INSERT INTO students (name,age,mobile,address) VALUES ('Jay','40',10086,'Taiwan');
Query OK, 1 row affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT *  FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  4 | yinzhengjie         | boy  |   27 | NULL   | shanxi    |
|  5 | Jay                 | boy  |   40 | 10086  | Taiwan    |
+----+---------------------+------+------+--------+-----------+
4 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SAVEPOINT point_jay;        #在第一個保存點point_yin以後執行了一些DML指令後,建立第二個保存點,名稱爲point_jay
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> INSERT INTO students (name,age,mobile,address) VALUES ('JangNaRa','38',null,'Seoul');
Query OK, 1 row affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT *  FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  4 | yinzhengjie         | boy  |   27 | NULL   | shanxi    |
|  5 | Jay                 | boy  |   40 | 10086  | Taiwan    |
|  6 | JangNaRa            | boy  |   38 | NULL   | Seoul     |
+----+---------------------+------+------+--------+-----------+
5 rows in set (0.00 sec)

MariaDB [yinzhengjie]> SAVEPOINT point_zhang;        #同理,這是咱們建立的第三個保存點
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> DELETE FROM students WHERE id >= 3;
Query OK, 4 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT *  FROM students;
+----+-----------+------+------+--------+---------+
| id | name      | sex  | age  | mobile | address |
+----+-----------+------+------+--------+---------+
|  1 | Jason Yin | boy  |   27 | 10000  | beijing |
+----+-----------+------+------+--------+---------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> ROLLBACK WORK TO SAVEPOINT point_zhang;    #因爲咱們上面誤刪除了數據,所以能夠回滾到指定的保存點
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT *  FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  4 | yinzhengjie         | boy  |   27 | NULL   | shanxi    |
|  5 | Jay                 | boy  |   40 | 10086  | Taiwan    |
|  6 | JangNaRa            | boy  |   38 | NULL   | Seoul     |
+----+---------------------+------+------+--------+-----------+
5 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> RELEASE SAVEPOINT point_jay;            #此時咱們釋放第二個保存點
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> ROLLBACK TO point_jay;      #因爲第二個保存點被釋放了,所以報錯改保存點不存在。
ERROR 1305 (42000): SAVEPOINT point_jay does not exist
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
案例展現

 

四.事務隔離級別實戰案例

1>.READ-UNCOMMITTED(不推薦使用,由於它能產生髒讀,不安全)

[root@node105.yinzhengjie.org.cn ~]# vim /mysql/3306/etc/my.cnf
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf      #修改默認的事務隔離級別爲"READ-UNCOMMITTED"
[mysqld]
character-set-server    = utf8mb4
default_storage_engine  = InnoDB
transaction-isolation    = READ-UNCOMMITTED
autocommit        = 1
skip_name_resolve    = 1
userstat        = ON
port            = 3306
datadir            = /mysql/3306/data
socket            = /mysql/3306/socket/mysql.sock


[mysqld_safe]
log-error        = /mysql/3306/log/mariadb.log
pid-file        = /mysql/3306/pid/mariadb.pid

[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3306/mysqld restart
Restarting MySQL...
Stoping MySQL...
Starting MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mysql/3306/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.19-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+------------------+
| Variable_name | Value            |
+---------------+------------------+
| tx_isolation  | READ-UNCOMMITTED |
+---------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf     #修改默認的事務隔離級別爲"READ-UNCOMMITTED"
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+------------------+
| Variable_name | Value            |
+---------------+------------------+
| tx_isolation  | READ-UNCOMMITTED |
+---------------+------------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
+----+---------------------+------+------+--------+-----------+
rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> BEGIN;              #開啓一個事務
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> INSERT INTO students SET name='yinzhengjie',age=27,address='shanxi';    #執行一條DML語句
Query OK, 1 row affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;    #雖然當前事務並未提交,可是我們能夠看到修改後的內容
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
|  8 | yinzhengjie         | boy  |   27 | NULL   | shanxi    |
+----+---------------------+------+------+--------+-----------+
rows in set (0.00 sec)

MariaDB [yinzhengjie]>
終端1事務執行DML語句並未提交
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+------------------+
| Variable_name | Value            |
+---------------+------------------+
| tx_isolation  | READ-UNCOMMITTED |
+---------------+------------------+
row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
+----+---------------------+------+------+--------+-----------+
rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> BEGIN WORK;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
|  8 | yinzhengjie         | boy  |   27 | NULL   | shanxi    |
+----+---------------------+------+------+--------+-----------+
rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]>
終端2事務竟能夠看到終端1未提交的內容

2>.READ-COMMITTED(Oracle數據庫默認使用就是相似該級別)

[root@node105.yinzhengjie.org.cn ~]# vim /mysql/3306/etc/my.cnf
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf            #修改默認的事務隔離級別爲READ-COMMITTED
[mysqld]
character-set-server    = utf8mb4
default_storage_engine  = InnoDB
transaction-isolation    = READ-COMMITTED
autocommit        = 1
skip_name_resolve    = 1
userstat        = ON
port            = 3306
datadir            = /mysql/3306/data
socket            = /mysql/3306/socket/mysql.sock


[mysqld_safe]
log-error        = /mysql/3306/log/mariadb.log
pid-file        = /mysql/3306/pid/mariadb.pid

[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3306/mysqld restart
Restarting MySQL...
Stoping MySQL...
Starting MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q               Local Address:Port                              Peer Address:Port              
LISTEN      0      128                              *:22                                           *:*                  
LISTEN      0      80                              :::3306                                        :::*                  
LISTEN      0      128                             :::22                                          :::*                  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mysql/3306/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.19-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+----------------+
| Variable_name | Value          |
+---------------+----------------+
| tx_isolation  | READ-COMMITTED |
+---------------+----------------+
1 row in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf       #修改默認的事務隔離級別爲READ-COMMITTED
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+----------------+
| Variable_name | Value          |
+---------------+----------------+
| tx_isolation  | READ-COMMITTED |
+---------------+----------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
+----+---------------------+------+------+--------+-----------+
3 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> BEGIN ;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> INSERT INTO students SET name='yinzhengjie',age=27,address='shanxi';
Query OK, 1 row affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
|  8 | yinzhengjie         | boy  |   27 | NULL   | shanxi    |
+----+---------------------+------+------+--------+-----------+
4 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
終端1事務執行DML語句並未提交
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+----------------+
| Variable_name | Value          |
+---------------+----------------+
| tx_isolation  | READ-COMMITTED |
+---------------+----------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
+----+---------------------+------+------+--------+-----------+
3 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> BEGIN WORK;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
+----+---------------------+------+------+--------+-----------+
3 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
終端2事務看不到終端1未提交的內容
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+----------------+
| Variable_name | Value          |
+---------------+----------------+
| tx_isolation  | READ-COMMITTED |
+---------------+----------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
+----+---------------------+------+------+--------+-----------+
3 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> BEGIN;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> INSERT INTO students SET name='yinzhengjie',age=27,address='shanxi';
Query OK, 1 row affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
|  9 | yinzhengjie         | boy  |   27 | NULL   | shanxi    |
+----+---------------------+------+------+--------+-----------+
4 rows in set (0.00 sec)

MariaDB [yinzhengjie]> COMMIT;
Query OK, 0 rows affected (0.01 sec)

MariaDB [yinzhengjie]> 
終端1事務執行DML語句並提交
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+----------------+
| Variable_name | Value          |
+---------------+----------------+
| tx_isolation  | READ-COMMITTED |
+---------------+----------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+----------------+
| Variable_name | Value          |
+---------------+----------------+
| tx_isolation  | READ-COMMITTED |
+---------------+----------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
+----+---------------------+------+------+--------+-----------+
3 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> BEGIN WORK;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
+----+---------------------+------+------+--------+-----------+
3 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
|  9 | yinzhengjie         | boy  |   27 | NULL   | shanxi    |
+----+---------------------+------+------+--------+-----------+
4 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
終端2事務能夠看到終端1已提交的內容

3>.REPEATABLE-READ(MySQL默認的事務隔離級別,備份數據默認使用該事務級別)

[root@node105.yinzhengjie.org.cn ~]# vim /mysql/3306/etc/my.cnf
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf         #修改默認的事務隔離級別爲REPEATABLE-READ
[mysqld]
character-set-server    = utf8mb4
default_storage_engine  = InnoDB
transaction-isolation    = REPEATABLE-READ
autocommit        = 1
skip_name_resolve    = 1
userstat        = ON
port            = 3306
datadir            = /mysql/3306/data
socket            = /mysql/3306/socket/mysql.sock


[mysqld_safe]
log-error        = /mysql/3306/log/mariadb.log
pid-file        = /mysql/3306/pid/mariadb.pid

[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3306/mysqld restart
Restarting MySQL...
Stoping MySQL...
Starting MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN      0      128                                          *:22                                                       *:*                  
LISTEN      0      80                                          :::3306                                                    :::*                  
LISTEN      0      128                                         :::22                                                      :::*                  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mysql/3306/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.19-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| tx_isolation  | REPEATABLE-READ |
+---------------+-----------------+
1 row in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf   #修改默認的事務隔離級別爲REPEATABLE-READ
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| tx_isolation  | REPEATABLE-READ |
+---------------+-----------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
|  9 | yinzhengjie         | boy  |   27 | NULL   | shanxi    |
+----+---------------------+------+------+--------+-----------+
4 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> BEGIN;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> INSERT students (age,sex,name,mobile,address) VALUES (38,'girl','JangNaRa',null,'Republic of Korea');
Query OK, 1 row affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> COMMIT;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-------------------+
| id | name                | sex  | age  | mobile | address           |
+----+---------------------+------+------+--------+-------------------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing           |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong         |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan            |
|  9 | yinzhengjie         | boy  |   27 | NULL   | shanxi            |
| 10 | JangNaRa            | girl |   38 | NULL   | Republic of Korea |
+----+---------------------+------+------+--------+-------------------+
5 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
終端1事務執行DML語句並提交
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| tx_isolation  | REPEATABLE-READ |
+---------------+-----------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
|  9 | yinzhengjie         | boy  |   27 | NULL   | shanxi    |
+----+---------------------+------+------+--------+-----------+
4 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> BEGIN WORK;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
|  9 | yinzhengjie         | boy  |   27 | NULL   | shanxi    |
+----+---------------------+------+------+--------+-----------+
4 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
終端2事務不能看見已提交的內容(由於終端2的事務要比和終端1事務先開啓)

4>.SERIALIZABLE(這種事務隔離級別不多用,由於併發性能差,除非數據要求過重要) 

[root@node105.yinzhengjie.org.cn ~]# vim /mysql/3306/etc/my.cnf
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf        #修改默認的事務隔離級別爲SERIALIZABLE 
[mysqld]
character-set-server    = utf8mb4
default_storage_engine  = InnoDB
transaction-isolation    = SERIALIZABLE
autocommit        = 1
skip_name_resolve    = 1
userstat        = ON
port            = 3306
datadir            = /mysql/3306/data
socket            = /mysql/3306/socket/mysql.sock


[mysqld_safe]
log-error        = /mysql/3306/log/mariadb.log
pid-file        = /mysql/3306/pid/mariadb.pid

[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3306/mysqld restart
Restarting MySQL...
Stoping MySQL...
Starting MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN      0      128                                          *:22                                                       *:*                  
LISTEN      0      80                                          :::3306                                                    :::*                  
LISTEN      0      128                                         :::22                                                      :::*                  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]#mmysql -uroot -pyinzhengjie -S /mysql/3306/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.19-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+--------------+
| Variable_name | Value        |
+---------------+--------------+
| tx_isolation  | SERIALIZABLE |
+---------------+--------------+
1 row in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]>
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf     #修改默認的事務隔離級別爲SERIALIZABLE
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+--------------+
| Variable_name | Value        |
+---------------+--------------+
| tx_isolation  | SERIALIZABLE |
+---------------+--------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> BEGIN;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-------------------+
| id | name                | sex  | age  | mobile | address           |
+----+---------------------+------+------+--------+-------------------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing           |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong         |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan            |
|  9 | yinzhengjie         | boy  |   27 | NULL   | shanxi            |
| 10 | JangNaRa            | girl |   38 | NULL   | Republic of Korea |
+----+---------------------+------+------+--------+-------------------+
5 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
終端1事務執行查看語句但並不提交
MariaDB [yinzhengjie]> BEGIN WORK;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-------------------+
| id | name                | sex  | age  | mobile | address           |
+----+---------------------+------+------+--------+-------------------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing           |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong         |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan            |
|  9 | yinzhengjie         | boy  |   27 | NULL   | shanxi            |
| 10 | JangNaRa            | girl |   38 | NULL   | Republic of Korea |
+----+---------------------+------+------+--------+-------------------+
rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> DELETE FROM students WHERE id = 10;           #因爲終端1有查詢事務且未提交,所以未提交的讀事務會阻塞修改事務
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
MariaDB [yinzhengjie]> 
終端2事務也能夠執行查看語句,但沒法執行刪除指令,即未提交的讀事務會阻塞修改事務
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+--------------+
| Variable_name | Value        |
+---------------+--------------+
| tx_isolation  | SERIALIZABLE |
+---------------+--------------+
1 row in set (0.01 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-------------------+
| id | name                | sex  | age  | mobile | address           |
+----+---------------------+------+------+--------+-------------------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing           |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong         |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan            |
|  9 | yinzhengjie         | boy  |   27 | NULL   | shanxi            |
| 10 | JangNaRa            | girl |   38 | NULL   | Republic of Korea |
+----+---------------------+------+------+--------+-------------------+
5 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> BEGIN;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> DELETE FROM students WHERE id = 10;
Query OK, 1 row affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+----+---------------------+------+------+--------+-----------+
| id | name                | sex  | age  | mobile | address   |
+----+---------------------+------+------+--------+-----------+
|  1 | Jason Yin           | boy  |   27 | 10000  | beijing   |
|  3 | Gloria Tang Tsz-Kei | girl |   28 | NULL   | Hong Kong |
|  7 | Jay                 | boy  |   40 | 10086  | Taiwan    |
|  9 | yinzhengjie         | boy  |   27 | NULL   | shanxi    |
+----+---------------------+------+------+--------+-----------+
4 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
終端1事務執行刪除語句並未提交
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+--------------+
| Variable_name | Value        |
+---------------+--------------+
| tx_isolation  | SERIALIZABLE |
+---------------+--------------+
1 row in set (0.01 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> BEGIN WORK;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;          #未提交的修改事務阻塞讀事務
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
MariaDB [yinzhengjie]> 
若終端1存在修改事務且未提交,終端2事務沒法執行查詢語句,即未提交的修改事務阻塞讀事務

 

五.死鎖問題

1>.什麼是死鎖

  兩個或多個事務在同一資源相互佔用,並請求鎖定對方佔用的資源的狀態

2>.死鎖案例模擬

MariaDB [yinzhengjie]> SELECT * FROM students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |
|     6 | Shi Qing      |  46 | M      |       5 |      NULL |
|     7 | Xi Ren        |  19 | F      |       3 |      NULL |
|     8 | Lin Daiyu     |  17 | F      |       7 |      NULL |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
|    10 | Yue Lingshan  |  19 | F      |       3 |      NULL |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    12 | Wen Qingqing  |  19 | F      |       1 |      NULL |
|    13 | Tian Boguang  |  33 | M      |       2 |      NULL |
|    14 | Lu Wushuang   |  17 | F      |       3 |      NULL |
|    15 | Duan Yu       |  19 | M      |       4 |      NULL |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |
|    19 | Xue Baochai   |  18 | F      |       6 |      NULL |
|    20 | Diao Chan     |  19 | F      |       7 |      NULL |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |
|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
|    23 | Ma Chao       |  23 | M      |       4 |      NULL |
|    24 | Xu Xian       |  27 | M      |    NULL |      NULL |
|    25 | Sun Dasheng   | 100 | M      |    NULL |      NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
MariaDB [yinzhengjie]> SELECT * FROM teachers;
+-----+---------------+-----+--------+
| TID | Name          | Age | Gender |
+-----+---------------+-----+--------+
|   1 | Song Jiang    |  45 | M      |
|   2 | Zhang Sanfeng |  94 | M      |
|   3 | Miejue Shitai |  77 | F      |
|   4 | Lin Chaoying  |  93 | F      |
+-----+---------------+-----+--------+
4 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM teachers;
MariaDB [yinzhengjie]> BEGIN;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> UPDATE students SET classid = 1 WHERE stuid = 25;     #於此同時可在終端2執行該指令,修改一下classid列的值便可
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |
|     6 | Shi Qing      |  46 | M      |       5 |      NULL |
|     7 | Xi Ren        |  19 | F      |       3 |      NULL |
|     8 | Lin Daiyu     |  17 | F      |       7 |      NULL |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
|    10 | Yue Lingshan  |  19 | F      |       3 |      NULL |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    12 | Wen Qingqing  |  19 | F      |       1 |      NULL |
|    13 | Tian Boguang  |  33 | M      |       2 |      NULL |
|    14 | Lu Wushuang   |  17 | F      |       3 |      NULL |
|    15 | Duan Yu       |  19 | M      |       4 |      NULL |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |
|    19 | Xue Baochai   |  18 | F      |       6 |      NULL |
|    20 | Diao Chan     |  19 | F      |       7 |      NULL |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |
|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
|    23 | Ma Chao       |  23 | M      |       4 |      NULL |
|    24 | Xu Xian       |  27 | M      |    NULL |      NULL |
|    25 | Sun Dasheng   | 100 | M      |       1 |      NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)

MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> UPDATE teachers SET age = 25 WHERE tid = 4;      #一樣,在終端2也執行該語句,觀察狀況
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [yinzhengjie]> SELECT * FROM teachers;
+-----+---------------+-----+--------+
| TID | Name          | Age | Gender |
+-----+---------------+-----+--------+
|   1 | Song Jiang    |  45 | M      |
|   2 | Zhang Sanfeng |  94 | M      |
|   3 | Miejue Shitai |  77 | F      |
|   4 | Lin Chaoying  |  25 | F      |
+-----+---------------+-----+--------+
4 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
終端1事務執行UPDATE命令對某一行數據進行修改的同時,Innodb存儲引擎默認會加行級鎖(咱們能夠對兩張表進程測試)
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| tx_isolation  | REPEATABLE-READ |
+---------------+-----------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> BEGIN WORK;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> UPDATE teachers SET age = 30 WHERE tid = 4;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM teachers;
+-----+---------------+-----+--------+
| TID | Name          | Age | Gender |
+-----+---------------+-----+--------+
|   1 | Song Jiang    |  45 | M      |
|   2 | Zhang Sanfeng |  94 | M      |
|   3 | Miejue Shitai |  77 | F      |
|   4 | Lin Chaoying  |  30 | F      |
+-----+---------------+-----+--------+
4 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> UPDATE students SET classid = 1 WHERE stuid = 25;
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
MariaDB [yinzhengjie]> 
終端2事務執行和終端1相似的SQL語句對同一行數據修改,這樣就會出現死鎖(Deadlock)的問題。MySQL會自動選擇損失最小的結果(即讓某個事務執行SQL失敗)

3>.死鎖的解決方案

  讓多個事務按照順序來修改表,不要讓多個事務交叉來修改表。固然,運維人員對數據庫死鎖問題不用太大關心,直接拋給開發人員就好。
相關文章
相關標籤/搜索