最近经常遇到mysql数据库死锁,郁闷死,
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

在insert into t select * from share 运行时, 同时执行alter table t add index(play_count),

alter table语句会Waiting for table metadata lock, 直到insert into … select 语句结束。

不是传说5.6支持online DDL么? 怎么还会Waiting for table metadata lock?

后来想想, online DDL应该是指在alter table进行的时候, 插入/修改/删除数据的sql语句不会Waiting for table metadata lock.

MySQL 5.6 enhances many other types OF ALTER TABLE operations TO avoid copying the TABLE. 

Another enhancement allows SELECT queries AND INSERT, UPDATE, AND DELETE (DML) statements TO proceed while the TABLE IS being altered. 

This combination OF features IS now known AS online DDL.

那么就让alter table wait去吧。

后来又发现另外一个神奇的事:

mysql [localhost] {msandbox} (spc) > SHOW processlist;

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

| Id | USER     | Host      | db   | Command | TIME | State                           | Info                                |

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

|  5 | msandbox | localhost | spc  | Query   |    1 | Waiting FOR TABLE metadata LOCK | ALTER TABLE t ADD INDEX(play_count) |

|  8 | msandbox | localhost | spc  | Query   |    3 | USER sleep                      | SELECT sleep(100) FROM t            |

| 10 | msandbox | localhost | spc  | Query   |    0 | init                            | SHOW processlist                    |

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

重启后再试一次:

mysql [localhost] {msandbox} (spc) > SHOW processlist;

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

| Id | USER     | Host      | db   | Command | TIME | State                           | Info                                |

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

|  1 | msandbox | localhost | spc  | Query   |  129 | USER sleep                      | SELECT sleep(100) FROM t            |

|  2 | msandbox | localhost | spc  | Query   |  102 | Waiting FOR TABLE metadata LOCK | ALTER TABLE t DROP INDEX play_count |

|  3 | msandbox | localhost | spc  | Query   |    0 | init                            | SHOW processlist                    |

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

这个sleep的时间。。。已经超过100秒了…

结论:

在准备alter table tbl 的时候,先观察一下,有没有正在运行的,且在短时间内无法结束的sql语句在操作tbl表

mysql出现Waiting for table metadata lock的原因及解决方案的更多相关文章

  1. MySQL出现Waiting for table metadata lock的原因以及解决方法

    转自:http://ctripmysqldba.iteye.com/blog/1938150 (有修改) MySQL在进行alter table等DDL操作时,有时会出现Waiting for tab ...

  2. MySQL出现Waiting for table metadata lock的原因以及解决方法(转)

    MySQL在进行alter table等DDL操作时,有时会出现Waiting for table metadata lock的等待场景.而且,一旦alter table TableA的操作停滞在Wa ...

  3. 记一次MySQL出现Waiting for table metadata lock的原因、排查过程与解决方法

    任务背景:将sql文件通过shell直接导入到mysql中执行(还原) bug表现:导入后java项目卡死 过程: 1.网上乱搜一通,无意间看到一篇文章,这篇文章说明了如何开启mysql的genera ...

  4. 【转】【MySql】Waiting for table metadata lock原因分析

    MySQL在进行alter table等DDL操作时,有时会出现Waiting for table metadata lock的等待场景.而且,一旦alter table TableA的操作停滞在Wa ...

  5. MySQL出现Waiting for table metadata lock的场景浅析

    MySQL版本为5.6.12. 在进行alter table操作时,有时会出现Waiting for table metadata lock的等待场景.而且,一旦alter table TableA的 ...

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

    在修改/增加表字段的时候,发现很慢, show processlist; 时, Waiting for table metadata lock 能一直锁很久. 官网的一段话,可以理解下 http:// ...

  7. 20180117MySQL出现Waiting for table metadata lock的原因以及解决方法

    转自http://www.cnblogs.com/digdeep/p/4892953.html 转自:http://ctripmysqldba.iteye.com/blog/1938150 (有修改) ...

  8. 记一次MySQL中Waiting for table metadata lock问题的处理

    起因:由于需要,要把一张表的一个字段从不是 null 改成 可null,我用的Navicat Premium ,但是在保存的时候,工具无响应了,几个同事操作都是这样的,很奇怪,怀疑是不是由于表被锁了还 ...

  9. 记一次MySQL中Waiting for table metadata lock的解决方法

    最近项目中的数据库查询经常挂起,应用程序启动后也报操作超时.测试人员就说数据库又挂了(貌似他们眼中的连接失败,查询无果都是挂了),通过 show processlist 一看,满屏都是 Waiting ...

随机推荐

  1. UISearchBar去除底部黑线问题

    有时我们在设置搜索框的时候底部会出现一条黑线,要 去除这黑线只需设置. [self.searchBar setBackgroundImage:[UIImage new]];

  2. [HDU 1428]--漫步校园(记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1428 漫步校园 Time Limit: 2000/1000 MS (Java/Others)    M ...

  3. 【转】LINUX下一款不错的网站压力测试工具webbench

    原文链接:http://blog.csdn.net/xinqingch/article/details/8618704 安装: wget http://blog.s135.com/soft/linux ...

  4. jq商品展示图放大镜 and 原生js和html5写的放大镜效果 ~~效果不错

    <!DOCTYPE HTML><html lang="en-US"><head> <meta charset="UTF-8&qu ...

  5. HDU 4739 求正方形个数

    九野的博客,转载请注明出处:http://blog.csdn.net/acmmmm/article/details/11711707 求所有可能围成的正方形,借个代码 #include <que ...

  6. 汉高澳大利亚sinox为什么不能下载源代码,因为sinox执行unix/linux/windows规划

    中国用户下载真正的澳大利亚sinox说完后sinox没有下载源代码. 这意味着,类似linux如下载linux 开源安装. 要知道.sinox并非linux. 首先,sinox是商业操作系统,就像 w ...

  7. Delphi发送邮件...

    ///首先在控件栏定位到:Indy Clients加入控件IdSMTP ///再定位到:Indy Misc加入控件IdMessage ///发送邮件函数 procedure TForm1.SendMa ...

  8. Sql 参数的使用

    代码片段: a)声明实例 1.声明SQLCommand实例. SqlCommand cmd = new SqlCommand(); 2.声明SqlDataAdapter实例. SqlDataAdapt ...

  9. C++逗号运算符与逗号表达式

    C++将赋值表达式作为表达式的一种,使赋值操作不仅可以出现在赋值语句中,而且可以以表达式形式出现在其他语句(如输出语句.循环语句等)中.这是C++语言灵活性的一种表现. 请注意,用cout语句输出一个 ...

  10. UISearchBar控件

    摘自:http://blog.sina.com.cn/s/blog_7b9d64af0101dfg8.html UISearchBar控件就是要为你完成搜索功能的一个专用控件.它集成了很多你意想不到的 ...