Home  >  Article  >  Database  >  How to batch check tables and repair and optimize

How to batch check tables and repair and optimize

坏嘻嘻
坏嘻嘻Original
2018-09-15 10:30:091342browse

本篇文章给大家带来的内容是关于如何批量检查表并进行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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn