Last active 1741247170

lsemenenko's Avatar lsemenenko revised this gist 1741247169. Go to revision

1 file changed, 106 insertions

optimize_innodb_tables.sh(file created)

@@ -0,0 +1,106 @@
1 + #!/bin/bash
2 +
3 + # Script to optimize all InnoDB tables in a specified MySQL database
4 + # Usage: ./optimize_innodb_tables.sh <database_name> [options]
5 +
6 + # Display help information
7 + function show_help {
8 + echo "Usage: $0 <database_name> [options]"
9 + echo ""
10 + echo "Options:"
11 + echo " -h, --help Display this help message"
12 + echo " -u, --user USERNAME MySQL username (optional, will use .my.cnf if not provided)"
13 + echo " -p, --password PASS MySQL password (optional, will use .my.cnf if not provided)"
14 + echo ""
15 + echo "Example: $0 mydatabase -u root -p mypassword"
16 + echo "Example: $0 mydatabase (uses credentials from .my.cnf)"
17 + exit 1
18 + }
19 +
20 + # Check if we have at least one argument
21 + if [ $# -lt 1 ]; then
22 + show_help
23 + fi
24 +
25 + # Initialize variables
26 + DB_NAME=""
27 + MYSQL_USER=""
28 + MYSQL_PASS=""
29 + MYSQL_OPTS=""
30 +
31 + # Parse command line arguments
32 + while [ $# -gt 0 ]; do
33 + case "$1" in
34 + -h|--help)
35 + show_help
36 + ;;
37 + -u|--user)
38 + MYSQL_USER="$2"
39 + shift 2
40 + ;;
41 + -p|--password)
42 + MYSQL_PASS="$2"
43 + shift 2
44 + ;;
45 + *)
46 + # First non-option argument is the database name
47 + if [ -z "$DB_NAME" ]; then
48 + DB_NAME="$1"
49 + shift
50 + else
51 + echo "Error: Unknown option $1"
52 + show_help
53 + fi
54 + ;;
55 + esac
56 + done
57 +
58 + # Verify database name was provided
59 + if [ -z "$DB_NAME" ]; then
60 + echo "Error: Database name is required."
61 + show_help
62 + fi
63 +
64 + # Set MySQL command options
65 + if [ -n "$MYSQL_USER" ]; then
66 + MYSQL_OPTS="$MYSQL_OPTS -u $MYSQL_USER"
67 + fi
68 +
69 + if [ -n "$MYSQL_PASS" ]; then
70 + MYSQL_OPTS="$MYSQL_OPTS -p$MYSQL_PASS"
71 + fi
72 +
73 + # Get all InnoDB tables
74 + echo "Fetching InnoDB tables from database $DB_NAME..."
75 + INNODB_TABLES=$(mysql $MYSQL_OPTS -B -N -e "
76 + SELECT TABLE_NAME
77 + FROM INFORMATION_SCHEMA.TABLES
78 + WHERE TABLE_SCHEMA='$DB_NAME'
79 + AND ENGINE='InnoDB'")
80 +
81 + # Check if any InnoDB tables were found
82 + if [ -z "$INNODB_TABLES" ]; then
83 + echo "No InnoDB tables found in the database $DB_NAME."
84 + exit 0
85 + fi
86 +
87 + # Count the tables
88 + TABLE_COUNT=$(echo "$INNODB_TABLES" | wc -l)
89 + echo "Found $TABLE_COUNT InnoDB tables in database $DB_NAME."
90 +
91 + # Initialize counter for tracking progress
92 + COUNTER=0
93 +
94 + # Optimize each table
95 + for TABLE in $INNODB_TABLES; do
96 + COUNTER=$((COUNTER + 1))
97 + echo "[$COUNTER/$TABLE_COUNT] Optimizing table: $TABLE"
98 +
99 + # Run OPTIMIZE TABLE command
100 + RESULT=$(mysql $MYSQL_OPTS -B -N -e "
101 + OPTIMIZE TABLE $DB_NAME.$TABLE" | tr '\t' ' ')
102 +
103 + echo " Result: $RESULT"
104 + done
105 +
106 + echo "Optimization completed for all InnoDB tables in database $DB_NAME."
Newer Older