十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
1、MySQL數(shù)據(jù)庫當出現(xiàn)慢查詢,是比較危險的,一旦有其他的DDL操作,可能會造成整個數(shù)據(jù)庫的等待
創(chuàng)新互聯(lián)公司總部坐落于成都市區(qū),致力網(wǎng)站建設(shè)服務(wù)有成都網(wǎng)站設(shè)計、做網(wǎng)站、網(wǎng)絡(luò)營銷策劃、網(wǎng)頁設(shè)計、網(wǎng)站維護、公眾號搭建、小程序設(shè)計、軟件開發(fā)等為企業(yè)提供一整套的信息化建設(shè)解決方案。創(chuàng)造真正意義上的網(wǎng)站建設(shè),為互聯(lián)網(wǎng)品牌在互動行銷領(lǐng)域創(chuàng)造價值而不懈努力!
可以分以下幾種情況:
當表是MyiSAM表,對表有慢查詢,不阻塞Select,對該表的其他DML,DDL操作都會被阻塞,比如出現(xiàn)Wating for table level lock,數(shù)據(jù)庫中一定不能還存在MyiSAM表
當表是Innodb表,當表上有慢查詢,不阻塞Select 和DML,其他的DDL操作都會被阻塞,比如出現(xiàn)waiting for table metadata lock
綜上,當數(shù)據(jù)庫中存在慢查詢時,是比較危險的,當執(zhí)行備份,create index ,alter table , flush table 等操作時就會造成數(shù)據(jù)庫的等待
解決辦法:
1、對數(shù)據(jù)庫中執(zhí)行時間較長的Select進行監(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ù)庫中當前有大量的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列是運行時間,第一列為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