linux 修复mysql表检查修复优化脚本

#!/bin/bash
host_name=localhost
user_name=root
user_pwd=123456
database=cyxbj
need_optmize_table=fales
tables=$(mysql -h$host_name -u$user_name -p$user_pwd $database -A -Bse “show tables”)
for table_name in $tables
do
check_result=$(mysql -h$host_name -u$user_name -p$user_pwd $database -A -Bse “check table $table_name” | awk ‘{ print $4 }’)
if [ “$check_result” = “OK” ]
then
echo “It’s no need to repair table $table_name”
else
echo $(mysql -h$host_name -u$user_name -p$user_pwd $database -A -Bse “repair table $table_name”)
fi

if [ $need_optmize_table = true ]
then
echo $(mysql -h$host_name -u$user_name -p$user_pwd $database -A -Bse “optimize table $table_name”)  >> /var/log/mysql/check.log
fi
done

 

 

记得去创建文件/var/log/mysql/check.log