Check whether the mysql database has a bad table shell script.

Source: Internet
Author: User

Check whether the mysql database has a bad table shell script.

Shell scripts detect and check whether a bad table exists in the mysql database

This script is mainly used to detect all databases on the mysql server or bad tables in individual databases. It is applicable to the RHEL/Centos series.

01 #!/bin/bash 02 # The main purpose of this script is to detect all db or bad tables in a separate db on the mysql server. 03 # Variable description pass mysql account password name mysql account name data_path mysql directory path 04 # Directory_list directory list file_list file list db_name database name repair_count total number of tables to be repaired in a single database 05 # Variable Description: Total number of tables to be repaired in all databases in repair_count_all mysql_version mysql version _ file_name data table name 06 07 echo -e "The main purpose of this script is to detect all databases on the mysql server or bad tables \ n in a separate database" 08 pass=123456 09 name=root 10 11 read -p "Enter the mysql storage path :" choose 12 data_path=$choose 13 unset choose 14 15 read -p "Enter the mysql command path :" mysql_version 16 # File identifiers for standard input, standard output, and standard error output are marked by 0, 1, and 2 17 read -P "select whether to check all databases on the server or the specified database 1: Check all databases 18 2: check only the specified database: "choose 19 if [ $choose == 1 ]; then 20 cd $data_path 21 for directory_list in $( ls ) 22 do 23 if [ -d $directory_list ]; then 24 if [ "mysql" != "${directory_list}" -a "test" != "${directory_list}" ]; then 25 cd ${directory_list} 26 echo "The current check database is :" ${directory_list} 27 for file_list in $( ls *.frm) 28 do 29 _file_name=${file_list%.frm} 30 echo -e "\n" >> /tmp/check_table_all.log 31 ${mysql_version} -h 127.0.0.1 -u${name} -p${pass} -e " 32 check table "${directory_list}.${_file_name} 2>&1 >> /tmp/check_table_all.log 33 done 34 cd .. 35 fi 36 fi 37 done 38 cat /tmp/check_table_all.log | grep "Table is marked as 39 crashed" > /tmp/check_table_repair.log 40 repair_count_all=` awk 'END{print NR}' /tmp/check_table_repair.log ` 41 echo -e "All databases have $ {repair_count_all} tables to be repaired! " 42 more /tmp/check_table_repair.log 43 else 44 read -p "Enter the name of the database to be checked :" db_name 45 cd ${data_path}/${db_name} 46 for file_list in $( ls *.frm) 47 do 48 _file_name=${file_list%.frm} 49 echo -e "\n" >> /tmp/check_${db_name}.log 50 ${mysql_version} -h 127.0.0.1 -u${name} -p${pass} -e "check table 51 "${db_name}.$_file_name 2>&1 >> /tmp/check_${db_name}.log 52 done 53 cat /tmp/check_${db_name}.log | grep "Table is marked as crashed 54 " > /tmp/check_${db_name}_Repair.log 55 repair_count=` awk 'END{print NR}' /tmp/check_${db_name}_Repair.log` 56 echo -e "$ {Db_name} has $ {repair_count} tables to be repaired! \ N" 57 more /tmp/check_${db_name}_Repair.log 58 fi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.