MySQL鎖;Lock wait timeout exceeded; try restarting transaction

問題

Lock wait timeout exceeded; try restarting transaction

分析

  • 設置了autocommit=0,但沒有提交或者回滾;autocommit是session級別的。如

    set autocommit=0;
    select * from A where id=1 for update;
    ...
    commit;/*rollback 這裏可能被忽略、異常等*/
  • 默認設置autocommit=1,但事務沒有正常結束,如

    begin;/*start transaction*/
    select * from A where id=1 for update;
    ...
    commit;/*rollback 這裏可能被忽略、異常等*/

解決方案

  • 檢查阻塞的進程並kill掉【推薦】

    select * from information_schema.processlist p join information_schema.innodb_trx it on it.trx_mysql_thread_id =p.id where trx_query is null;/*排查沒有執行sql,但是佔用事務的進程*/
    select concat('call mysql.rds_kill(',id,');') from information_schema.processlist p join information_schema.innodb_trx it on it.trx_mysql_thread_id =p.id where trx_query is null;/*kill 阻塞其他進程的進程*/
  • 檢查程序是否發生異常導致沒有事務沒有正常結束。事務正確的使用方式【推薦】

    begin;/*start transaction;*/
    select * from A where id=1 for update;
    ...
    commit;/*rollback*/
  • 查看鎖狀態

    select * from information_schema.innodb_locks;
    
    select * from information_schema.innodb_lock_waits ;
    
    select * from information_schema.processlist pl join information_schema.innodb_trx it on pl.id=it.trx_mysql_thread_id join information_schema.innodb_locks il on it.trx_id=il.lock_trx_id;/*當前進行的事務 lock*/
  • 查看innodb的狀態

    show engine innodb status;

    參考
    http://my.oschina.net/sansom/blog/123364


Wait for your reward

這裏寫圖片描述 這裏寫圖片描述 這裏寫圖片描述 這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章