MySQL:数据库事务锁处理

执行update或insert或delete语句超时报错:

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
-- 错误1205(HY000):超出锁定等待超时; 尝试重新启动事务

可以重启数据库服务来解决,也可以如下方式解决:

--查看先当前库线程情况:
show full processlist;

--没有看到正在执行的慢SQL记录线程
+-----+------+--------------------+--------+---------+------+-------+-----------------------+----------+
| Id  | User | Host               | db     | Command | Time | State | Info                  | Progress |
+-----+------+--------------------+--------+---------+------+-------+-----------------------+----------+
|   2 | root | localhost          | estate | Query   |    0 | NULL  | show full processlist |    0.000 |
| 308 | root | 58.59.25.109:33457 | estate | Sleep   |  894 |       | NULL                  |    0.000 |
| 310 | root | 58.59.25.109:34619 | estate | Sleep   |  928 |       | NULL                  |    0.000 |
| 312 | root | 58.59.25.109:38101 | estate | Sleep   |    0 |       | NULL                  |    0.000 |
+-----+------+--------------------+--------+---------+------+-------+-----------------------+----------+


--再去查看innodb的事务表INNODB_TRX,看下里面是否有正在锁定的事务线程
--看看ID是否在show full processlist里面的sleep线程中.
--如果是,就证明这个sleep的线程事务一直没有commit或者rollback而是卡住了,我们需要手动kill掉。
SELECT * FROM information_schema.INNODB_TRX\G;

*************************** 1. row ***************************
                    trx_id: C15
                 trx_state: RUNNING
               trx_started: 2018-03-29 13:42:44
     trx_requested_lock_id: NULL
          trx_wait_started: NULL
                trx_weight: 4
       trx_mysql_thread_id: 310    --发现这个310事务卡住,(这里的数据是伪造的)
                 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: 1
         trx_rows_modified: 2
   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

--手动kill掉:
kill 310;

问题解决。

猜你喜欢

转载自blog.csdn.net/xuejianbest/article/details/85605920