The shell script automatically fixes corrupted mysql tables.

Source: Internet
Author: User
Recently, I checked the mysql database server log and found a Table corruption ERROR log, for example, 1207247: 30: 48 [ERROR]/data/soft/mysql/libexec/mysqld: Table '. /blog/wp_links 'ismarkedascrashedandlast (automatic ?) The repairfailed is normal after manual repair of the table, not a few

Recently, I checked the mysql database server log and found the ERROR log with Table damage. For example: 120724 7:30:48 [ERROR]/data/soft/mysql/libexec/mysqld: Table '. /blog/wp_links 'is marked as crashed and last (automatic ?) After the repair failed manually repairs the table, it is normal. After a few days, an error is found. So I wrote a script to automatically fix it. Logs are detected at a certain time. If such an error record exists, the error table is repaired to achieve automatic repair. To prevent repeated execution of error records in logs, after each log detection, the log file is cleared. There are actually many methods for this type of script, but this is only one of them. I haven't written the script for a long time, and I am confused about it. I hope you will come up with some mistakes, for more information, see.

 

#!/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  


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.