Skip to content

Instantly share code, notes, and snippets.

@idettman
Last active August 29, 2015 14:27
Show Gist options
  • Save idettman/f494bbc4e499c67d2b1b to your computer and use it in GitHub Desktop.
Save idettman/f494bbc4e499c67d2b1b to your computer and use it in GitHub Desktop.
Shell script to rename a MySQL database by creating a new database and importing the old database data. usage> ./mysql-rename-db olddbname newdbname
GNU nano 2.2.6 File: ./mysql-rename-db.sh
#!/bin/bash
# update username and password where mysql is executed in this file
mysqlconn="mysql -u 'root' -p ''"
# export the data for the old database name
mysqldump -u root --password="" $1 > $2".sql";
# create a database for the new database name
echo "CREATE DATABASE $2; DROP DATABASE $1" | mysql -u root --password="";
# using the new database name, import the exported data from the old database
mysql -u root --password='' $2 < $2".sql";
# delete the exported data from the old database
rm ./$2".sql";
# output the renaming that took place
echo "database $1 renamed to $2";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment