Snippets

Catalyst2 Convert MyISAM tables to InnoDB

Created by Robert

File convert_myisam_tables.sh Added

  • Ignore whitespace
  • Hide word diff
+#! /usr/bin/env bash
+# This is an example script that finds MyISAM tables in a database 
+# and converts them InnoDB. It assumes the user has passwordless 
+# access to the database.
+
+# Store the database name
+db_name=testdb
+
+# Store any MyISAM tables in an array
+readarray -t myisam_tables < <(mysql --skip-column-names --batch --execute "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = '$db_name' AND ENGINE = 'MyISAM'")
+
+# Check if the array contains items, and if so convert the tables
+if [ "${#myisam_tables[@]}" -ge 1 ]; then
+  for table in "${myisam_tables[@]}"; do
+    mysql "$db_name" --execute "ALTER TABLE $table ENGINE = 'InnoDB'"
+  done
+else
+  echo "No MyISAM tables found in database $db_name"
+fi
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.