首页 > 技术文章 > 数据库死锁解决办法

wzk1992 2016-08-04 09:39 原文

1.之前遇到过一个场景:有一个用户登录一直失败,但是其他用户却可以正常登录,后来测试借口发现在登录时更新这个用户的登录时间时,一直没有反应然后超时了。因为innordb是行级锁的所以就想到了是这条用户的信息被锁住了。

2.解决方案:

执行语句:SELECT * FROM information_schema.INNODB_TRX\G ;

*************************** 1. row ***************************
trx_id: 189324
trx_state: RUNNING
trx_started: 2013-04-18 17:48:14
trx_requested_lock_id: NULL
trx_wait_started: NULL
trx_weight: 3
trx_mysql_thread_id: 16
trx_query: NULL
trx_operation_state: NULL
trx_tables_in_use: 0
trx_tables_locked: 0
trx_lock_structs: 2
trx_lock_memory_bytes: 376
trx_rows_locked: 3
trx_rows_modified: 1
trx_concurrency_tickets: 0
trx_isolation_level: REPEATABLE READ
trx_unique_checks: 1
trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 10000
trx_is_read_only: 0
trx_autocommit_non_locking: 0

这个语句是查询innordb的事务信息的数据,一般trx_started这个时间到现在时间很长的话,说明这个事务已经锁死了,trx_rows_locked代表的是有多少行被锁定了。trx_tables_locked表示多少表被锁定了。所以可以根据这个信息去找到trx_mysql_thread_id的值然后使用kill 线程id 杀掉对应的线程就可以解开锁定了

推荐阅读