MySQL 5.5 LOCK TABLES 和 UNLOCK TABLES 語句介紹
LOCK TABLES 可以為當前客戶端會話的表加鎖。表鎖可以加到表或檢視中。你需要具有LOCK TABLES許可權和對錶的查詢許可權。當執行LOCK TABLES語句時,表中的觸發器也會被鎖住。
表鎖可以防止其他會話對錶的讀或寫。持有寫鎖的會話可以執行DROP TABLE或TRUNCATE TABLE語句,持有讀鎖的會話,不允許執行DROP TABLE或TRUNCATE TABLE語句。
讀鎖(READ [LOCAL] lock)
持有鎖的會話可以讀取表,但是不能執行寫入操作。
多個會話可以在同一時間獲取相同表的讀鎖。
其他沒有獲取讀鎖的會話可以讀取表中的內容。
LOCAL識別符號可以使其他會話執行併發的INSERT語句。對於InnoDB表,READ LOCAL和READ具有同樣的效果。
例子一:
--會話①
mysql> lock table t10 read;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from t10;
+------+---------------------+
| name | email |
+------+---------------------+
| Neo | feelpurple@163.com |
| fire | real_neo@163.com |
| xxx | real_xiaomm@163.com |
+------+---------------------+
3 rows in set (0.00 sec)
--會話②,可以執行查詢語句,對錶的DML、DDL操作會處於阻塞狀態,直到會話①表鎖被釋放
mysql> select * from t10;
+------+---------------------+
| name | email |
+------+---------------------+
| Neo | feelpurple@163.com |
| fire | real_neo@163.com |
| xxx | real_xiaomm@163.com |
+------+---------------------+
3 rows in set (0.00 sec)
mysql> insert into t10 values('Jason','real_xiaoyu@163.com');
--會話①
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
--會話②
mysql> insert into t10 values('Jason','real_xiaoyu@163.com');
Query OK, 1 row affected (2 min 7.82 sec)
當一個會話獲取表鎖後,這個會話只能訪問被鎖的這張表,直到表鎖被釋放為止。
例子二:
--會話①
mysql> lock table t10 read;
Query OK, 0 rows affected (0.00 sec)
mysql> select count(*) from t10;
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)
mysql> select count(*) from t11;
ERROR 1100 (HY000): Table 't11' was not locked with LOCK TABLES
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql> select count(*) from t10;
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)
mysql> select count(*) from t11;
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
寫鎖(WRITE lock)
持有鎖的會話可以對錶執行讀寫操作。
只有獲取鎖的會話才能訪問表。只有當鎖釋放掉,其他的會話才可以訪問表。
當表被加上寫鎖的時候,其他對這張表的鎖定請求會被阻塞。
--例子三:
--會話①
mysql> lock table t10 write;
Query OK, 0 rows affected (0.00 sec)
mysql> select count(*) from t10;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)
--會話②,對錶的查詢、DML、DDL操作都會處於阻塞狀態,直到會話①表鎖被釋放
mysql> select * from t10;
--會話①
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
--會話②
mysql> select * from t10;
+---------------------+
| email |
+---------------------+
| feelpurple@163.com |
| real_neo@163.com |
| real_xiaomm@163.com |
| real_xiaoyu@163.com |
+---------------------+
4 rows in set (1 min 35.10 sec)
--會話①
mysql> lock table t10 write;
Query OK, 0 rows affected (0.00 sec)
--會話②的DML操作會處於阻塞狀態
mysql> insert into t10 values('real_xiaolan@163.com');
--會話①
mysql> unlock tables;
Query OK, 0 rows affected (0.01 sec)
--會話②的DML操作成功
mysql> insert into t10 values('real_xiaolan@163.com');
Query OK, 1 row affected (35.04 sec)
--會話①,此時如果會話②不提交或回滾事務,則會話①再次執行LOCK TABLE語句會阻塞
mysql> lock table t10 write;
--會話②
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)
--會話①
mysql> lock table t10 write;
Query OK, 0 rows affected (24.75 sec)
UNLOCK TABLES 會釋放當前會話的表鎖,也可以釋放FLUSH TABLES WITH READ LOCK(這個命令會給MySQL中所有的資料庫加鎖)語句所加的全域性只讀鎖。
表鎖可以防止其他會話對錶的讀或寫。持有寫鎖的會話可以執行DROP TABLE或TRUNCATE TABLE語句,持有讀鎖的會話,不允許執行DROP TABLE或TRUNCATE TABLE語句。
讀鎖(READ [LOCAL] lock)
持有鎖的會話可以讀取表,但是不能執行寫入操作。
多個會話可以在同一時間獲取相同表的讀鎖。
其他沒有獲取讀鎖的會話可以讀取表中的內容。
LOCAL識別符號可以使其他會話執行併發的INSERT語句。對於InnoDB表,READ LOCAL和READ具有同樣的效果。
例子一:
--會話①
mysql> lock table t10 read;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from t10;
+------+---------------------+
| name | email |
+------+---------------------+
| Neo | feelpurple@163.com |
| fire | real_neo@163.com |
| xxx | real_xiaomm@163.com |
+------+---------------------+
3 rows in set (0.00 sec)
--會話②,可以執行查詢語句,對錶的DML、DDL操作會處於阻塞狀態,直到會話①表鎖被釋放
mysql> select * from t10;
+------+---------------------+
| name | email |
+------+---------------------+
| Neo | feelpurple@163.com |
| fire | real_neo@163.com |
| xxx | real_xiaomm@163.com |
+------+---------------------+
3 rows in set (0.00 sec)
mysql> insert into t10 values('Jason','real_xiaoyu@163.com');
--會話①
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
--會話②
mysql> insert into t10 values('Jason','real_xiaoyu@163.com');
Query OK, 1 row affected (2 min 7.82 sec)
當一個會話獲取表鎖後,這個會話只能訪問被鎖的這張表,直到表鎖被釋放為止。
例子二:
--會話①
mysql> lock table t10 read;
Query OK, 0 rows affected (0.00 sec)
mysql> select count(*) from t10;
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)
mysql> select count(*) from t11;
ERROR 1100 (HY000): Table 't11' was not locked with LOCK TABLES
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql> select count(*) from t10;
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)
mysql> select count(*) from t11;
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
寫鎖(WRITE lock)
持有鎖的會話可以對錶執行讀寫操作。
只有獲取鎖的會話才能訪問表。只有當鎖釋放掉,其他的會話才可以訪問表。
當表被加上寫鎖的時候,其他對這張表的鎖定請求會被阻塞。
--例子三:
--會話①
mysql> lock table t10 write;
Query OK, 0 rows affected (0.00 sec)
mysql> select count(*) from t10;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)
--會話②,對錶的查詢、DML、DDL操作都會處於阻塞狀態,直到會話①表鎖被釋放
mysql> select * from t10;
--會話①
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
--會話②
mysql> select * from t10;
+---------------------+
| email |
+---------------------+
| feelpurple@163.com |
| real_neo@163.com |
| real_xiaomm@163.com |
| real_xiaoyu@163.com |
+---------------------+
4 rows in set (1 min 35.10 sec)
--會話①
mysql> lock table t10 write;
Query OK, 0 rows affected (0.00 sec)
--會話②的DML操作會處於阻塞狀態
mysql> insert into t10 values('real_xiaolan@163.com');
--會話①
mysql> unlock tables;
Query OK, 0 rows affected (0.01 sec)
--會話②的DML操作成功
mysql> insert into t10 values('real_xiaolan@163.com');
Query OK, 1 row affected (35.04 sec)
--會話①,此時如果會話②不提交或回滾事務,則會話①再次執行LOCK TABLE語句會阻塞
mysql> lock table t10 write;
--會話②
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)
--會話①
mysql> lock table t10 write;
Query OK, 0 rows affected (24.75 sec)
UNLOCK TABLES 會釋放當前會話的表鎖,也可以釋放FLUSH TABLES WITH READ LOCK(這個命令會給MySQL中所有的資料庫加鎖)語句所加的全域性只讀鎖。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2086077/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL 5.5 FLUSH TABLES WITH READ LOCK語句介紹MySql
- mysql關於FLUSH TABLES和FLUSH TABLES WITH READ LOCK的理解MySql
- mysql 5.5 lock tables與隱式事務提交commitMySqlMIT
- 【Mysql】FLUSH TABLES WITH READ LOCKMySql
- mysql表鎖與lock tablesMySql
- MySQL 5.5 SHOW PROFILE、SHOW PROFILES語句介紹MySql
- Mysql報錯Fatal error:Can't open and lock privilege tablesMySqlError
- xtrabackup 不用lock tables來複製備庫
- MySQL 5.7 LIMIT語句介紹MySqlMIT
- MySQL 5.5 mysqlimport介紹MySqlImport
- dba_tables,dba_all_tables,user_tables,all_tables有什麼區別
- MySQL 5.5 mysqlbinlog 介紹MySql
- Oracle TablesOracle
- How Logs Work On MySQL With InnoDB TablesMySql
- MySQL 5.7 PREPARE、EXECUTE、DEALLOCATE語句介紹MySql
- MySQL 5.5儲存引擎介紹MySql儲存引擎
- 使用skip-grant-tables啟動庫後不能執行grant語句
- Oracle Externale TablesOracle
- Oracle - Tables/IndexesOracleIndex
- Oracle X$TablesOracle
- Oracle X$ TablesOracle
- Edit SAP tables
- Oracle Partitioned TablesOracle
- The differences between index-organized tables and ordinary tables (228)IndexZed
- mysqldump: Got error: 1168 differently defined non-MyISAM LOCK TABLESMySqlGoError
- MySQL 5.7 的事務控制語句的介紹MySql
- MySQL 5.6 global read lock 介紹MySql
- Profitability Analysis – General tables
- 【oracle】user_tablesOracle
- Views and Base Tables (243)View
- Restrictions on Analyzing TablesREST
- Overview of Tables (154)View
- Partitioned Tables (165)
- Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist解決辦法ErrorMySql
- mongodb常用語句介紹MongoDB
- 精妙SQL語句介紹SQL
- [原創] Mysql中 Desc tables 中MUl解釋MySql
- mysql 使用 informatin_schema tables 建立 shell commandsMySqlORM