十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
本篇內(nèi)容介紹了“怎么用shell腳本自動(dòng)修復(fù)mysql損壞的表”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
問(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
“怎么用shell腳本自動(dòng)修復(fù)mysql損壞的表”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!