MySQL死鎖檢查處理的正常方法
正常情況下,死鎖發(fā)生時(shí),權(quán)重最小的連接將被kill并回滾。但是為了找出語句來優(yōu)化,啟用可啟用死鎖將死鎖信息記錄下來。
#step 1:窗口一 mysql> start transaction; mysql> update aa set name='aaa' where id = 1; #step 2:窗口二 mysql> start transaction; mysql> update bb set name='bbb' where id = 1; #step 3:窗口一 mysql> update bb set name='bbb';
#step 4:窗口三 #是否自動(dòng)提交 mysql> show variables like 'autocommit'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | autocommit | ON | +---------------+-------+ #查看當(dāng)前連接 mysql> show processlist; mysql> show full processlist; mysql> SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST; +----+------+-----------+------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+------+---------+------+-------+------------------+ | 4 | root | localhost | test | Sleep | 244 | | NULL | | 5 | root | localhost | test | Sleep | 111 | | NULL | | 6 | root | localhost | NULL | Query | 0 | init | show processlist | +----+------+-----------+------+---------+------+-------+------------------+ #查看當(dāng)前正在被鎖的事務(wù)(鎖請求超時(shí)后則查不到) mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS; +------------------+-------------+-----------+-----------+-------------+-----------------+------------+-----------+----------+----------------+ | lock_id | lock_trx_id | lock_mode | lock_type | lock_table | lock_index | lock_space | lock_page | lock_rec | lock_data | +------------------+-------------+-----------+-----------+-------------+-----------------+------------+-----------+----------+----------------+ | 130718495:65:3:4 | 130718495 | X | RECORD | `test`.`bb` | GEN_CLUST_INDEX | 65 | 3 | 4 | 0x000000000300 | | 130718496:65:3:4 | 130718496 | X | RECORD | `test`.`bb` | GEN_CLUST_INDEX | 65 | 3 | 4 | 0x000000000300 | +------------------+-------------+-----------+-----------+-------------+-----------------+------------+-----------+----------+----------------+ #查看當(dāng)前等待鎖的事務(wù)(鎖請求超時(shí)后則查不到) mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS; +-------------------+-------------------+-----------------+------------------+ | requesting_trx_id | requested_lock_id | blocking_trx_id | blocking_lock_id | +-------------------+-------------------+-----------------+------------------+ | 130718499 | 130718499:65:3:4 | 130718500 | 130718500:65:3:4 | +-------------------+-------------------+-----------------+------------------+ #查看當(dāng)前未提交的事務(wù)(如果死鎖等待超時(shí),事務(wù)可能還沒有關(guān)閉) mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX; +--------------------------------------------------------------------------------------------------------+ | trx_id | trx_state | trx_started | trx_requested_lock_id | trx_wait_started | trx_weight | +-----------+-----------+---------------------+-----------------------+---------------------+------------+ | 130718500 | RUNNING | 2018-03-12 09:28:10 | NULL | NULL | 3 | | 130718499 | LOCK WAIT | 2018-03-12 09:27:59 | 130718499:65:3:4 | 2018-03-12 09:32:48 | 5 | ========================================================================================================== | trx_mysql_thread_id | trx_query | trx_operation_state | trx_tables_in_use | +---------------------+---------------------------------------+---------------------+-------------------+ | 4 | NULL | NULL | 0 | | 5 | update bb set name='bbb' | starting index read | 1 | ========================================================================================================= | trx_tables_locked | trx_lock_structs | trx_lock_memory_bytes | trx_rows_locked | trx_rows_modified | +-------------------+------------------+-----------------------+-----------------+-------------------+ | 0 | 2 | 360 | 3 | 1 | | 1 | 4 | 1184 | 4 | 1 | =========================================================================================================================== | trx_concurrency_tickets | trx_isolation_level | trx_unique_checks | trx_foreign_key_checks | trx_last_foreign_key_error | +-------------------------+---------------------+-------------------+------------------------+----------------------------+ | 0 | REPEATABLE READ | 1 | 1 | NULL | | 0 | REPEATABLE READ | 1 | 1 | NULL | =========================================================================================================================== | trx_adaptive_hash_latched | trx_adaptive_hash_timeout | trx_is_read_only | trx_autocommit_non_locking | +---------------------------+---------------------------+------------------+----------------------------+ | 0 | 10000 | 0 | 0 | | 0 | 10000 | 0 | 0 | +---------------------------+---------------------------+------------------+----------------------------+ #查看正在被訪問的表 mysql> show OPEN TABLES where In_use > 0; +----------+-------+--------+-------------+ | Database | Table | In_use | Name_locked | +----------+-------+--------+-------------+ | test | bb | 1 | 0 | +----------+-------+--------+-------------+
#step 3:窗口一 (若第三步中鎖請求太久,則出現(xiàn)鎖超時(shí)而終止執(zhí)行) mysql> update bb set name='bbb'; ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction #"窗口一" 鎖請求超時(shí)前,執(zhí)行第五步,使死鎖產(chǎn)生,則該連接 "窗口二" 執(zhí)行終止,"窗口一" 順利執(zhí)行 #step 5:窗口二 mysql> update aa set name='aa'; ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
查看最近一個(gè)死鎖情況
#查看最近一個(gè)死鎖情況 mysql> SHOW ENGINE INNODB STATUS\G ; ............... ------------------------ LATEST DETECTED DEADLOCK ------------------------ 2018-03-12 11:01:06 7ffb4993a700 #發(fā)生時(shí)間 *** (1) TRANSACTION: #事務(wù)1 TRANSACTION 130718515, ACTIVE 19 sec starting index read mysql tables in use 1, locked 1 #正被訪問的表 LOCK WAIT 4 lock struct(s), heap size 1184, 4 row lock(s), undo log entries 1 #影響行數(shù) MySQL thread id 5, OS thread handle 0x7ffb498f8700, query id 205 localhost root updating #線程/連接host/用戶 update bb set name='bb' #請求語句 *** (1) WAITING FOR THIS LOCK TO BE GRANTED: #等待以下資源 (鎖定位置及鎖模式) RECORD LOCKS space id 65 page no 3 n bits 72 index `GEN_CLUST_INDEX` of table `test`.`bb` trx id 130718515 lock_mode X waiting Record lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 000000000300; asc ;; 1: len 6; hex 000007ca9b34; asc 4;; 2: len 7; hex 1f000002092075; asc u;; 3: len 4; hex 80000001; asc ;; 4: len 2; hex 6262; asc bb;; *** (2) TRANSACTION: #事務(wù)2 TRANSACTION 130718516, ACTIVE 14 sec starting index read mysql tables in use 1, locked 1 4 lock struct(s), heap size 1184, 4 row lock(s), undo log entries 1 MySQL thread id 4, OS thread handle 0x7ffb4993a700, query id 206 localhost root updating update aa set name='aa' #請求語句 *** (2) HOLDS THE LOCK(S): #持有鎖資源 RECORD LOCKS space id 65 page no 3 n bits 72 index `GEN_CLUST_INDEX` of table `test`.`bb` trx id 130718516 lock_mode X Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0 0: len 8; hex 73757072656d756d; asc supremum;; Record lock, heap no 3 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 000000000301; asc ;; 1: len 6; hex 000007ca9b17; asc ;; 2: len 7; hex 9000000144011e; asc D ;; 3: len 4; hex 80000002; asc ;; 4: len 2; hex 6262; asc bb;; Record lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 000000000300; asc ;; 1: len 6; hex 000007ca9b34; asc 4;; 2: len 7; hex 1f000002092075; asc u;; 3: len 4; hex 80000001; asc ;; 4: len 2; hex 6262; asc bb;; *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 64 page no 3 n bits 80 index `GEN_CLUST_INDEX` of table `test`.`aa` trx id 130718516 lock_mode X waiting Record lock, heap no 7 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 000000000200; asc ;; 1: len 6; hex 000007ca9b33; asc 3;; 2: len 7; hex 1e000001d53057; asc 0W;; 3: len 4; hex 80000001; asc ;; 4: len 2; hex 6161; asc aa;; *** WE ROLL BACK TRANSACTION (2) ...............
#死鎖記錄只記錄最近一個(gè)死鎖信息,若要將每個(gè)死鎖信息都保存到錯(cuò)誤日志,啟用以下參數(shù): mysql> show variables like 'innodb_print_all_deadlocks'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | innodb_print_all_deadlocks | OFF | +----------------------------+-------+ #上面 【step 3:窗口一】若一直請求不到資源,默認(rèn)50秒則出現(xiàn)鎖等待超時(shí)。 mysql> show variables like 'innodb_lock_wait_timeout'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | innodb_lock_wait_timeout | 50 | +--------------------------+-------+ ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction #設(shè)置全局變量 鎖等待超時(shí)為60秒(新的連接生效) #mysql> set session innodb_lock_wait_timeout=50; mysql> set global innodb_lock_wait_timeout=60; #上面測試中,當(dāng)事務(wù)中的某個(gè)語句超時(shí)只回滾該語句,事務(wù)的完整性屬于被破壞了。為了回滾這個(gè)事務(wù),啟用以下參數(shù): mysql> show variables like 'innodb_rollback_on_timeout'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | innodb_rollback_on_timeout | OFF | +----------------------------+-------+
最終參數(shù)設(shè)置如下:(重啟服務(wù)重新連接測試)
[mysqld] log-error =/var/log/mysqld3306.log innodb_lock_wait_timeout=60 #鎖請求超時(shí)時(shí)間(秒) innodb_rollback_on_timeout = 1 #事務(wù)中某個(gè)語句鎖請求超時(shí)將回滾真?zhèn)€事務(wù) innodb_print_all_deadlocks = 1 #死鎖都保存到錯(cuò)誤日志
#若手動(dòng)刪除堵塞會(huì)話,刪除 Command='Sleep' 、無State、無Info、trx_weight 權(quán)重最小的。 show processlist; SELECT trx_mysql_thread_id,trx_state,trx_started,trx_weight FROM INFORMATION_SCHEMA.INNODB_TRX;
總結(jié)
到此這篇關(guān)于MySQL死鎖檢查處理的文章就介紹到這了,更多相關(guān)MySQL死鎖檢查處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL比較運(yùn)算符使用詳解及注意事項(xiàng)
這篇文章主要給大家介紹了關(guān)于MySQL比較運(yùn)算符使用詳解及注意事項(xiàng)的相關(guān)資料,Mysql可以通過運(yùn)算符來對表中數(shù)據(jù)進(jìn)行運(yùn)算,比如通過出生日期求年齡等,需要的朋友可以參考下2024-01-01mysql 本地?cái)?shù)據(jù)庫如何從遠(yuǎn)程數(shù)據(jù)庫導(dǎo)數(shù)據(jù)
mysql 本地?cái)?shù)據(jù)庫如何從遠(yuǎn)程數(shù)據(jù)庫導(dǎo)數(shù)據(jù),本文以此問題進(jìn)行詳細(xì)介紹,需要了解的朋友可以參考下2012-11-11mysql中異常錯(cuò)誤ERROR:2002的解決方法
最近在啟動(dòng)mysql的時(shí)候發(fā)現(xiàn)mysql報(bào)錯(cuò)了,錯(cuò)誤代碼是2002,通過查找相關(guān)的資料發(fā)現(xiàn)是var/lib/mysql 的訪問權(quán)限問題,所以這篇文章主要介紹了mysql中異常錯(cuò)誤ERROR:2002的解決方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-03-03