Skip to content

Instantly share code, notes, and snippets.

@preethamhegdes
Forked from tadas-s/mysql-rename-db.sh
Last active August 29, 2015 14:03
Show Gist options
  • Save preethamhegdes/1c37aab26104136ff517 to your computer and use it in GitHub Desktop.
Save preethamhegdes/1c37aab26104136ff517 to your computer and use it in GitHub Desktop.
#!/bin/bash
# https://gist.github.com/preethamhegdes/1c37aab26104136ff517
# Disclaimer - make backups, use at your own risk.
# Updated version of gist : https://gist.github.com/tadas-s/5411299
# Based on this comment: http://stackoverflow.com/a/13944924/843067
# Views and stored procedures have to be done separately.
#Note : Please configure mysql username and password properly (Line number 17)
echo "Will convert the DB and its tables uppercase into smaller case name."
echo -n "Enter OLD DB Name : "
read OLDDB
NEWDB=$(echo $OLDDB | tr '[:upper:]' '[:lower:]')
MYSQL="mysql -u root -ppass"
$MYSQL -e "CREATE DATABASE \`$NEWDB\`;"
for table in `$MYSQL -B -N -e "SHOW TABLES" $OLDDB`
do
NEWTABLE=$(echo $table | tr '[:upper:]' '[:lower:]')
$MYSQL -e "RENAME TABLE \`$OLDDB\`.\`$table\` to \`$NEWDB\`.\`$NEWTABLE\`"
echo "Table : " $table " renamed to " $NEWTABLE
done
echo "Renamed " $OLDDB " to " $NEWDB
# 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