Skip to content

Instantly share code, notes, and snippets.

@goldenbucks
Forked from tadas-s/mysql-rename-db.sh
Created February 9, 2017 04:27
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 goldenbucks/ea17ce60e59b8c351052fd88b0663d10 to your computer and use it in GitHub Desktop.
Save goldenbucks/ea17ce60e59b8c351052fd88b0663d10 to your computer and use it in GitHub Desktop.
MySQL database rename script
#!/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="old_db_name"
NEWDB="new_db_name"
MYSQL="mysql -u root -pyour_password "
$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\`;"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment