最後活躍 1740717150

convert_to_innodb.sh 原始檔案
1#!/bin/bash
2#===============================================================================
3# convert_to_innodb.sh
4#===============================================================================
5# Description:
6# Converts all non-InnoDB tables in a MySQL database to the InnoDB engine.
7# This script requires a properly configured ~/.my.cnf file for authentication.
8# It automatically detects all tables with engines other than InnoDB and
9# converts them one by one, providing progress updates.
10#
11# Usage:
12# ./convert_to_innodb.sh <database_name>
13#
14# Example:
15# ./convert_to_innodb.sh my_wordpress_db
16#
17# Author: Generated on $(date +%Y-%m-%d)
18#===============================================================================
19
20if [ "$#" -ne 1 ]; then
21 echo "Usage: $0 <database_name>"
22 echo "Example: $0 my_database"
23 exit 1
24fi
25
26DB_NAME="$1"
27
28# Welcome message
29echo "Converting all non-InnoDB tables to InnoDB engine..."
30echo "Database: $DB_NAME"
31
32# Get a list of all non-InnoDB tables
33TABLES=$(mysql -e "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='$DB_NAME' AND ENGINE != 'InnoDB';" -s)
34
35# Check if there are any non-InnoDB tables
36if [ -z "$TABLES" ]; then
37 echo "No non-InnoDB tables found in $DB_NAME. Nothing to convert."
38 exit 0
39fi
40
41# Count how many tables need conversion
42TABLE_COUNT=$(echo "$TABLES" | wc -l)
43echo "Found $TABLE_COUNT tables to convert..."
44
45# Convert each table
46COUNTER=0
47for TABLE in $TABLES; do
48 COUNTER=$((COUNTER+1))
49 echo "[$COUNTER/$TABLE_COUNT] Converting table: $TABLE"
50
51 # Get current engine for logging
52 CURRENT_ENGINE=$(mysql -e "SELECT ENGINE FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='$DB_NAME' AND TABLE_NAME='$TABLE';" -s)
53 echo " Current engine: $CURRENT_ENGINE"
54
55 # Execute the ALTER TABLE statement
56 mysql $DB_NAME -e "ALTER TABLE \`$TABLE\` ENGINE=InnoDB;"
57
58 if [ $? -eq 0 ]; then
59 echo " Conversion successful!"
60 else
61 echo " Conversion failed! Please check the table structure."
62 fi
63done
64
65# Verify all tables have been converted
66REMAINING=$(mysql -e "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='$DB_NAME' AND ENGINE != 'InnoDB';" -s)
67
68echo ""
69echo "Conversion completed!"
70echo "Non-InnoDB tables remaining: $REMAINING"
71
72if [ "$REMAINING" -eq "0" ]; then
73 echo "All tables successfully converted to InnoDB!"
74else
75 echo "Some tables could not be converted. Manual intervention may be required."
76fi