mysql table crash 自动修复脚本

已验证:eg1:

#!/bin/sh

cd /var/lib/mysql

echo "$(date) start mysql_deamon running..."
sleep 180

while [ 1 ]
do
echo "$(date) mysql_deamon running..."
sleep 30

for i in `cat mysqld.log |grep crash|awk -F "'" '{print $2}'|sort -u|sed -e '1d' `;
do
if [ "$i" != "./xxx/xxx" ]; then
echo 'not find xxx crash'
else
echo 'find xxx crash'
echo 'repair xxx'
mysqlcheck -uxxx -pxxx xxx xxx -r
fi

echo 'clear the err log'
> mysqld.log
done
done

eg1 中红色部分代码其实可以1句搞定,由于本人不善 shell 提取字符操作,只能做固定判断了

未验证 eg2:

#!/bin/bash

#This script edit by badboy connect leezhenhua17@163.com
#This script used by repair tables
mysql_host=localhost
mysql_user=root
mysql_pass=123456   #密码如果带特殊字符如分号可以这么写  root;2010就可以了
database=test

tables=$(mysql -h$mysql_host -u$mysql_user -p$mysql_pass $database -A -Bse “show tables”)
for arg in $tables
do
check_status=$(mysql -h$mysql_host -u$mysql_user -p$mysql_pass $database -A -Bse “check table $arg” | awk ‘{ print $4 }’)
if [ "$check_status" = "OK" ]
then
echo “$arg is ok”
else
echo $(mysql -h$mysql_host -u$mysql_user -p$mysql_pass $database -A -Bse “repair table $arg”)

fi
echo $(mysql -h$mysql_host -u$mysql_user -p$mysql_pass $database -A -Bse “optimize table $arg”)
done

未验证 eg3:

#!/bin/bash
#author:itnihao
#mail:itnihao@qq.com
#date 2013-02-18
#version v1.0
#function:repair mysql table

User=root
Password=123456
Host=192.168.1.10
Database=$(mysql -u${User} -p${Password} -h${Host} -e 'show databases'|grep -v 'Database')
for DBname in ${Database}
do
table=$(mysql -u${User} -p${Password} -h${Host} ${DBname} -e 'show tables'|grep -v tables_in_mysql)
for tableName in ${table}
do
mysql -u${User} -p${Password} -h${Host} ${DBname} -e "check table ${tableName}" [ "$?" != 0 ] &&mysql -u${User} -p${Password} -h${Host} ${DBname} -e "repair table ${tableName}"
done
done

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注