Home > Database > Mysql Tutorial > body text

How to Identify the Culprit Transaction Causing \'Waiting for Table Metadata Lock\' in MySQL?

Linda Hamilton
Release: 2024-10-30 17:23:25
Original
886 people have browsed it

How to Identify the Culprit Transaction Causing

Investigating the Source of "Waiting for Table Metadata Lock"

In MySQL, encountering a "Waiting for table metadata lock" message during DDL queries can be frustrating. This state often indicates that another transaction is holding a lock on the affected table, preventing the current transaction from proceeding.

Identifying the Culprit Transaction

To pinpoint which transaction is responsible for this hold, consider using the following approaches:

1. MySQL v5.5.24 and Below

For MySQL versions prior to 5.7.3, execute the following command:

SHOW ENGINE INNODB STATUS \G
Copy after login

Inspect the "TRANSACTIONS" section to identify the offending transaction.

2. Information Schema Tables

All Waiting Locks:

SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS;
Copy after login

Blocking Transactions:

SELECT *
FROM INFORMATION_SCHEMA.INNODB_LOCKS
WHERE LOCK_TRX_ID IN (SELECT BLOCKING_TRX_ID FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS);
Copy after login

Locks on a Specific Table:

SELECT *
FROM INFORMATION_SCHEMA.INNODB_LOCKS
WHERE LOCK_TABLE = db_name.table_name;
Copy after login

Transactions Waiting for Locks:

SELECT TRX_ID, TRX_MYSQL_THREAD_ID, TRX_QUERY
FROM INFORMATION_SCHEMA.INNODB_TRX
WHERE TRX_STATE = 'LOCK WAIT';
Copy after login

Additional Reference:

For a comprehensive guide on troubleshooting this issue, refer to "MySQL Troubleshooting: What To Do When Queries Don't Work," Chapter 6, Page 96.

The above is the detailed content of How to Identify the Culprit Transaction Causing \'Waiting for Table Metadata Lock\' in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!