How do I quickly rename a MySQL database (change its schema name)?
Usually I just dump a database and re-import 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, exists 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 InnoDB, the following seems to work: create the new empty database, then rename each table in turn into the new database:
You will need to adjust the permissions after that.
For scripting in a shell, you can use either of the following:
OR
Notes:
-pand the password. If your database has no password, remove the-u username -ppasswordpart.If some table has a trigger, it cannot be moved to another database using above method (will result
Trigger in wrong schemaerror). If that is the case, use a traditional way to clone a database and then drop the old one:mysqldump old_db | mysql new_dbIf you have stored procedures, you can copy them afterwards:
mysqldump -R old_db | mysql new_db