Skip to content

Instantly share code, notes, and snippets.

@motiejus
Created October 22, 2012 20:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motiejus/3934170 to your computer and use it in GitHub Desktop.
Save motiejus/3934170 to your computer and use it in GitHub Desktop.
Convert several MySQL databases to MEMORY storage
#!/bin/sh
set -e
usage() {
echo "Usage: $0 MYSQL_USER MYSQL_PASSWORD PREFIX"
echo
echo "Converts as many as possible SSP MySQL tables to MEMORY"
echo
}
main() {
USER=$1
PASS=$2
PREFIX=$3
MYSQL="mysql -u${USER}"
if [ "${PASS}" != "NONE" ]; then
MYSQL="$MYSQL -p${PASS}"
fi
DBs=`Mysql "SHOW DATABASES" | sed -e '1d' | grep ^${PREFIX}`
for db in $DBs; do
tbls=`Mysql $db "SHOW TABLES" | sed -e '1d'`
for tbl in $tbls; do
code=
ret=`Mysql $db "ALTER TABLE $tbl ENGINE = MEMORY" 2>&1` || code=$?
if [ -n "$code" ]; then
echo "Error ($db:$tbl): $ret"
else
echo "Converted ($db:$tbl)"
fi
done
done
}
Mysql() {
if [ $# -eq 1 ]; then
echo "$1" | $MYSQL;
else
echo "$2" | $MYSQL $1;
fi
}
if [ $# -ne 3 ]; then
usage
exit 1
else
main $1 $2 $3
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment