MySQL中lock tables和unlock tables浅析

 

在MySQL中提供了锁定表(lock tables)和解锁表(unlock tables)的语法功能,ORACLE与SQL Server数据库当中没有这种语法。相信刚接触MySQL的人,都想详细、深入的了解一下这个功能.下面就尽量全面的解析、总结一下MySQL中lock tables与unlock tables的功能,如有不足或不正确的地方,欢迎指点一二。

锁定表的语法:

 

LOCK TABLES

tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE}

[, tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE}] ...

LOCAL修饰符表示可以允许在其他会话中对在当前会话中获取了READ锁的的表执行插入。但是当保持锁时,若使用Server外的会话来操纵数据库则不能使用READ LOCAL。另外,对于InnoDB表,READ LOCAL与READ相同。

The LOCAL modifier enables nonconflicting INSERT statements (concurrent inserts) by other sessions to execute while the lock is held. (See Section 8.11.3, “Concurrent Inserts”.) However, READ LOCAL cannot be used if you are going to manipulate the database using processes external to the server while you hold the lock. For InnoDB tables, READ LOCAL is the same as READ.

修饰符LOW_PRIORITY用于之前版本的MySQL,它会影响锁定行为,但是从MySQL 5.6.5以后,这个修饰符已经被弃用。如果使用它则会产生警告。

[LOW_PRIORITY] WRITE lock:

The session that holds the lock can read and write the table.

Only the session that holds the lock can access the table. No other session can access it until the lock is released.

Lock requests for the table by other sessions block while the WRITE lock is held.

The LOW_PRIORITY modifier has no effect. In previous versions of MySQL, it affected locking behavior, but this is no longer true. As of MySQL 5.6.5, it is deprecated and its use produces a warning. Use WRITE without LOW_PRIORITY instead.

解锁表的语法:

 

UNLOCK TABLES

LOCK TABLES为当前会话锁定表。 UNLOCK TABLES释放被当前会话持有的任何锁。官方文档“13.3.5 LOCK TABLES and UNLOCK TABLES Syntax”已经对LOCK TALES与UNLOCK  TABLES做了不少介绍,下面我们通过一些测试例子来深入的理解一下锁表与解锁表的相关知识点。我们先准备一下测试环境用的表和数据。

mysql> create table test( id int, name varchar(12));

Query OK, 0 rows affected (0.07 sec)

 

mysql> insert into test

    -> select 10001, 'kerry'   union all

    -> select 10002, 'richard' union all

    -> select 10003, 'jimmy' ;

Query OK, 3 rows affected (0.05 sec)

Records: 3  Duplicates: 0  Warnings: 0

 

mysql> 

当前会话(会话ID为61)持有test表的READ锁后,那么当前会话只可以读该表,而不能往表中写入数据,否则就会报“Table 'test' was locked with a READ lock and can't be updated”这样的错误。

mysql> select connection_id();

+-----------------+

| connection_id() |

+-----------------+

|              61 |

+-----------------+

1 row in set (0.00 sec)

 

mysql> show open tables where in_use >=1;

Empty set (0.00 sec)

 

mysql> lock tables test read;

Query OK, 0 rows affected (0.00 sec)

 

mysql> show open tables where in_use >=1;

+----------+-------+--------+-------------+

| Database | Table | In_use | Name_locked |

+----------+-------+--------+-------------+

| MyDB     | test  |      1 |           0 |

+----------+-------+--------+-------------+

1 row in set (0.01 sec)

 

mysql> select * from test;

+-------+---------+

| id    | name    |

+-------+---------+

| 10001 | kerry   |

| 10002 | richard |

| 10003 | jimmy   |

+-------+---------+

3 rows in set (0.00 sec)

 

mysql> insert into test

    -> values(10004, 'ken');

ERROR 1099 (HY000): Table 'test' was locked with a READ lock and can't be updated

mysql> 

另外,我们测试一下修饰符LOCAL的用途,如下所示:

mysql> create table test2( id int , name varchar(12)) engine=MyISAM;

Query OK, 0 rows affected (0.05 sec)

 

mysql> insert into test2

    -> select 1001, 'test';

Query OK, 1 row affected (0.00 sec)

Records: 1  Duplicates: 0  Warnings: 0

mysql> select connection_id();

+-----------------+

| connection_id() |

+-----------------+

|              66 |

+-----------------+

1 row in set (0.00 sec)

 

mysql> lock tables test2 read local;

Query OK, 0 rows affected (0.00 sec)

 

mysql> select * from test2;

+------+------+

| id   | name |

+------+------+

| 1001 | test |

+------+------+

1 row in set (0.00 sec)

 

mysql> insert into test2

    -> select 1002, 'kkk';

ERROR 1099 (HY000): Table 'test2' was locked with a READ lock and can't be updated

mysql> 

在其它会话当中,你可以看到表test2可以被插入。当然前提是表的存储引擎不能是InnoDB引擎,否则使用修饰符LOCAL和不用LOCAL是一样的,其它会话无法对表写入。

mysql> select connection_id();

+-----------------+

| connection_id() |

+-----------------+

|              65 |

+-----------------+

1 row in set (0.00 sec)

 

mysql> select * from test2;

+------+------+

| id   | name |

+------+------+

| 1001 | test |

+------+------+

1 row in set (0.00 sec)

 

mysql> insert into test2

    -> select 1002, 'kkk';

Query OK, 1 row affected (0.00 sec)

Records: 1  Duplicates: 0  Warnings: 0

那么其他会话是否也能读此表呢?  其它会话能否也能锁定该表(LOCK TABLES READ)? 其它会话是否也能锁定该表呢?(LOCK TABLES WRITE)

mysql> select connection_id();

+-----------------+

| connection_id() |

+-----------------+

|              62 |

+-----------------+

1 row in set (0.01 sec)

 

mysql> select * from test;

+-------+---------+

| id    | name    |

+-------+---------+

| 10001 | kerry   |

| 10002 | richard |

| 10003 | jimmy   |

+-------+---------+

3 rows in set (0.00 sec)

 

mysql> lock tables test read;

Query OK, 0 rows affected (0.00 sec)

 

mysql> show open tables where in_use >=1;

+----------+-------+--------+-------------+

| Database | Table | In_use | Name_locked |

+----------+-------+--------+-------------+

| MyDB     | test  |      2 |           0 |

+----------+-------+--------+-------------+

1 row in set (0.00 sec)

 

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)

 

mysql> show open tables where in_use >=1;

+----------+-------+--------+-------------+

| Database | Table | In_use | Name_locked |

+----------+-------+--------+-------------+

| MyDB     | test  |      1 |           0 |

+----------+-------+--------+-------------+

1 row in set (0.00 sec)

 

mysql> lock tables test write;

如上测试所示,如果一个会话在一个表上获得一个READ锁后,该会话和所有其他会话只能从表中读。不能往表中写,其它会话也可在该表获取一个READ锁,此时你会在show open tables里面看到in_use的值增加。其实LOCK TABLES READ是一个表锁,而且是共享锁。但是当一个会话获取一个表上的READ锁后,其它会话就不能获取该表的WRITE锁了,此时就会被阻塞,直到持有READ锁的会话释放READ锁。

另外需要注意的是,当前会话如果锁定了其中一个表,那么是无法查询其它表的。否则会报“ERROR 1100 (HY000): Table 'worklog' was not locked with LOCK TABLES”错误。

那么我们再来看看WRITE锁吧。测试前,先在上面两个会话中执行 unlock tables命令。然后获得表TEST上的一个WRITE锁,如下所示,当前会话可以读写表TEST

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)

 

mysql> select connection_id();

+-----------------+

| connection_id() |

+-----------------+

|              61 |

+-----------------+

1 row in set (0.00 sec)

 

mysql> show open tables where in_use >=1;

Empty set (0.00 sec)

 

mysql> lock tables test write;

Query OK, 0 rows affected (0.00 sec)

 

mysql> select * from test;

+-------+---------+

| id    | name    |

+-------+---------+

| 10001 | kerry   |

| 10002 | richard |

| 10003 | jimmy   |

+-------+---------+

3 rows in set (0.00 sec)

 

mysql> update test set name='ken' where id=10003;

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0

 

mysql> 

其它会话无法读写表TEST,都会被阻塞,当然也无法获取表TEST的READ锁或WRITE锁。也就是说当一个会话获得一个表上的一个WRITE锁后,那么只有持锁的会话READ或WRITE表,其他会话都会被阻止。

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)

 

mysql> 

mysql> 

mysql> show open tables where in_use >=1;

+----------+-------+--------+-------------+

| Database | Table | In_use | Name_locked |

+----------+-------+--------+-------------+

| MyDB     | test  |      1 |           0 |

+----------+-------+--------+-------------+

1 row in set (0.00 sec)

 

mysql> select * from test;

mysql> select connection_id();

+-----------------+

| connection_id() |

+-----------------+

|              63 |

+-----------------+

1 row in set (0.00 sec)

 

mysql> show processlist;

+----+------+-----------+------+---------+------+---------------------------------+--------------------+

| Id | User | Host      | db   | Command | Time | State                           | Info               |

+----+------+-----------+------+---------+------+---------------------------------+--------------------+

| 61 | root | localhost | MyDB | Sleep   |   86 |                                 | NULL               |

| 62 | root | localhost | MyDB | Query   |   40 | Waiting for table metadata lock | select * from test |

| 63 | root | localhost | MyDB | Query   |    0 | init                            | show processlist   |

| 64 | root | localhost | MyDB | Sleep   | 2551 |                                 | NULL               |

+----+------+-----------+------+---------+------+---------------------------------+--------------------+

4 rows in set (0.00 sec)

UNLOCK TABLES释放被当前会话持有的任何锁,但是当会话发出另外一个LOCK TABLES时,或当服务器的连接被关闭时,当前会话锁定的所有表会隐式被解锁。下面我们也可以测试看看

mysql> lock tables test read;

Query OK, 0 rows affected (0.00 sec)

 

mysql> show open tables where in_use >=1;

+----------+-------+--------+-------------+

| Database | Table | In_use | Name_locked |

+----------+-------+--------+-------------+

| MyDB     | test  |      1 |           0 |

+----------+-------+--------+-------------+

1 row in set (0.00 sec)

 

mysql> lock tables worklog read;

Query OK, 0 rows affected (0.00 sec)

 

mysql> show open tables where in_use >=1;

+----------+---------+--------+-------------+

| Database | Table   | In_use | Name_locked |

+----------+---------+--------+-------------+

| MyDB     | worklog |      1 |           0 |

+----------+---------+--------+-------------+

1 row in set (0.00 sec)

 

mysql> 

那么我们如何在当前会话锁定多个表呢?如下所示:

mysql> show open tables where in_use >=1;

Empty set (0.00 sec)

 

mysql> lock tables test read, worklog read;

Query OK, 0 rows affected (0.00 sec)

 

mysql> show open tables where in_use >=1;

+----------+---------+--------+-------------+

| Database | Table   | In_use | Name_locked |

+----------+---------+--------+-------------+

| MyDB     | worklog |      1 |           0 |

| MyDB     | test    |      1 |           0 |

+----------+---------+--------+-------------+

2 rows in set (0.00 sec)

 

mysql> 

另外,还有一些细节问题,LOCK TABLES是否可以为视图、触发器、临时表加锁呢?

mysql> create table test2( id int, sex bit);

Query OK, 0 rows affected (0.06 sec)

 

mysql> insert into test2

    -> select 10001, 1 union all

    -> select 10002, 0 union all

    -> select 10003, 1;

Query OK, 3 rows affected (0.02 sec)

Records: 3  Duplicates: 0  Warnings: 0

mysql> create view v_test

    -> as

    -> select t1.id, t1.name, t2.sex

    -> from test t1 left join test2 t2 on t1.id =t2.id;

Query OK, 0 rows affected (0.01 sec)

mysql> lock tables v_test read;

Query OK, 0 rows affected (0.00 sec)

 

mysql> show open tables where in_use >=1;

+----------+-------+--------+-------------+

| Database | Table | In_use | Name_locked |

+----------+-------+--------+-------------+

| MyDB     | test2 |      1 |           0 |

| MyDB     | test  |      1 |           0 |

+----------+-------+--------+-------------+

2 rows in set (0.00 sec)

 

mysql> 

如上测试所示,对于VIEW加锁,LOCK TABLES语句会为VIEW中使用的所有基表加锁。对触发器使用LOCK TABLE,那么就会锁定触发器中所包含的全部表(any tables used in triggers are also locked implicitly)

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)

mysql> create temporary table tmp like test;

Query OK, 0 rows affected (0.04 sec)

mysql> show open tables where in_use >=1;

Empty set (0.00 sec)

mysql> select database();

+------------+

| database() |

+------------+

| MyDB       |

+------------+

1 row in set (0.00 sec)

mysql> select * from tmp;

Empty set (0.00 sec)

mysql> insert into tmp

-> select 1001, 'kerry' ;

Query OK, 1 row affected (0.01 sec)

Records: 1  Duplicates: 0  Warnings: 0

mysql>

LOCK TABLES 与 UNLOCK TABLES只能为自己获取锁和释放锁,不能为其他会话获取锁,也不能释放由其他会话保持的锁。一个对象获取锁,需具备该对象上的SELECT权限和LOCK TABLES权限。LOCK TABLES语句为当前会话显式的获取表锁。最后,关于LOCK TABLES与事务当中锁有那些异同,可以参考官方文档13.3.5.1 Interaction of Table Locking and Transactions

LOCK TABLES and UNLOCK TABLES interact with the use of transactions as follows:

·         LOCK TABLES is not transaction-safe and implicitly commits any active transaction before attempting to lock the tables.

·         UNLOCK TABLES implicitly commits any active transaction, but only if LOCK TABLES has been used to acquire table locks. For example, in the following set of statements,UNLOCK TABLES releases the global read lock but does not commit the transaction because no table locks are in effect:

 

MySQL中lock tables和unlock tables浅析的更多相关文章

  1. MySQL的lock tables和unlock tables的用法(转载)

    早就听说lock tables和unlock tables这两个命令,从字面也大体知道,前者的作用是锁定表,后者的作用是解除锁定.但是具体如何用,怎么用,不太清楚.今天详细研究了下,总算搞明白了2者的 ...

  2. lock tables和unlock tables

    1.lock tables table1 read,table2 read,table3 read igoodful@a8-apple-iphone-db00.wh(glc) > show ta ...

  3. LOCK TABLES和UNLOCK TABLES与Transactions的交互

    LOCK TABLES对事务不安全,并且在试图锁定表之前隐式提交任何活动事务. UNLOCK TABLES只有在LOCK TABLES已经获取到表锁时,会隐式提交任何活动事务.对于下面的一组语句,UN ...

  4. 14.3.5 LOCK TABLES and UNLOCK TABLES Syntax

    14.3.5 LOCK TABLES and UNLOCK TABLES Syntax LOCK TABLES tbl_name [[AS] alias] lock_type [, tbl_name ...

  5. LOCK TABLES 和 UNLOCK TABLES

    MySQLdump的时LOCK TABLES 和 UNLOCK TABLES 在mysqldump后的数据中会发现有 LOCK TABLES tables_name WRITE;和结尾处有 UNLOC ...

  6. mysql中lock tables与unlock tables

    官网:https://dev.mysql.com/doc/refman/5.0/en/lock-tables.html LOCK TABLES tbl_name [[AS] alias] lock_t ...

  7. mysql中lock tables与unlock tables(锁表/解锁)使用总结

    php mysql lock tables 使用有感 mysql 的 表锁 lock tables 感觉就像一个 封闭的空间 mysql发现 lock tables 命令的时候,会将带有锁标记的表(t ...

  8. MySQL LOCK TABLES 与UNLOCK TABLES

    http://blog.csdn.net/zyz511919766/article/details/16342003 1语法 LOCK TABLES tbl_name[[AS] alias] lock ...

  9. MySQL中lock与latch的区分

    这里要区分锁中容易令人混淆的概念lock与latch.在数据库中,lock与latch都可以成为锁,但两者有截然不同的含义 latch 一般称为闩锁(轻量级的锁) 因为其要求锁定的时间非常短,若迟勋时 ...

随机推荐

  1. (转)python生态环境简介

    Python生态环境简介 作者: Mir Nazim 原文: Python Ecosystem - An Introduction 译者: dccrazyboy  原译: Python生态环境简介 当 ...

  2. Python快速学习08:模块的操作

    前言 系列文章:[传送门] 天气干燥,我就上火,流鼻血.希望身子好起来. 正文 函数和对象都是为了更好的组织已经有的程序,以方便重复利用. 模块(module)也是为了同样的目的.模块可以包含可执行代 ...

  3. solr(六): 集群

    前言 随着用户的增多,空间和并发量越来越多,会导致一台solr服务器干不过了.这时候,就需要将solr集群以下. 集群架构 由多台服务器共同完成索引和搜索任务 实现的思路是将索引数据进行shard(分 ...

  4. 使用Laya引擎开发微信小游戏(上)

    本文由云+社区发表 使用一个简单的游戏开发示例,由浅入深,介绍了如何用Laya引擎开发微信小游戏. 作者:马晓东,腾讯前端高级工程师. 微信小游戏的推出也快一年时间了,在IEG的游戏运营活动中,也出现 ...

  5. 使用Asp.Net Core MVC 开发项目实践[第四篇:基于EF Core的扩展2]

    上篇我们说到了基于EFCore的基础扩展,这篇我们讲解下基于实体结合拉姆达表达式的自定义更新以及删除数据. 先说下原理:其实通过实体以及拉姆达表达式生成SQL语句去执行 第一种更新扩展: 自定义更新字 ...

  6. Perl模块管理

    Perl模块管理 perl有自带的模块,还有第三方模块.自带的模块是随perl安装而安装好的,第三方模块需要从CPAN(Comprehensive Perl Archive Network)上下载并安 ...

  7. 分布式系统监视zabbix讲解八之自动发现/自动注册--技术流ken

    自动发现(LLD) 概述 自动发现(LLD)提供了一种在计算机上为不同实体自动创建监控项,触发器和图形的方法.例如,Zabbix可以在你的机器上自动开始监控文件系统或网络接口,而无需为每个文件系统或网 ...

  8. Startup在不同环境中的处理

    ASP.NET Core引进了在多种环境中对控制应用程序行为的进一步支持,例如开发环境(Development Environment).预发布环境(Staging Environment),和生产环 ...

  9. mysqlslap 压力测试使用总结

    今天从运维同事那听说了mysql压力测试工具mysqlslap.经了解其实mysql自带就有一个叫mysqlslap的压力测试工具,还是模拟的不错的.下面举例说说.mysqlslap是从5.1.4版开 ...

  10. 快乐的一天从JAVA第一课开始,生活美滋滋!!!

    ---恢复内容开始--- 学JAVA第一天 今天稀里糊涂就把JAVA环境配好了 现在回想一下,吧环境跟大家分享一下…… 第一步:下载         JAVA(推荐使用谷歌浏览器,因为谷歌浏览器右键点 ...