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.
The simple way
Change to the database directory:
Shut down MySQL... This is important!
Okay, this way doesn't work for InnoDB or BDB-Databases.
Rename database:
...or the table...
Restart MySQL
Done...
OK, this way doesn't work with InnoDB or BDB databases. In this case you have to dump the database and re-import it.
This is what I use:
Most of the answers here are wrong for one of two reasons:
Percona has a blog post about how to do this well: https://www.percona.com/blog/2013/12/24/renaming-database-schema-mysql/
and script posted (made?) by Simon R Jones that does what is suggested in that post. I fixed a bug I found in the script. You can see it here:
https://gist.github.com/ryantm/76944318b0473ff25993ef2a7186213d
Here is a copy of it:
Save it to a file called
rename_db
and make the script executable withchmod +x rename_db
then use it like./rename_db localhost old_db new_db
TodoInTX's stored procedure didn't quite work for me. Here's my stab at it:
Here is a quick way to generate renaming sql script, if you have many tables to move.
In MySQL Administrator do the following: