user1411272
user1411272

Reputation:

How to detect deadlocks in Mysql / innodb?

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

Upvotes: 18

Views: 72663

Answers (7)

Mark Baaijens
Mark Baaijens

Reputation: 515

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");
}

Upvotes: 1

Andrei Sura
Andrei Sura

Reputation: 2604

If you are on a mac:

$ brew install percona-toolkit

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

Upvotes: 2

boz
boz

Reputation: 211

"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).

Upvotes: 21

Sandy
Sandy

Reputation: 1053

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

Upvotes: 5

Green Card
Green Card

Reputation:

Try innotop, will detect the deadlock for you.

Upvotes: 1

S.Lott
S.Lott

Reputation: 391852

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

1213 (ER_LOCK_DEADLOCK)

Transaction deadlock. You should rerun the transaction.

Upvotes: 14

jonstjohn
jonstjohn

Reputation: 60276

Try MaatKit. It has a deadlock logger.

Upvotes: 8

Related Questions