您好,登錄后才能下訂單哦!
數據庫在出現鎖或者死鎖的時候,處理起來應該綜合分析,應該配合數據庫監控系統、結合業務同時分析數據庫尋找問題,下面是一些常用的手動供參考:
參考參數:
innodb_lock_wait_timeout
查看是否有表鎖住:
show OPEN TABLES where In_use > 0;
show processlist ;
select * from information_schema.`PROCESSLIST` where info is not null order by time desc ;
show engine innodb status;
或者查詢
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS;
SELECT * from INFORMATION_SCHEMA.INNODB_TRX;
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS;
應該熟悉常用的表INNODB_LOCKS、INNODB_TRX、INNODB_LOCK_WAITS的一些字段含義,這樣可以快速的分析問題。
innodb_trx #當前運行的所有事務
innodb_locks #當前出現的鎖
innodb_lock_waits #鎖等待的對應關系
其中show OPEN TABLES where In_use > 0;只能看到那些表被鎖住;
show processlist 看不到鎖的信息,判讀起來比較麻煩;
show engine innodb status; 可以查看到很多信息,特別是一些死鎖信息,但是分析起來比較麻煩。
下面為常用的sql語句供參考:
SELECT
r.trx_id waiting_trx_id,
r.trx_mysql_thread_Id waiting_thread,
r.trx_query waiting_query,
b.trx_id blocking_trx_id,
b.trx_mysql_thread_id blocking_thread,
b.trx_query blocking_query
FROM
information_schema.innodb_lock_waits w
INNER JOIN information_schema.innodb_trx b ON b.trx_id = w.blocking_trx_id
INNER JOIN information_schema.innodb_trx r ON r.trx_id = w.requesting_trx_id;
生成kill:
SELECT
r.trx_id waiting_trx_id,
r.trx_mysql_thread_Id waiting_thread,
r.trx_query waiting_query,
b.trx_id blocking_trx_id,
CONCAT('kill query ',b.trx_mysql_thread_id , ';'),
b.trx_query blocking_query
FROM
information_schema.innodb_lock_waits w
INNER JOIN information_schema.innodb_trx b ON b.trx_id = w.blocking_trx_id
INNER JOIN information_schema.innodb_trx r ON r.trx_id = w.requesting_trx_id;
根據查詢結果殺掉相應的thread:
kill blocking_thread;
表對應的字段含義參考:
SELECT
trx_id AS `事務ID`,
trx_state AS `事務狀態`,
trx_requested_lock_id AS `事務需要等待的資源`,
trx_wait_started AS `事務開始等待時間`,
trx_tables_in_use AS `事務使用表`,
trx_tables_locked AS `事務擁有鎖`,
trx_rows_locked AS `事務鎖定行`,
trx_rows_modified AS `事務更改行`
FROM
information_schema.innodb_trx ;
SELECT
lock_id AS `鎖ID`,
lock_trx_id AS `擁有鎖的事務ID`,
lock_mode AS `鎖模式 `,
lock_type AS `鎖類型`,
lock_table AS `被鎖的表`,
lock_index AS `被鎖的索引`,
lock_space AS `被鎖的表空間號`,
lock_page AS `被鎖的頁號`,
lock_rec AS `被鎖的記錄號`,
lock_data AS `被鎖的數據`
FROM
information_schema.innodb_locks;
SELECT
requesting_trx_id AS `請求鎖的事務ID`,
requested_lock_id AS `請求鎖的鎖ID`,
blocking_trx_id AS `當前擁有鎖的事務ID`,
blocking_lock_id AS `當前擁有鎖的鎖ID`
FROM
innodb_lock_waits;
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。