Home >Database >Mysql Tutorial >How to batch check tables and repair and optimize
本篇文章给大家带来的内容是关于如何批量检查表并进行repair,optimize,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
以下是shell的参考代码:
#!/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命令,此方法可以在检查表并自动修复损坏的表,不过该过程比较耗时。
The above is the detailed content of How to batch check tables and repair and optimize. For more information, please follow other related articles on the PHP Chinese website!