MySQL : 永久获取 "等待表元数据锁"

2022-01-02 00:00:00 process blocking mysql

我的 MySQL 数据库为三个 web 应用程序提供存储后端.但是我最近经常遇到错误等待表元数据锁定".它几乎一直发生,我不明白为什么.

My MySQL database serves three webapps as the storage backend. However I recently encounter permanantly the error "Waiting for table metadata lock". It happen nearly all the time and I do not understand why.

mysql> show processlist
    -> ;
+------+-----------+-----------------+------------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+
| Id   | User      | Host            | db         | Command | Time | State                           | Info                                                                                                 |
+------+-----------+-----------------+------------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+
|   36 | root      | localhost:33444 | bookmaker2 | Sleep   |  139 |                                 | NULL                                                                                                 |
|   37 | root      | localhost:33445 | bookmaker2 | Sleep   |  139 |                                 | NULL                                                                                                 |
|   38 | root      | localhost:33446 | bookmaker2 | Sleep   |  139 |                                 | NULL                                                                                                 |
|   39 | root      | localhost:33447 | bookmaker2 | Sleep   |   49 |                                 | NULL                                                                                                 |
|   40 | root      | localhost:33448 | bookmaker2 | Sleep   |  139 |                                 | NULL                                                                                                 |
| 1315 | bookmaker | localhost:34869 | bookmaker  | Sleep   |   58 |                                 | NULL                                                                                                 |
| 1316 | root      | localhost:34874 | bookmaker3 | Sleep   |   56 |                                 | NULL                                                                                                 |
| 1395 | bookmaker | localhost:34953 | bookmaker  | Sleep   |   58 |                                 | NULL                                                                                                 |
| 1396 | root      | localhost:34954 | bookmaker3 | Sleep   |   46 |                                 | NULL                                                                                                 |
| 1398 | root      | localhost:34956 | bookmaker3 | Query   |   28 | Waiting for table metadata lock | CREATE TABLE IF NOT EXISTS LogEntries  ( 
                    lid         INT NOT NULL AUTO_INCREMEN |
| 1399 | root      | localhost       | NULL       | Query   |    0 | NULL                            | show processlist                                                                                     |
+------+-----------+-----------------+------------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+

当然可以杀掉相应的进程.但是,如果我重新启动试图创建我的数据库bookmaker3"的表结构的程序,新创建的进程将再次在 Metalock 中结束.

Of course one can kill the corresponding process. However if I restart my program that tries to create the table structure of my database "bookmaker3" the newly created process ends up again in a metalock.

我什至无法删除数据库:

I even cannot drop the database:

mysql> drop database bookmaker3;

这也产生了一个金属.

如何修复?

推荐答案

用锁杀死连接

Kill 1398

然后检查您是否将 autocommit 设置为 0

Then check if you have autocommit set to 0 by

select @@autocommit;

如果是,您可能忘记提交事务.然后另一个连接想要对这个表做一些事情,导致锁定.

If yes, you propably forgot to commit transaction. Then another connection want to do something with this table, which causes the lock.

在您的情况下:如果您对 LogEntries 进行了一些查询(其中存在)并且没有提交它,那么您尝试从另一个连接执行 CREATE TABLE IF NOT EXISTS - 发生元数据锁定.

In your case: If you made some query to LogEntries (whitch exists) and did not commit it, then you try to execute CREATE TABLE IF NOT EXISTS from another connection - metadata lock happens.

编辑对我来说,错误在您的应用程序中.检查那里,或者如果您不在应用程序中使用事务,则将 autocommit 设置为 1.

edit For me the bug is somewhere at your application. Check there, or set autocommit to 1 if your not using transactions in application.

ps 也检查这个帖子:

ps also check this posts:

  • 不是 bugs.mysql 上的错误
  • 复制 MyISAM 和INNOdb 表

相关文章