Shell script automatically fixes MySQL corrupted table _linux shell

Source: Internet
Author: User
Tags log log

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.

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.