1. 獲取mysql進(jìn)程Id
ps -ef |grep -i mysql

image.png
2. 實(shí)時(shí)查看mysql進(jìn)程中占用cpu,內(nèi)存最多的操作系統(tǒng)線程Id
top -Hp 6132

image.png
3. 根據(jù)操作系統(tǒng)線程id,查看mysql數(shù)據(jù)庫中對(duì)應(yīng)的線程id,根據(jù)mysql數(shù)據(jù)庫的線程id獲取sql
- 連接mysql,用root用戶連接
mysql -u root -P 3306 -h 127.0.0.1 -p
一般情況,root用戶只允許本地登錄,是不允許遠(yuǎn)程登錄,為了安全性
- 根據(jù)操作系統(tǒng)的線程id,查詢可疑的mysql的線程id
select thread_id,name ,PROCESSLIST_ID,THREAD_OS_ID from performance_schema.threads where thread_os_id =136942 ;

image.png
標(biāo)紅的是mysql的線程id
- 根據(jù)mysql的線程id,查詢具體的sql
select sql_text from performance_schema.events_statements_current where thread_id =1254;
- 也可以兩個(gè)sql,一起使用
select sql_text from performance_schema.events_statements_current
where thread_id in ( select thread_id from performance_schema.threads where thread_os_id = 408271);

image.png
- 在mysql內(nèi)部,快速查看可疑sql內(nèi)容
show processlist;
ps:Id為mysql線程id

image.png
select sql_text from performance_schema.events_statements_current where
thread_id in (select thread_id from performance_schema.threads where processlist_id = 15396);

image.png
4. 事務(wù)卡住情況
- 查詢當(dāng)前運(yùn)行的所有事務(wù)
mysql> SELECT * FROM information_schema.INNODB_TRX;
- 當(dāng)前出現(xiàn)的鎖
mysql> SELECT * FROM information_schema.INNODB_LOCKs;
- 鎖等待的對(duì)應(yīng)關(guān)系
mysql> SELECT * FROM information_schema.INNODB_LOCK_waits;
解釋:看事務(wù)表INNODB_TRX,里面是否有正在鎖定的事務(wù)線程,看看ID是否在show processlist里面的sleep線程中,如果是,就證明這個(gè)sleep的線程事務(wù)一直沒有commit或者rollback而是卡住了,我們需要手動(dòng)kill掉。
搜索的結(jié)果是在事務(wù)表發(fā)現(xiàn)了很多任務(wù),這時(shí)候最好都kill掉。
批量刪除事務(wù)表中的事務(wù)
我這里用的方法是:通過information_schema.processlist表中的連接信息生成需要處理掉的MySQL連接的語句臨時(shí)文件,然后執(zhí)行臨時(shí)文件中生成的指令。
mysql> select concat('KILL ',id,';') from information_schema.processlist p inner
join information_schema.INNODB_TRX x on p.id=x.trx_mysql_thread_id where db='test';
+------------------------+
| concat('KILL ',id,';') |
+------------------------+
| KILL 588379; |
| KILL 588313; |
| KILL 588275; |
+------------------------+
3 rows in set
kill掉以后再執(zhí)行SELECT * FROM information_schema.INNODB_TRX; 就是空了。
這時(shí)候系統(tǒng)就正常了