MySQL implements batch checklists and carries out repair and optimize methods

  • 2020-12-20 03:50:09
  • OfStack

An example of MySQL shows how to implement batch checklists and carry out repair and optimize. To share for your reference, the details are as follows:

Here is the reference code for 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
 #  Optimize the table , Improved performance 
 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

You can also use the mysqlcheck command, which can check tables and automatically repair damaged ones, although this process can be time-consuming.

For more information about MySQL, please refer to MySQL Transaction Operation Skills Summary, MySQL Stored Procedure Skills Collection, MySQL Database Locking Skills Summary and MySQL Common Functions Summary.

I hope this article has been helpful to you with the MySQL database.


Related articles: