How to rename a MySQL database?
The MySQL online manual has said about the RENAME DATABASE command (this documentation page has been removed by Oracle some time ago):
This statement was added in MySQL 5.1.7 but was found to be dangerous and was removed in MySQL 5.1.23.
So, how to proceed? The rationale: We started with a code name for the project and want the database name now to reflect the definitive name of the project.
From this blog post by Ilan Hazan:
In MySQL there is no support for database renaming. In order to rename a MySQL database you can do one of the following:
Create new database and rename all tables in the old database to be in the new database:
In Linux shell, use mysqldump to back up the old database, then restore the dumped database under a new name using the MySQL utility. Finally, use the drop database command to drop the old database. This option can perform badly for large database.
Write a simple Linux script (my favorite solution)
If all your tables are MyISAM, you can rename the old database folder name:
MySQL kinda sucks for this. The only solid reliable solution is to use
phpMyAdmin
.Login
--> clickScheme
--> clickOperations
--> findRename database to:
--> writeNewName
> clickGo
.As simple as that. All permissions are carried over.
I found a very simple solution: Shut down MySQL, rename the database directory and restart. That's all!
It's a bit dangerous if you have SQL code or data referring to the old name. Then you need to change that as well before you restart the application. But I didn't need to do that, but YMV.
Googling gives a few pointers like these two:
https://stackoverflow.com/questions/67093/how-do-i-quickly-rename-a-mysql-database-change-schema-name
http://www.delphifaq.com/faq/databases/mysql/f574.shtml
I tend to create a new database, and then dump the tables out of the old one, into a .sql file (with mysqldump), edit the file, do some kind of
s/old_database/new_database/g
and then reimport it into the new db.Probably not the best way to do it, but it does work.
If you have chance to use a MySQL Management-Tool (e.g. phpMyAdmin) then you can rename it easily as they create the query for you.
In phpMyAdmin they also create each table and insert the data by "INSERT INTO... SELECT * FROM...". So by chaining they copy the data over.
If you can't do this I would recommend to make a dump and re-import the sql-File into a new database.
Good luck!
Regards, Ben.
I used following method to rename the database
take backup of the file using mysqldump or any DB tool eg heidiSQL,mysql administrator etc
Open back up (eg backupfile.sql) file in some text editor.
Search and replace the database name and save file.
Restore the edited sql file