MySQL “waiting for table metadata lock” (2025): diagnose, unblock, prevent
Understand MySQL Metadata Locks (MDL), quickly find blockers, release waits safely, and keep them from returning with better DDL practices, short transactions, and online schema changes.
Typical cause
Long transactions
Resolution speed
Minutes, if known
Prevention
Process & DDL
What is a metadata lock (MDL)?
MySQL protects table definitions with Metadata Locks (MDL). Readers typically take shared MDL; DDL needs stronger locks. If a long transaction holds a shared MDL, an ALTER/DROP/RENAME may block with “waiting for table metadata lock”.
Symptoms & common causes
Symptoms
What you’ll see
- ALTER TABLE stuck in “waiting for table metadata lock”
- High concurrency + rising Threads_running
- Slow or stalled deploys/migrations
Common causes
Why it happens
- Long transactions holding shared MDL (open transactions, idle in transaction)
- Long SELECTs (reporting, unbounded scans)
- Explicit locks:
LOCK TABLES, ongoing DDL
Diagnose (queries & views)
Find waiters & blockers
schema_table_lock_waits
SELECT * FROM sys.schema_table_lock_waits ORDER BY waiting_query_secs DESC;
Shows waiting/locking thread IDs, queries, and table names.
Unblock safely — runbook
1) Identify blocker
Confirm owner & impact
SELECT * FROM sys.schema_table_lock_waits ORDER BY waiting_query_secs DESC;
Capture blocking thread_id and SQL; notify owner if possible.
2) End minimal work
Prefer KILL QUERY
-- Safer: stop only the current statement KILL QUERY <thread_id>; -- Last resort: drop the whole connection (rolls back tx) KILL <thread_id>;
If the blocker is your DDL
- Cancel the DDL (
KILL QUERY), re-run during a change window. - Prefer online/instant ALTERs in MySQL 8 (
ALGORITHM=INPLACE/ALGORITHM=INSTANTwhen supported). - Use online tools like gh-ost or pt-online-schema-change for busy tables.
Prevent future waits
Short transactions
Design & coding
- Autocommit for read-only sessions
- Avoid “idle in transaction” (close cursors, commit promptly)
- Split long reports into batches or replicas
Safer schema changes
Operational practices
- Schedule DDL windows; announce freezes
- Use
ALGORITHM=INSTANT/INPLACEwhere possible (MySQL 8) - Apply gh-ost / pt-online-schema-change for heavy tables
FAQ
Stabilize schema changes — prevent MDL incidents
Keep transactions short, schedule DDL windows, and use online schema tools. When sharing incident docs, scrub image metadata first.