Home > Database > Mysql Tutorial > body text

显示 metadata lock 信息的 mysql 插件

WBOY
Release: 2016-06-07 16:38:34
Original
1064 people have browsed it

有一回碰上在 mysql 5.5 中,一些表没法执行 DDL ,会一直被阻塞

有一回碰上在 mysql 5.5 中,一些表没法执行 DDL ,会一直被阻塞住。show processlist 会看到

mysql> show full processlist;
+----+------+-----------------+------+---------+------+---------------------------------+-----------------------+
| Id | User | Host            | db   | Command | Time | State                           | Info                  |
+----+------+-----------------+------+---------+------+---------------------------------+-----------------------+
...
|  6 | root | localhost:56841 | test | Query   |    0 | init                            | show full processlist |
|  7 | root | localhost:56843 | test | Query   |   13 | Waiting for table metadata lock | rename table u to uu  |
...
+----+------+-----------------+------+---------+------+---------------------------------+-----------------------+
Copy after login

可以看出,是被一个 metadata lock 阻塞住了。但是从 process list 里是没法看出是哪个线程持有了这个锁。用别的方式,也只能得到通过 lock table 方式获取的锁或 innodb 事务的信息。对于一些比较隐蔽的方式,比如

mysql> begin;
mysql> select * from test.u;
Copy after login

这样也同样会获取 metadata lock 。在 autocommit 为 0 的时候,没有显式 begin 也是如此。这类情况没法从任何途径获取究竟是谁上了锁。

于是我就试着做了一个 mysql 的 information schema 插件。 https://github.com/xiezhenye/mysql-plugin-mdl-lock 安装后,只要 select * from information_schema.MDL_LOCKS 就可以获取到所有 metadata lock ,也就是 mdl lock 的信息。

mysql> select * from information_schema.MDL_LOCKS;
+-----------+-------------+---------------------+-----------+----------+------+
| THREAD_ID | DURATION    | TYPE                | NAMESPACE | DATABASE | NAME |
+-----------+-------------+---------------------+-----------+----------+------+
|         7 | STATEMENT   | INTENTION_EXCLUSIVE | GLOBAL    |          |      |
|         7 | TRANSACTION | SHARED_WRITE        | TABLE     | test     | t    |
+-----------+-------------+---------------------+-----------+----------+------+
2 rows in set (0.00 sec)
Copy after login

在写这个插件的时候,发现 mysql 把 mdl lock 的信息封装的相当严密,即使是写插件也没法用常规途径获取。只好用了一些很恶心的技巧。如果各位有碰到类似的问题,也可以先在测试环境试试。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template