Last active
April 15, 2018 21:15
-
-
Save dakala/df343d915e1fe1a579b508f171255d92 to your computer and use it in GitHub Desktop.
Rename a MySQL database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Disclaimer - make backups, use at your own risk. | |
# | |
# Based on this comment: http://stackoverflow.com/a/13944924/843067 | |
# Views and stored procedures have to be done separately. | |
OLDDB="thunder" | |
NEWDB="phpedu" | |
MYSQL="mysql -u root -p " | |
$MYSQL -e "CREATE DATABASE \`$NEWDB\`;" | |
for table in `$MYSQL -B -N -e "SHOW TABLES" $OLDDB` | |
do | |
$MYSQL -e "RENAME TABLE \`$OLDDB\`.\`$table\` to \`$NEWDB\`.\`$table\`" | |
done | |
# You *might not* want to uncoment line below since | |
# in case of problems while renaming tables | |
# you will lose all database | |
mysql -e "DROP DATABASE \`$OLDDB\`;" | |
echo "Database renamed successfully" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment