alter table锁表,MySQL出现Waiting for table metadata lock的场景浅析及解决方案

在修改/增加表字段的时候,发现很慢,

show processlist; 时, Waiting for table metadata lock 能一直锁很久。

官网的一段话,可以理解下

http://dev.mysql.com/doc/refman/5.5/en/metadata-locking.html

8.10.4. Metadata Locking MySQL 5.5.3 and up uses metadata locking to manage access to objects (tables, triggers, and so forth). Metadata locking is used to ensure data consistency but does involve some overhead, which increases as query volume increases. Metadata contention increases the more that multiple queries attempt to access the same objects. Metadata locking is not a replacement for the table definition case, and its mutxes and locks differ from the LOCK_open mutex. The following discussion provides some information about how metadata locking works. To ensure transaction serializability, the server must not permit one session to perform a data definition language (DDL) statement on a table that is used in an uncompleted transaction in another session. The server achieves this by acquiring metadata locks on tables used within a transaction and deferring release of those locks until the transaction ends. A metadata lock on a table prevents changes to the table's structure. This locking approach has the implication that a table that is being used by a transaction within one session cannot be used in DDL statements by other sessions until the transaction ends. This principle applies not only to transactional tables, but also to nontransactional tables. Suppose that a session begins a transaction that uses transactional table t and nontransactional table nt as follows: START TRANSACTION; SELECT * FROM t; SELECT * FROM nt; Metadata locks are held on both t and nt until the transaction ends. If another session attempts a DDL operation on either table, it blocks until metadata lock release at transaction end. For example, a second session blocks if it attempts any of these operations: DROP TABLE t; ALTER TABLE t ...; DROP TABLE nt; ALTER TABLE nt ...; If the server acquires metadata locks for a statement that is syntactically valid but fails during execution, it does not release the locks early. Lock release is still deferred to the end of the transaction because the failed statement is written to the binary log and the locks protect log consistency. In autocommit mode, each statement is in effect a complete transaction, so metadata locks acquired for the statement are held only to the end of the statement. Metadata locks acquired during a PREPARE statement are released once the statement has been prepared, even if preparation occurs within a multiple-statement transaction. Before MySQL 5.5.3, when a transaction acquired the equivalent of a metadata lock for a table used within a statement, it released the lock at the end of the statement. This approach had the disadvantage that if a DDL statement occurred for a table that was being used by another session in an active transaction, statements could be written to the binary log in the wrong order

一个没提交的事务使用了A表, 另外一个session 对A表进行alter,出现waiting for table metadata lock


MySQL版本为5.6.12。

在进行alter table操作时,有时会出现Waiting for table metadata lock的等待场景。而且,一旦alter table TableA的操作停滞在Waiting for table metadata lock的状态,后续对TableA的任何操作(包括读)都无法进行,也会在Opening tables的阶段进入Waiting for table metadata lock的队列。如果是产品环境的核心表出现了这样的锁等待队列,就会造成灾难性的后果。

造成alter table产生Waiting for table metadata lock的原因其实很简单,一般是以下几个简单的场景:

场景一:

通过show processlist可以看到TableA上有正在进行的操作(包括读),此时alter table语句无法获取到metadata 独占锁,会进行等待。 这是最基本的一种情形,这个和mysql 5.6中的online ddl并不冲突。一般alter table的操作过程中(见下图),在after create步骤会获取metadata 独占锁,当进行到altering table的过程时(通常是最花时间的步骤),对该表的读写都可以正常进行,这就是online ddl的表现,并不会像之前在整个alter table过程中阻塞写入。(当然,也并不是所有类型的alter操作都能online的,具体可以参见官方手册:http://dev.mysql.com/doc/refman/5.6/en/innodb-create-index-overview.html)

场景二:

通过show processlist看不到TableA上有任何操作,但实际上存在有未提交的事务,可以在information_schema.innodb_trx中查看到。在事务没有完成之前,TableA上的锁不会释放,alter table同样获取不到metadata的独占锁。

场景三:

通过show processlist看不到TableA上有任何操作,在information_schema.innodb_trx中也没有任何进行中的事务。这很可能是因为在一个显式的事务中,对TableA进行了一个失败的操作(比如查询了一个不存在的字段),这时事务没有开始,但是失败语句获取到的锁依然有效。从performance_schema.events_statements_current表中可以查到失败的语句。

官方手册上对此的说明如下:

If the server acquires metadata locks for a statement that is syntactically valid but fails during execution, it does not release the locks early. Lock release is still deferred to the end of the transaction because the failed statement is written to the binary log and the locks protect log consistency. 也就是说除了语法错误,其他错误语句获取到的锁在这个事务提交或回滚之前,仍然不会释放掉。because the failed statement is written to the binary log and the locks protect log consistency 但是解释这一行为的原因很难理解,因为错误的语句根本不会被记录到二进制日志。

总之,alter table的语句是很危险的,在操作之前最好确认对要操作的表没有任何进行中的操作、没有未提交事务、也没有显式事务中的报错语句。如果有alter table的维护任务,在无人监管的时候运行,最好通过lock_wait_timeout设置好超时时间,避免长时间的metedata锁等待。

参考:

http://ctripmysqldba.iteye.com/blog/1938150

http://blog.csdn.net/cloudcraft/article/details/9343069

原文发布于微信公众号 - 马哥Linux运维(magedu-Linux)

原文发表时间:2015-01-12

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏程序你好

MySQL数据库面试题和答案(一)

23230
来自专栏性能与架构

Mysql 8.0 新增特性

1. 数据字典 新增了事务型的数据字典,用来存储数据库对象信息 之前,字典数据是存储在元数据文件和非事务型表中的 2. 账号权限管理 添加了对 “角色” 的支持...

387110
来自专栏大学生计算机视觉学习DeepLearning

新手 php连接数据库大概。简单过程浅析以及遇到的问题分析

44560
来自专栏友弟技术工作室

Beego Models之四模型定义

使用orm定义,然后使用cmd方式,自动建表,不过在实际生产中还是直接使用sql操作的,这种模型定义在生产环境中定义的比较少,基本上都是直接使用基本类型,一些特...

31620
来自专栏魏艾斯博客www.vpsss.net

MySQL 数据库类型从 InnoDB 转换为 MyISAM

魏艾斯博客有一个 wordpress 站点,有一天无意中发现数据库挺大的,可是这个站也就不到 10 篇文章,没道理这么大的数据库啊。然后开始查找原因,发现在 p...

532220
来自专栏大学生计算机视觉学习DeepLearning

新手 php连接数据库大概。简单过程浅析以及遇到的问题分析

17260
来自专栏我和PYTHON有个约会

33.企业级开发进阶6:数据库操作

python作为一个编程语言,在开发B/S或者C/S结构的软件时,不可避免的会设计到和数据库之间的交互操作,和其他高级的面向对象的语言一样,Python在操作数...

10820
来自专栏余林丰

再说单例模式的线程安全问题

今天和同事聊起了单例模式的线程安全,我说如果不做任何措施,单例模式在多线程下是不安全的,得到的“单例”实际上并不是单例。但是为什么不是单例呢?由此我上网查了一下...

20860
来自专栏农夫安全

注入学习之sqli-labs-2(第一关)

前言 从这一节开始,一个个关卡来学习sql注入 从最基础的sql语句开始 环境配置完成后,还有一个步骤需要做,要安装一下sqli数据库,环境配置那节课忘了说了,...

46380
来自专栏同步博客

MySQL注入与防御

  在一个应用中,数据的安全无疑是最重要的。数据的最终归宿都是数据库,因此如何保证数据库不被恶意攻击者入侵是一项重要且严肃的问题!

23420

扫码关注云+社区

领取腾讯云代金券