DelphiFAQ Home Search:

mysql: How can I rename a database

 

comments8 comments. Current rating: 4 stars (4 votes). Leave comments and/ or rate it.

Question:

I want to rename my database from 'TEST' to something more meaningful. Do I have to make a backup, modify the SQL script and then restore it?

Answer:

While that would work, there is an easier way. Shutdown the mysql server, rename the directory that stores the database from 'TEST' to your desired name. This 'TEST' directory is below the data directory. If you don't know where that is, try:

$ mysqladmin variables | grep datadir

After renaming, restart the MySQL server and check the grants - they might still contain references to the old database name.

You can also use a tool like phpMyAdmin (web interface to mysql) which has an option to rename a database.


Comments:

2006-02-24, 18:13:55
anonymous from Hungary  
rating
2006-03-13, 00:51:59
anonymous from United States  
rating
Worked perfectly!

If you're renaming the DB by command line, you'll want to grant user permissions to the new DB and delete permissions granted for the old DB. At a minimum, you'll need to run the following SQL:

remove from mysql.db where db='oldDBname';
2006-04-04, 07:02:07
[hidden] from Italy  
try this..
$ mysqladmin create new_db
$ mysqldump --opt old_db | mysql new_db

mysql>; revoke all from old_db_user@host;
2006-05-21, 09:39:19
amir_habibi@yahoo.com from United States  
rating
MySQL 5.x added rename database
try rename table for Mysql 4.x
2006-07-09, 02:34:00
anonymous from Israel  
Actually, the rename statement is only available from version 5.1.7 (according to the MySQL documentation).
2007-10-25, 14:43:27
anonymous from Slovenia  
And, as I read in the MySQL docs, it was removed due to it's serurity-breach-iness. It seems as if there is no way to do this... Copying to a new DB seems reasonable, but I can't believe there's no RENAME DB COMMAND available...
2008-01-21, 07:49:01
anonymous  
I think that renaming the directory will cause trouble for those of you using stored procedures.
2008-05-30, 01:01:25
anonymous from Australia  
rating
not being able to just rename a schema blows ass

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
Leave your comment here:
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.