Converting MyISAM tables to InnoDB

From EPrints Documentation
Revision as of 15:57, 5 August 2019 by Drn@ecs.soton.ac.uk (talk | contribs) (Added proper code highlighting for bash)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Below is Bash script that will convert all tables to InnoDB if they are currently MyISAM. If tables are already InnoDB nothing will change and the script should complete quite quickly. If not you may find it takes several hours to run. It is advised that if you are running this on a live repository you do the following:

  1. Take the repository offline (i.e. either completely stop Apache and the EPrints indexer or get the former to load a splash screen saying the repository is down for maintenance).
  2. Take a backup of the database or if you are running the server as a virtual machine, a snapshot of the whole server.
  3. Run the script below direct from the console or in screen, in case you lose your network connection.
#!/bin/bash

if [ -z $1 ]; then
  echo "Usage: $0 <DATABASE_NAME>"
  exit 1
fi

database=$1
read -p "Please enter the hostname of the server hosting this database [localhost]: " hostname
read -p "Please enter a username that has permission to alter tables [root]: " username
read -sp "Please enter the password for this user: " password
 
if [ -z "$hostname" ]; then
    hostname="localhost"
fi
if [ -z "$username" ]; then
    username="root"
fi
mysql_password=""
if [ ! -z $password ]; then
    mysql_password="-p$password"
fi
mysql_start="mysql -h $hostname -u $username $mysql_password $database"

for t in $(echo "show tables" | $mysql_start --batch --skip-column-names); do
    sql="ALTER TABLE \`$t\` ENGINE = InnoDB;"
    echo $sql
    time $mysql_start -e "$sql";
done