mysql如何解除死锁状态

第一种:

前提条件:找到执行非常慢的sql;

如何找呢:还原客户遇到的问题场景,从控制台找到所执行的sql,一句句的去执行,直到找到执行非常慢的sql

1.查询是否锁表

show OPEN TABLES where In_use > 0;

2.查询进程(如果您有SUPER权限,您可以看到所有线程。否则,您只能看到您自己的线程)

show processlist

3.杀死进程id(就是上面命令的id列)

kill id

第二种:

1.查看下在锁的事务 

SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX;

2.杀死进程id(就是上面命令的trx_mysql_thread_id列)

kill 线程ID

第三种:

也许你无法查看到所在的info,这个时候你需要重启服务器,如果是分布式的话,就一台台重启吧;

原理是:杀死所有进程,释放所有锁。

MySQL - 锁等待超时与information_schema的三个表:
-- 1.information_schema.innodb_trx–当前运行的所有事务
select * from information_schema.innodb_trx;

-- information_schema.innodb_locks–当前出现的锁
select * from information_schema.innodb_locks;

-- information_schema.innodb_lock_waits–锁等待的对应关系
select * from information_schema.innodb_lock_waits;
-------------------------------------------------------------------------------------------
//常用

MySQL - 锁等待超时与information_schema的三个表:
-- 1.information_schema.innodb_trx–当前运行的所有事务
select * from information_schema.innodb_trx;

-- information_schema.innodb_locks–当前出现的锁
select * from information_schema.innodb_locks;

-- information_schema.innodb_lock_waits–锁等待的对应关系
select * from information_schema.innodb_lock_waits;
-------------------------------------------------------------------------------------------
//常用
select * from information_schema.innodb_trx where trx_state = 'LOCK WAIT';

SELECT a.* FROM information_schema.processlist a where command <> 'sleep' ORDER BY time desc

select * from information_schema.processlist where time> 60 and user='srapp_stsj';

SELECT
concat('kill',' ', id,';')
FROM
information_schema. PROCESSLIST a
WHERE
command <> 'sleep'
AND info LIKE 'select%'
AND time > 60
and user = 'srapp_stsj'
ORDER BY
time DESC

EXPLAIN
select * from sr_main where sys_scbj = '0' and mdjlx = 'ls_jz' and sys_spzt = 2
and mhzsfz = '330219193210200010'

原文地址:https://www.cnblogs.com/tongcc/p/11807551.html