本文实例讲述了MySQL实现批量检查表并进行repair与optimize的方法。分享给大家供大家参考,具体如下:
以下是shell的参考代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#!/bin/bash
host_name=192.168.0.123
user_name=xiaomo
user_pwd=my_pwd
database =my_db_name
need_optmize_table= true
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" )
fi
done
|
也可以使用mysqlcheck命令,此方法可以在检查表并自动修复损坏的表,不过该过程比较耗时。
希望本文所述对大家MySQL数据库计有所帮助。