MySQL Error 1213: Deadlock Found When Trying to Get Lock

MySQL error 1213 (Deadlock Found When Trying to Get Lock) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.

MySQL Error Code 1213 MySQL Last verified 2026-08-19

Quick Answer

MySQL rolls back one transaction automatically (the 'deadlock victim') — retry it whole. If that does not apply, the official guidance: restart the transaction that lost the deadlock — the full checklist is below.

Error Code

MySQL Error Code: 1213
Official name: Deadlock Found When Trying to Get Lock
Service: MySQL

What does this error mean?

Deadlock found when trying to get lock; try restarting transaction

Common Causes

How to Fix

  1. MySQL rolls back one transaction automatically (the 'deadlock victim') — retry it whole
  2. The official guidance: restart the transaction that lost the deadlock
  3. Standardize the order of updates so every transaction touches rows in the same sequence
  4. Keep transactions short to shrink the lock footprint
  5. Reduce locking with appropriate indexes; consider lowering isolation if semantics allow
  6. Look at 'LATEST DETECTED DEADLOCK' in SHOW ENGINE INNODB STATUS for the exact lock cycle

Code Examples

Read the deadlock report sql
SHOW ENGINE INNODB STATUS;
-- 'LATEST DETECTED DEADLOCK' shows both transactions and the locks

Pinpoints the two statements that deadlocked so you can order them consistently.

Framework-Specific Fixes

nodejs-mysql2

Catch ER_LOCK_DEADLOCK and retry the whole transaction a bounded number of times.

for (let attempt = 0; attempt < 3; attempt++) {
  try {
    await runTransfer(pool, fromId, toId, amount)
    break
  } catch (e) {
    if (e.code !== 'ER_LOCK_DEADLOCK') throw e
    // else retry the full transaction
  }
}
python-sqlalchemy

Retry on the 1213 error code with a small backoff.

import time
for attempt in range(3):
    try:
        with session.begin(): transfer(session, a, b, n)
        break
    except OperationalError as e:
        if e.orig.args[0] != 1213: raise
        time.sleep(0.05 * attempt)
php-pdo

Re-run the transaction if it loses the deadlock race.

for ($i = 0; $i < 3; $i++) {
  try {
    $pdo->beginTransaction();
    // transfer...
    $pdo->commit(); break;
  } catch (PDOException $e) {
    if ($e->errorInfo[1] !== 1213) throw $e;
  }
}

You Might Also Like

Frequently Asked Questions

Why am I seeing MySQL error 1213?

Most often this happens when two or more transactions wait on each other's locks, forming a cycle, or when transactions update the same rows in different orders (A->B vs B->A).

How do I fix MySQL error 1213?

MySQL rolls back one transaction automatically (the 'deadlock victim') — retry it whole.

Which frameworks have documented fixes for error 1213?

This page documents fixes for: nodejs-mysql2, python-sqlalchemy, php-pdo.

Official Sources

This page is based on the official MySQL documentation linked below and adds practical troubleshooting guidance on top.