問(wèn)題描述:最近查看mysql數(shù)據(jù)庫(kù)服務(wù)器日志,老發(fā)現(xiàn)有表?yè)p壞的錯(cuò)誤日志,比如:120724 7:30:48 [ERROR] /data/soft/mysql/libexec/mysqld: Table './blog/wp_links' is marked as crashed and last (automatic?) repair failed 手動(dòng)修復(fù)了表后正常了,沒(méi)過(guò)幾天又發(fā)現(xiàn)出現(xiàn)錯(cuò)誤。
解決方法:于是就寫(xiě)了個(gè)腳本來(lái)自動(dòng)修復(fù)。是根據(jù)一定時(shí)間檢測(cè)一次日志,如果有這樣的錯(cuò)誤記錄時(shí),就對(duì)出錯(cuò)的表進(jìn)行修復(fù)來(lái)達(dá)到自動(dòng)修復(fù)的目的,為了防止日志中錯(cuò)誤記錄的重復(fù)執(zhí)行,每次檢測(cè)完日志后特將日志文件清空。
此類(lèi)腳本的方法其實(shí)有很多,只不過(guò)這是其中一種而已,有錯(cuò)誤之處大家提出來(lái),多多指教。
#!/bin/sh
DB_USER="root"
DB_PASS="123456"
DB_NAME="blog"
LOG_PATH="/data/db/errlog.log"
TIME=`date +%Y-%m-%d" "%H:%M:%S`
TABLES=`/usr/bin/awk '/'"repair failed"'/ {print $6}' $LOG_PATH | sort -k1n | uniq -c | awk -F "'" '{print $2}' | awk -F '/' '{print $3}'`
if [ -n "$TABLES" ]
then
for i in `/usr/bin/awk '/'"repair failed"'/ {print $6}' $LOG_PATH | sort -k1n | uniq -c | awk -F "'" '{print $2}' | awk -F '/' '{print $3}'`
do
/data/soft/mysql/bin/mysql -u$DB_USER -p$DB_PASS $DB_NAME -e "repair TABLE $i" > repair_$i
if grep "OK" repair_$i >/dev/null
then
echo "$TIME repair TABLES $i successful!"
else
echo "$TIME repair TABLES $i Failed!"
fi
rm -rf repair_$i
done
else
echo "There is no need to repair the table!"
fi
:>$LOG_PATH
通過(guò)這篇文章大家應(yīng)該知道shell腳本是如何自動(dòng)修復(fù)mysql損壞的表了吧,希望大家喜歡。
您可能感興趣的文章:- Shell下實(shí)現(xiàn)免密碼快速登陸MySQL數(shù)據(jù)庫(kù)的方法
- MySQL數(shù)據(jù)庫(kù)的shell腳本自動(dòng)備份
- Centos7安裝 mysql5.6.29 shell腳本
- MySQL5.7安裝過(guò)程并重置root密碼的方法(shell 腳本)
- 實(shí)戰(zhàn)模擬監(jiān)控MySQL服務(wù)shell腳本小結(jié)
- MySQL的一些功能實(shí)用的Linux shell腳本分享
- CentOS下編寫(xiě)shell腳本來(lái)監(jiān)控MySQL主從復(fù)制的教程
- linux下mysql如何自動(dòng)備份shell腳本
- 一個(gè)簡(jiǎn)單的MySQL備份Shell腳本
- Shell腳本檢查IP格式及mysql操作實(shí)例
- 用shell寫(xiě)一個(gè)mysql數(shù)據(jù)備份腳本