您的位置:首页 > 数据库 > MySQL

MYSQL:Lock wait timeout exceeded; try restarting transaction ;ERROR 1205

2016-10-26 11:01 507 查看
声明:本文转载至:http://blog.csdn.net/mchdba/article/details/38313881   如有版权问题请联系本人删除

今天在编码时遇到了MYSQL的一个错误提示::Lock wait timeout exceeded; try restarting transaction;因为不熟悉MYSQL所以在网上查了一下,找到了解决方案。转载出来方便大家同时也做一下记录备忘;

1、查看是否有事务未提交造成锁定

SELECT * FROM information_schema.innodb_trx

-----------------我查到的是一条trx_id: 657815;  trx_state: RUNNING;  trx_mysql_thread_id: 58(这个是线程ID,其实直接KILL掉这个线程就可以了,可以直接执行第三步就好,只是下面写的更加全面。)

2,去查看先当前库的线程情况:

mysql> show full processlist;

+----------+-----------------+-------------------+-----------------+-------------+---------+-------------------------+-----------------------+

| Id       | User            | Host              | db              | Command     | Time    | State                   | Info                  |

+----------+-----------------+-------------------+-----------------+-------------+---------+-------------------------+-----------------------+

|        1 | event_scheduler | localhost         | NULL            | Daemon      | 9635385 | Waiting on empty queue  | NULL                  |

|  9930577 | business_web    | 192.168.1.21:45503 | business_db     | Sleep       |     153 |                         | NULL                  |

|  9945825 | business_web    | 192.168.1.25:49518 | business_db     | Sleep       |      43 |                         | NULL                  |

|  9946322 | business_web    | 192.168.1.23:44721 | business_db     | Sleep       |     153 |                         | NULL                  |

|  9960167 | business_web    | 192.168.3.28:2409  | business_db     | Sleep       |      93 |                         | NULL                  |

|  9964484 | business_web    | 192.168.1.21:24280 | business_db     | Sleep       |       7 |                         | NULL                  |

|  9972499 | business_web    | 192.168.3.28:35752 | business_db     | Sleep       |      13 |                         | NULL                  |

| 10000117 | business_web    | 192.168.3.28:9149  | business_db     | Sleep       |       6 |                         | NULL                  |

| 10002523 | business_web    | 192.168.3.29:42872 | business_db     | Sleep       |       6 |                         | NULL                  |

| 10007545 | business_web    | 192.168.1.21:51379 | business_db     | Sleep       |     155 |                         | NULL                  |

......

+----------+-----------------+-------------------+-----------------+-------------+---------+-------------------------+-----------------------+

 

没有看到正在执行的慢SQL记录线程,再去查看innodb的事务表INNODB_TRX,看下里面是否有正在锁定的事务线程,看看ID是否在show full processlist里面的sleep线程中,如果是,就证明这个sleep的线程事务一直没有commit或者rollback而是卡住了,我们需要手动kill掉。

 

mysql> SELECT * FROM information_schema.INNODB_TRX\G;

*************************** 1. row ***************************

                    trx_id: 20866

                 trx_state: LOCK WAIT

               trx_started: 2014-07-31 10:42:35

     trx_requested_lock_id: 20866:617:3:3

          trx_wait_started: 2014-07-30 10:42:35

                trx_weight: 2

       trx_mysql_thread_id: 9930577

                 trx_query: delete from dltask where id=1

       trx_operation_state: starting index read

         trx_tables_in_use: 1

         trx_tables_locked: 1

          trx_lock_structs: 2

     trx_lock_memory_bytes: 376

           trx_rows_locked: 1

         trx_rows_modified: 0

   trx_concurrency_tickets: 0

       trx_isolation_level: READ COMMITTED

         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

 

3,看到有这条[b]9930577的sql,kill掉,执行kill 9930577;[/b]

mysql> kill 9930577;

Query OK, 0 rows affected (0.00 sec)

 

mysql>

 

然后再去查询INNODB_TRX表,就没有阻塞的事务sleep线程存在了,如下所示:

mysql> SELECT * FROM INNODB_TRX\G;

Empty set (0.00 sec)

 

ERROR:

No query specified

 

mysql>

再去执行update语句,就能正常执行了,如下所示:

mysql> update order_info  set province_id=15  ,city_id= 1667  where order_from=10 and order_out_sn='1407261241xxxx';

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0

 

mysql>

4,总结分析

表数据量也不大,按照普通的情况来说,简单的update应该不会造成阻塞的,mysql都是autocommit,不会出现update卡住的情况,去查看下autocommit的值。

mysql> select @@autocommit;

+--------------+

| @@autocommit |

+--------------+

|            0 |

+--------------+

1 row in set (0.00 sec)

--------------------------------------------------------------------------

说明:我的问题与之不同,我的是因为数据库服务器的空间不足,造成连接、存储过程运行中断,所以才出了这样的问题,但解决方式是一样的。
--------------------------------------------------------------------------

mysql>

看到亮闪闪的0,这个设置导致原来的update语句如果没有commit的话,你再重新执行update语句,就会等待锁定,当等待时间过长的时候,就会报ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction的错误。

所以赶紧commit刚才执行的update语句,之后 set global autocommit=1;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐