1. 程式人生 > >lock table read/write區別

lock table read/write區別

mysql lock
lock table table_nmae read|write
unlock tables 

[email protected]:32:27[q]>lock table t1 read ;
[email protected]:39:14[q]>show processlist ;
+----+------+-----------+--------------------+---------+------+----------+------------------+
| Id | User | Host      | db                 | Command | Time | State    | Info             |
+----+------+-----------+--------------------+---------+------+----------+------------------+
|  5 | root | localhost | q                  | Sleep   | 4344 |          | NULL             |
|  6 | root | localhost | q                  | Sleep   | 4431 |          | NULL             |
|  8 | root | localhost | information_schema | Sleep   | 4558 |          | NULL             |
| 10 | root | localhost | q                  | Query   |    0 | starting | show processlist |
| 11 | root | localhost | q                  | Sleep   |   27 |          | NULL             |
+----+------+-----------+--------------------+---------+------+----------+------------------+

當前會話操作只能對t1表read ,不能write:

[email protected]:36:47[q]>select * from t1 ;
+------+------+
| name | age  |
+------+------+
| a6   |    6 |
| a5   |    5 |
| a6   |    6 |
| a    |    1 |
+------+------+
[email protected]
:36:52[q]>insert into t1 select 'z' ,100 ; ERROR 1099 (HY000): Table 't1' was locked with a READ lock and can't be updated


當前會話對其它表不能read,write 

[email protected]:37:17[q]>select * from node ;
ERROR 1100 (HY000): Table 'node' was not locked with LOCK TABLES

其它會話對錶t1的dml操作都會metalock鎖等待,對其它表沒影響:
其它會話操作:

[email protected]:39:19[q]>insert into t1 select *from  ; 卡住  metadata lock 等待。
[email protected]:39:20[q]>show processlist ;
+----+------+-----------+--------------------+---------+------+---------------------------------+--------------------------------+
| Id | User | Host      | db                 | Command | Time | State                           | Info                           |
+----+------+-----------+--------------------+---------+------+---------------------------------+--------------------------------+
|  5 | root | localhost | q                  | Sleep   | 4397 |                                 | NULL                           |
|  6 | root | localhost | q                  | Sleep   | 4484 |                                 | NULL                           |
|  8 | root | localhost | information_schema | Sleep   | 4611 |                                 | NULL                           |
| 10 | root | localhost | q                  | Query   |    0 | starting                        | show processlist               |
| 11 | root | localhost | q                  | Query   |   32 | Waiting for table metadata lock | insert into t1 select *from t1 |
+----+------+-----------+--------------------+---------+------+---------------------------------+--------------------------------+

解鎖

[email protected]:40:13[q]>unlock table ;
Query OK, 0 rows affected (0.00 sec)
[email protected]:39:19[q]>insert into t1 select *from t1 ;
Query OK, 3 rows affected (1 min 36.86 sec)
Records: 3  Duplicates: 0  Warnings: 0
[email protected]:41:18[q]>show processlist ;
+----+------+-----------+--------------------+---------+------+----------+------------------+
| Id | User | Host      | db                 | Command | Time | State    | Info             |
+----+------+-----------+--------------------+---------+------+----------+------------------+
|  5 | root | localhost | q                  | Sleep   | 4485 |          | NULL             |
|  6 | root | localhost | q                  | Sleep   | 4572 |          | NULL             |
|  8 | root | localhost | information_schema | Sleep   | 4699 |          | NULL             |
| 10 | root | localhost | q                  | Query   |    0 | starting | show processlist |
| 11 | root | localhost | q                  | Sleep   |  120 |          | NULL             |
+----+------+-----------+--------------------+---------+------+----------+------------------+


LOCK TABLE t1 WRITE
write帶read許可權
當前會話可以對指定表t1進行read ,write ;其它表不行。

[email protected]:25:59[q]>lock table t1 write ;
Query OK, 0 rows affected (5.03 sec)
[email protected]:29:25[q]>show processlist ;
+----+------+-----------+------+---------+------+----------+------------------+
| Id | User | Host      | db   | Command | Time | State    | Info             |
+----+------+-----------+------+---------+------+----------+------------------+
| 18 | root | localhost | q    | Sleep   |    7 |          | NULL             |
| 22 | root | localhost | q    | Query   |    0 | starting | show processlist |
+----+------+-----------+------+---------+------+----------+------------------+
[email protected]:29:32[q]>select * from t1 ;
+------+------+
| name | age  |
+------+------+
| a6   |    6 |
| a5   |    5 |
| a6   |    6 |
+------+------+
[email protected]:29:36[q]>insert into t1 select * from t1 ;
ERROR 1100 (HY000): Table 't1' was not locked with LOCK TABLES
[email protected]:29:45[q]>insert into t1 select 'a',1 ;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

[email protected]:30:00[q]>select * from node limit 1 ;
ERROR 1100 (HY000): Table 'node' was not locked with LOCK TABLES

其它會話不能對錶t1當前read,write ,其它可以正常操作

[email protected]:29:25[q]>select * from t1 ;  -- 直接卡住
[email protected]:31:05[q]>select * from node limit 1 ; -- 其它表正常
+----+------+-----------+
| id | name | parent_id |
+----+------+-----------+
| 11 | 3    |         0 |
+----+------+-----------+


鎖的區別
lock tables/ flush table read lock 

flush table read lock: 全域性讀鎖  ,解鎖:unlock tables 
lock table  table_name read/write :指定表鎖,解鎖:unlock table table_name /unlock tables table_names 每次只能持一個鎖

lock tables table read :沒有會話能DML read-locked 表
lock tables table_name write :除了持有鎖會話,沒有會話能訪問write-locked表