問題描述:最近查看mysql數(shù)據(jù)庫服務(wù)器日志,老發(fā)現(xiàn)有表損壞的錯誤日志,比如:120724 7:30:48 [ERROR] /data/soft/mysql/libexec/mysqld: Table './blog/wp_links' is marked as crashed and last (automatic?) repair failed 手動修復(fù)了表后正常了,沒過幾天又發(fā)現(xiàn)出現(xiàn)錯誤。
解決方法:于是就寫了個腳本來自動修復(fù)。是根據(jù)一定時間檢測一次日志,如果有這樣的錯誤記錄時,就對出錯的表進行修復(fù)來達到自動修復(fù)的目的,為了防止日志中錯誤記錄的重復(fù)執(zhí)行,每次檢測完日志后特將日志文件清空。
此類腳本的方法其實有很多,只不過這是其中一種而已,有錯誤之處大家提出來,多多指教。
#!/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 
通過這篇文章大家應(yīng)該知道shell腳本是如何自動修復(fù)mysql損壞的表了吧,希望大家喜歡。
新聞熱點
疑難解答