MySQL8死锁检测

一、死锁相关的表

  1. information_schema.INNODB_TRX
  2. `performance_schema`.data_lock_waits
  3. `performance_schema`.threads
  4. `performance_schema`.data_locks

相关说明:

  1. -- request 被阻塞的
  2. -- block 引起阻塞的

对应字段

  1. `performance_schema`.threads.PROCESSLIST_ID = `information_schema`.innodb_trx.trx_mysql_thread_id = information_schema.`PROCESSLIST`.id
  2. `performance_schema`.threads.thread_id = `performance_schema`.data_lock_waits.REQUESTING_THREAD_ID

二、示意图

uuxMUzpC5GdW1_sj4d4htK4bVTe_8pCM2S8FAMpQDZo.png

三、检测脚本

SELECT
	a.*, c.trx_state AS block_trx_state,
	c.trx_started AS block_trx_started,
	c.trx_query AS block_trx_query,
	c.trx_mysql_thread_id AS block_trx_mysql_thread_id,
	d.thread_id AS block_thread_id,
	d.PROCESSLIST_USER AS block_user,
	d.PROCESSLIST_HOST AS block_host
FROM
	(
		SELECT
			b.trx_id AS req_trx_id,
			b.trx_state AS req_trx_state,
			b.trx_started AS req_trx_started,
			b.trx_query AS req_trx_query,
			b.trx_mysql_thread_id AS req_trx_mysql_thread_id,
			c.thread_id AS req_thread_id,
			c.PROCESSLIST_USER AS req_user,
			c.PROCESSLIST_HOST AS req_host
		FROM
			`performance_schema`.data_lock_waits a
		INNER JOIN information_schema.INNODB_TRX b ON a.REQUESTING_ENGINE_TRANSACTION_ID = b.trx_id
		AND a.REQUESTING_ENGINE_LOCK_ID = b.trx_requested_lock_id
		INNER JOIN `performance_schema`.threads c ON a.REQUESTING_THREAD_ID = c.THREAD_ID
	) a
INNER JOIN `performance_schema`.data_lock_waits b ON a.req_trx_id = b.REQUESTING_ENGINE_TRANSACTION_ID
AND a.req_thread_id = b.REQUESTING_THREAD_ID
INNER JOIN information_schema.INNODB_TRX c ON b.BLOCKING_ENGINE_TRANSACTION_ID = c.TRX_ID
INNER JOIN `performance_schema`.threads d ON b.BLOCKING_THREAD_ID = d.THREAD_ID

四、结果展示

edKa8o787SPRexEJ8TskUrdw5_5YGRYU6UKl5oy0BM.png

req_trx_query 是被阻塞的语句,block_trx_query是导致阻塞的语句

五、转载

https://blog.csdn.net/weixin_33344952/article/details/113296439?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_title~default-0.queryctrv2&spm=1001.2101.3001.4242.1&utm_relevant_index=3

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×