Rename MySQL Databases

A shell script for renaming MySQL databases by export and re-import under a new name.
created by on 2014-07-04

MySQL databases cannot be renamed, they must be exported and re-imported to rename them:

# Set Database Names
oldDB="oldname"
newDB="newname"

user="your-user-name"

# Backup existing DB
mysqldump --default-character-set="UTF8" -u $user -p -h localhost $oldDB > oldDB.sql

mysql --default-character-set="UTF8" -u $user -p -h localhost -e "CREATE SCHEMA `$newDB` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"

mysql --default-character-set="UTF8" -u $user -p -h localhost $newDB < $oldDB.sql

mysql --default-character-set="UTF8" -u $user -p -h localhost -e "DROP SCHEMA `$oldDB`;"

At least I don’t know a better way.

Tags:
Fork allmark on GitHub