問題描述:最近查看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ù)來達(dá)到自動修復(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損壞的表了吧,希望大家喜歡。
標(biāo)簽:江西 大慶 三門峽 安康 大興安嶺 仙桃 遵義 上饒
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《shell腳本自動修復(fù)mysql損壞的表》,本文關(guān)鍵詞 shell,腳本,自動,修復(fù),mysql,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。