您好,登錄后才能下訂單哦!
1、MySQL數(shù)據(jù)庫當(dāng)出現(xiàn)慢查詢,是比較危險的,一旦有其他的DDL操作,可能會造成整個數(shù)據(jù)庫的等待
可以分以下幾種情況:
當(dāng)表是MyiSAM表,對表有慢查詢,不阻塞Select,對該表的其他DML,DDL操作都會被阻塞,比如出現(xiàn)Wating for table level lock,數(shù)據(jù)庫中一定不能還存在MyiSAM表
當(dāng)表是Innodb表,當(dāng)表上有慢查詢,不阻塞Select 和DML,其他的DDL操作都會被阻塞,比如出現(xiàn)waiting for table metadata lock
綜上,當(dāng)數(shù)據(jù)庫中存在慢查詢時,是比較危險的,當(dāng)執(zhí)行備份,create index ,alter table , flush table 等操作時就會造成數(shù)據(jù)庫的等待
解決辦法:
1、對數(shù)據(jù)庫中執(zhí)行時間較長的Select進(jìn)行監(jiān)控,并及時報警
2、如果允許的話,寫腳本,發(fā)現(xiàn)較長的select語句,直接kill,并記錄日志中
-B, --batch Don't use history file. Disable interactive behavior.
-s, --silent Be more silent. Print results with a tab as separator,each row on new line.
-e, --execute=name Execute command and quit. (Disables --force and historyfile.)
#如果數(shù)據(jù)庫中當(dāng)前有大量的select,可以過濾掉,只kill waiting的
cat killWaitSession.sh
#!/bin/bash for i in `mysql -Bse 'show full processlist' | grep -i select |grep -i "Waiting | awk '{print $1}'` do mysql -Bse "kill $i" done
show processlist的command的狀態(tài)有很多,其中Query代表正在執(zhí)行的命令
Query : The thread is executing a statement.
cat killLongQuerySession.sh
#!/bin/bash executetime=(`mysql -Bse 'show processlist'| grep 'Query'|awk '{print $6 " " $1}'|sort -rn|head -1`) #第6列是運(yùn)行時間,第一列為session id time=${executetime[0]} id=${executetime[1]} while : do maxtime=300 if [ $time -gt $maxtime ] ; then echo $time $id >> /tmp/killqueryid.log mysql -Bse "kill $id" #else # echo $time $id fi sleep 10 #睡眠10s done
按MySQL中執(zhí)行時間反向排序
mysqladmin processlist --verbose |grep 'Query'|awk -F "|" '{print $7 $2 $9}'|sort -rn -k1
參考:
https://blog.51cto.com/jim123/1836712
https://dev.mysql.com/doc/refman/5.7/en/show-processlist.html
https://dev.mysql.com/doc/refman/5.7/en/thread-commands.html
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。