problem Description: recently looked at the MySQL database server log, the old found that there are damage to the table error log, such as: 120724 7:30:48 [ERROR]/data/soft/mysql/libexec/mysqld:table '. Blog/wp_links ' is marked as crashed and last (automatic?) repair failed manual repair of the table after the normal, not a few days later found an error.
Workaround: So I wrote a script to fix it automatically. is based on a certain period of time to detect a log, if there is such an error record, the error of the table to repair to achieve the purpose of automatic repair, in order to prevent the log in the wrong record of repeated execution, every time after the completion of the log log file empty.
There are a lot of ways to do this kind of script, but this is one of them, there are mistakes that everyone put forward, a lot of advice.
#!/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 $} ' $LOG _path | sort-k1n | uniq-c | Awk-f "'" ' {print $} ' | Awk-f '/' {print $} '
if [-N ' $TABLES] then for
i in '/usr/bin/awk '/' repair failed '/{print $} ' $LOG _path | sort-k1n | uniq-c | Awk-f "'" ' {print $} ' | Awk-f '/' {print $} ' 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 are no need to repair the table!"
Fi
Through this article you should know how the shell script automatically fixes the MySQL corrupted table, I hope you like it.