The MySQL manual at MySQL covers this.
Usually I just dump the database and reimport it with a new name. This is not an option for very big databases. Apparently RENAME {DATABASE | SCHEMA} db_name TO new_db_name;
does bad things, exist only in a handful of versions, and is a bad idea overall.
This needs to work with InnoDB, which stores things very differently than MyISAM.
For those who are Mac users, Sequel Pro has a Rename Database option in the Database menu. http://www.sequelpro.com/
Simplest bullet-and-fool-proof way of doing a complete rename (including dropping the old database at the end so it's a rename rather than a copy):
Steps:
I posed a question on Server Fault trying to get around downtime when restoring very large databases by using MySQL Proxy. I didn't have any success, but I realized in the end what I wanted was RENAME DATABASE functionality because dump/import wasn't an option due to the size of our database.
There is a RENAME TABLE functionality built in to MySQL so I ended up writing a simple Python script to do the job for me. I've posted it on GitHub in case it could be of use to others.
Use these few simple commands:
Or to reduce I/O use the following as suggested by @Pablo Marin-Garcia:
For your convenience, below is a small shellscript that has to be executed with two parameters: db-name and new db-name.
You might need to add login-parameters to the mysql-lines if you don't use the .my.cnf-file in your home-directory. Please make a backup before executing this script.
Here is a one-line Bash snippet to move all tables from one schema to another:
The history command at the start simply ensures that the MySQL commands containing passwords aren't saved to the shell history.
Make sure that
db_user
has read/write/drop permissions on the old schema, and read/write/create permissions on the new schema.