---
title: "Avoid MySQL [Deadlock found when trying to get lock; try restarting transaction]"
author: "Ihavegotyou"
date: "2015-11-13T16:14:38Z"
tags: []
categories: ["数据库"]
slug: "avoid-mysql-deadlock-found-when-trying-to-get-lock-try-restarting-transaction"
draft: false
lastmod: "2015-11-13T16:14:38Z"
id: 2256678
url: "https://ucstudio.iteye.com/blog/2256678"
---

偶尔一天，监控到程序异常:  OperationalError: (1213, 'Deadlock found when trying to get lock; try restarting transaction').

       通过网络Seach,发现**show** innodb **status (MySQL5.1以下可以用)/show engine innodb status(MySQL5.1以上可用) 查看**  the LATEST DETECTED DEADLOCK

可以具体看到是哪两个SQL引起的死锁. 但需注意的是后面一个DEAD LOCK Info会覆盖之前的，所以需要监控才能发现问题所在.

 

     进一步通过Search,发现是有方案可以避免它,参考:

[http://stackoverflow.com/questions/2332768/how-to-avoid-mysql-deadlock-found-when-trying-to-get-lock-try-restarting-trans](http://stackoverflow.com/questions/2332768/how-to-avoid-mysql-deadlock-found-when-trying-to-get-lock-try-restarting-trans "avoid mysql deadlock")