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