MySQL实现批量检查表并进行repair与optimize的方法_MySQL
程序员文章站
2022-04-26 15:18:52
...
本文实例讲述了MySQL实现批量检查表并进行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命令,此方法可以在检查表并自动修复损坏的表,不过该过程比较耗时。
以上就是MySQL实现批量检查表并进行repair与optimize的方法_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!