欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

shell脚本自动修复mysql损坏的表

程序员文章站 2022-05-12 08:42:30
问题描述:最近查看mysql数据库服务器日志,老发现有表损坏的错误日志,比如:120724 7:30:48 [error] /data/soft/mysql/libexec...

问题描述:最近查看mysql数据库服务器日志,老发现有表损坏的错误日志,比如:120724 7:30:48 [error] /data/soft/mysql/libexec/mysqld: table './blog/wp_links' is marked as crashed and last (automatic?) repair failed 手动修复了表后正常了,没过几天又发现出现错误。

解决方法:于是就写了个脚本来自动修复。是根据一定时间检测一次日志,如果有这样的错误记录时,就对出错的表进行修复来达到自动修复的目的,为了防止日志中错误记录的重复执行,每次检测完日志后特将日志文件清空。

此类脚本的方法其实有很多,只不过这是其中一种而已,有错误之处大家提出来,多多指教。

#!/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脚本自动修复mysql损坏的表

通过这篇文章大家应该知道shell脚本是如何自动修复mysql损坏的表了吧,希望大家喜欢。