使用Linux的Shell腳本定時(shí)處理MySQL超時(shí)
更新時(shí)間:2013年11月07日 10:45:00 作者:
本文提供使用Linux的Shell腳本定時(shí)處理MySQL超時(shí)Locked進(jìn)程腳本
最近一段時(shí)間,我剛剛進(jìn)入一家新公司,并接手了這里的一個(gè)站點(diǎn),由于這個(gè)站點(diǎn)的架構(gòu)設(shè)計(jì)不太合理,導(dǎo)致MySQL的壓力始終很大,經(jīng)常出現(xiàn)超時(shí)的Locked進(jìn)程,于是編寫(xiě)了一段Linux的Shell腳本來(lái)定時(shí)kill掉這些進(jìn)程。
腳本如下:
#!/bin/bash
mysql_pwd="xxxxxx" #mysql的root密碼
mysql_exec="/usr/local/mysql/bin/mysql"
tmp_dir="/tmp"
file_sh="$tmp_dir/mysql_kill_locked.sh"
file_tmp="$tmp_dir/mysql_kill_locked.tmp"
file_log="$tmp_dir/mysql_kill_locked.log" #日志
$mysql_exec -uroot -p$mysql_pwd -e "show processlist" | grep -i "Locked" > $file_tmp
cat $file_tmp >> $file_log
for line in `cat $file_tmp | awk '{print $1}'`
do
echo "$mysql_exec -uroot -p$mysql_pwd -e "kill $line"" >> $file_sh
done
chmod +x $file_sh
sh $file_sh #執(zhí)行臨時(shí)腳本
> $file_sh #清空臨時(shí)腳本
最后,將這段腳本加入到crontab,定時(shí)執(zhí)行即可。
腳本如下:
復(fù)制代碼 代碼如下:
#!/bin/bash
mysql_pwd="xxxxxx" #mysql的root密碼
mysql_exec="/usr/local/mysql/bin/mysql"
tmp_dir="/tmp"
file_sh="$tmp_dir/mysql_kill_locked.sh"
file_tmp="$tmp_dir/mysql_kill_locked.tmp"
file_log="$tmp_dir/mysql_kill_locked.log" #日志
$mysql_exec -uroot -p$mysql_pwd -e "show processlist" | grep -i "Locked" > $file_tmp
cat $file_tmp >> $file_log
for line in `cat $file_tmp | awk '{print $1}'`
do
echo "$mysql_exec -uroot -p$mysql_pwd -e "kill $line"" >> $file_sh
done
chmod +x $file_sh
sh $file_sh #執(zhí)行臨時(shí)腳本
> $file_sh #清空臨時(shí)腳本
最后,將這段腳本加入到crontab,定時(shí)執(zhí)行即可。
您可能感興趣的文章:
- Linux shell實(shí)現(xiàn)每天定時(shí)備份mysql數(shù)據(jù)庫(kù)
- MySQL定時(shí)備份之使用Linux下的crontab定時(shí)備份實(shí)例
- linux實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)每天自動(dòng)備份定時(shí)備份
- linux實(shí)現(xiàn)定時(shí)備份mysql數(shù)據(jù)庫(kù)的簡(jiǎn)單方法
- Linux下如何實(shí)現(xiàn)Mysql定時(shí)任務(wù)
- linux下mysql自動(dòng)備份腳本代碼
- Linux下實(shí)現(xiàn)MySQL數(shù)據(jù)備份和恢復(fù)的命令使用全攻略
- Linux 每天自動(dòng)備份mysql數(shù)據(jù)庫(kù)的方法
- Linux下Mysql定時(shí)任務(wù)備份數(shù)據(jù)的實(shí)現(xiàn)方法
相關(guān)文章
詳解如何利用amoeba(變形蟲(chóng))實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)讀寫(xiě)分離
這篇文章主要介紹了詳解如何利用amoeba(變形蟲(chóng))實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)讀寫(xiě)分離,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05詳談innodb的鎖(record,gap,Next-Key lock)
下面小編就為大家?guī)?lái)一篇詳談innodb的鎖(record,gap,Next-Key lock)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03使用MySQL的Explain執(zhí)行計(jì)劃的方法(SQL性能調(diào)優(yōu))
這篇文章主要介紹了使用MySQL的Explain執(zhí)行計(jì)劃的方法(SQL性能調(diào)優(yōu)),使用EXPLAIN關(guān)鍵字可以模擬優(yōu)化器執(zhí)行SQL語(yǔ)句,具體詳解,需要的小伙伴可以參考一下2022-08-08select?into?from和insert?into?select的使用舉例詳解
select into from和insert into select都是用來(lái)復(fù)制表,下面這篇文章主要給大家介紹了關(guān)于select?into?from和insert?into?select使用的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04Navicat中導(dǎo)入mysql大數(shù)據(jù)時(shí)出錯(cuò)解決方法
這篇文章主要介紹了Navicat中導(dǎo)入mysql大數(shù)據(jù)時(shí)出錯(cuò)解決方法,需要的朋友可以參考下2017-04-04SQL實(shí)現(xiàn)LeetCode(182.重復(fù)的郵箱)
這篇文章主要介紹了SQL實(shí)現(xiàn)LeetCode(182.重復(fù)的郵箱),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08