mysqldude
Error codes › 1213

MySQL & MariaDB error 1213: Deadlock found when trying to get lock; try restarting transaction

Error messageDeadlock found when trying to get lock; try restarting transaction
Example from our test runs: Deadlock found when trying to get lock; try restarting transaction (5.6.51)

Two transactions each held a lock the other needed. InnoDB detected the cycle and rolled back one of them (yours) so the other could finish.

MySQLMariaDB
SymbolER_LOCK_DEADLOCKER_LOCK_DEADLOCK
SQLSTATE4000140001
TypeServer errorServer error
Versionsevery release since 5.0 activeevery release since 5.5 active

What it means

A deadlock is a cycle: transaction A holds row 1 and wants row 2, transaction B holds row 2 and wants row 1. Neither can continue, so InnoDB picks a victim (usually the one with less work done), rolls back its whole transaction and returns 1213. The other transaction proceeds normally.

Occasional deadlocks are normal in busy systems. The application is expected to retry the transaction.

Why it happens

  • Transactions touch the same rows in different orders (the classic money-transfer example in our reproduction).
  • Gap and next-key locks under REPEATABLE READ: two sessions insert into the same index range after reading it with SELECT … FOR UPDATE.
  • Foreign key checks and unique-index checks take locks on parent or index rows too.
  • Large transactions that lock many rows increase the chance of overlap.

How to fix it

  • Retry the whole transaction when you get 1213; it was rolled back completely, so it is safe to run again.
  • Access rows in a consistent order in every code path (for example always by primary key ascending).
  • Keep transactions small and short, and index the columns in WHERE clauses of updates.
  • Read the details of the last deadlock: SHOW ENGINE INNODB STATUS\G (section LATEST DETECTED DEADLOCK), or set innodb_print_all_deadlocks = ON to log every one.
  • READ COMMITTED isolation uses fewer gap locks and often removes insert deadlocks.

Reproduce it yourself

These steps are the exact test our harness runs. Use a scratch server, never production.

  1. Connect with mysql -u root -p and create a scratch database and the test data:

    CREATE DATABASE demo;
    USE demo;
    CREATE TABLE acct (id INT PRIMARY KEY, bal INT) ENGINE=InnoDB;
    INSERT INTO acct VALUES (1, 100), (2, 100);
  2. Session A: lock row 1.

    USE demo;
    START TRANSACTION;
    UPDATE acct SET bal = bal - 10 WHERE id = 1;
  3. Session B: lock row 2.

    USE demo;
    START TRANSACTION;
    UPDATE acct SET bal = bal - 5 WHERE id = 2;
  4. Session A: now ask for row 2 (it waits for B).

    UPDATE acct SET bal = bal + 10 WHERE id = 2;
  5. Session B: ask for row 1. The cycle is complete: InnoDB rolls one transaction back with 1213 and the other continues.

    UPDATE acct SET bal = bal + 5 WHERE id = 1;

You get: ERROR 1213 (40001) at line 1: Deadlock found when trying to get lock; try restarting transaction(11.8.9)

Verified fixes

  1. Lock rows in the same order in every transaction (here: by id)

    Make both sessions update the rows in the same order (row 1, then row 2). Session B now simply waits for A instead of deadlocking.

    -- session A
    START TRANSACTION;
    UPDATE acct SET bal = bal - 10 WHERE id = 1;
    UPDATE acct SET bal = bal + 10 WHERE id = 2;
    COMMIT;
    
    -- session B
    START TRANSACTION;
    UPDATE acct SET bal = bal + 5 WHERE id = 1;
    UPDATE acct SET bal = bal - 5 WHERE id = 2;
    COMMIT;

Tested on

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
Two transactions lock the same two rows in opposite order
repro
✓ 1213✓ 1213✓ 1213✓ 1213✓ 1213✓ 1213✓ 1213✓ 1213✓ 1213
Lock rows in the same order in every transaction (here: by id)
fix
✓✓✓✓✓✓✓✓✓
Two transactions lock the same two rows in opposite order repro
MySQL 5.6✓ 12135.7✓ 12138.0✓ 12138.4✓ 12139✓ 1213
MariaDB 10.6✓ 121310.11✓ 121311.4✓ 121311.8✓ 1213
Lock rows in the same order in every transaction (here: by id) fix
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9

Related errors

  • 1205 ER_LOCK_WAIT_TIMEOUTexplained
  • 1614 ER_XA_RBDEADLOCK