How to detect deadlocks in Mysql / innodb?

耗尽温柔 提交于 2019-12-20 18:39:27

问题


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


回答1:


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

1213 (ER_LOCK_DEADLOCK)

Transaction deadlock. You should rerun the transaction.




回答2:


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




回答3:


Try MaatKit. It has a deadlock logger.




回答4:


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.




回答5:


Try innotop, will detect the deadlock for you.




回答6:


If you are on a mac:

$ brew install percona-toolkit

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




回答7:


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


来源:https://stackoverflow.com/questions/643122/how-to-detect-deadlocks-in-mysql-innodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!