16

I know that deadlocks occur inevitably when using transactions in Innodb and that they are harmless if they are treated properly by the application code - "just try it again", as the manual says.

So I was wondering - how do you detect deadlocks? Does a deadlock issue some special mysql error number? I am using PHP's mysqli extension if that matters.

Thank you.

Edit: solution found, see comments

7 Answers7

20

"SHOW ENGINE INNODB STATUS" from the MySQL Command line client (not a query browser) will give you info on deadlocks.

Deadlocks can also be caused by uncommitted transactions (usually program bugs) and the person who is running the uncommitted transaction will not see the problem as they will be working fine (through their data will not be committed).

msanford
  • 11,803
  • 11
  • 66
  • 93
boz
  • 201
  • 2
  • 2
14

http://dev.mysql.com/doc/refman/5.0/en/innodb-error-codes.html

1213 (ER_LOCK_DEADLOCK)

Transaction deadlock. You should rerun the transaction.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
8

Try MaatKit. It has a deadlock logger.

jonstjohn
  • 59,650
  • 8
  • 43
  • 55
  • 8
    Maatkit has been absorbed into the [Percona Toolkit](http://www.percona.com/software/percona-toolkit/) recently. The tool is called [pt-deadlock-logger](http://www.percona.com/doc/percona-toolkit/pt-deadlock-logger.html) and was formerly __mk-deadlock-logger__. – Mei Oct 04 '11 at 15:29
5

Try using MONyog. Enable MONyog's "Deadlock Monitoring" option to trace the deadlocks reported by INNODB STATUS. MONyog will send an alert to the user when a new deadlock occur. enter image description here

Sandy
  • 1,043
  • 2
  • 21
  • 32
2

If you are on a mac:

$ brew install percona-toolkit

$ pt-deadlock-logger -uroot --ask-pass localhost

Andrei Sura
  • 2,465
  • 1
  • 20
  • 15
1

Try innotop, will detect the deadlock for you.

1

I recently created a very simple check for deadlocks for the implementation of a smoke test of a web applciation. Code can be improved a lot, but it's working for now. See https://dev.mysql.com/doc/refman/8.0/en/innodb-standard-monitor.html for more info on the output of the used query below.

$status = DB::select("SHOW ENGINE INNODB STATUS")["Status"]??null;

if(strpos($status,"LATEST DETECTED DEADLOCK") !== false)
{
  trigger_error("LATEST DETECTED DEADLOCK section present in output of SHOW ENGINE INNODB STATUS");
}

if(strpos($status,"LATEST FOREIGN KEY ERROR") !== false)
{
  trigger_error("LATEST FOREIGN KEY ERROR section present in output of SHOW ENGINE INNODB STATUS");
}
Mark Baaijens
  • 496
  • 5
  • 6