Skip to content

Instantly share code, notes, and snippets.

@bingalls
Last active November 9, 2015 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bingalls/8cfa80640749d9ebbb4a to your computer and use it in GitHub Desktop.
Save bingalls/8cfa80640749d9ebbb4a to your computer and use it in GitHub Desktop.
Mysql hack, to rename databases
#!/bin/bash
# renameDatabase.sh Rename a Mysql database, by moving all its tables
if [ $# -ne 2 ];then
echo "Usage: `basename $0` srcDatabase destDatabase"
echo 'Both database schemas must already exist. Escape any spaces in password with "\"'
exit
fi
echo "About to bulk rename $1 tables into $2. Control-C to quit"
read -sp "Please enter root database password" PASSWORD
# Avoid completion; No column names; Silence column formatting
for i in `mysql -ANsu root $1 -p$PASSWORD -e "SHOW TABLES"`; do
mysql -ANsu root -p$PASSWORD -e "RENAME TABLE $1.$i TO $2.$i"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment