Introduction to MySQL rename database
Sometimes, the database administrator may feel a necessity to change the name of the database that is present on a database server such as, when the project is to be deployed to the client-side or new project needs to share the same database and now, you feel that the existing name of the database is not that relevant or meaningful as per the new context. The older versions of MySQL that is versions before MySQL 5.1.23 had provided the simple method to rename the database using the RENAME DATABASE command. But this proved to be dangerous concerning the security and hence it was removed in all the new versions of MySQL starting from 5.1.23. In this article, we will learn about some of the techniques that can be used to change the name of the database safely and effectively.
How to Rename the database?
Many methods can be followed to rename the database. Out of them, which method is to be used depends on the context like how big is your database, what type of tables are present in your database, and so on. If your MYSQL version is older than MySQL 5.1.23, you can make the use of following commands:
RENAME DATABASE existing_DB_name TO name_Of_new_DB
ALTER DATABASE existing_DB_name MODIFY NAME = name_Of_new_DB
Methods of Renaming the database
However, this method is not supported in the latest versions of MySQL for security reasons. Below is the list of some of the methods that can be followed to rename the database in the latest versions of MySQL:
Create a new database with the name that you wish the older database to be changed. Rename all the tables of the old database to new database names and then drop the old database.
Dump the existing database to generate backup and restore the backup with a new name of the database.
Write the script in case you are using the Linux platform that will contain the code to get all the tables from the database and renaming all the tables to the database to the new database name. tablename format.
If all the tables present in your existing database are of type MyISAM then you can use one trick. Just close your MySQL server and then rename the directory of your database i.e the folder of the database then that will automatically lead to a changed database name with the changed directory name.
Let us see each of the methods in detail one by one:
1. Renaming the tables
Step 1: The first step that we need to perform is to create a new database with the name with which we wanted to rename the existing database. For that, you can simply execute the following statement.
CREATE database new_Name_Of_DB;
Explanation: where new_Name_Of_DB is the name of the new database.
Step 2: The next step is to rename the names of tables of the existing database. As we know that the table is completely referred to by using the name of the database in which it resides then dot operator and then the actual table name. So here we will rename it to the new database name then dot and then the table name that will lead the table to be placed and belong to the new database. This can be done in the following manner.
RENAME TABLE old_Name_Of_DB.presentTable1 TO new_Name_Of_DB.presentTable1, old_Name_Of_DB.presentTable2 TO new_Name_Of_DB.presentTable2;
Step 3: The last thing to do is to drop the old database that existed and you wanted to rename. But before that, you can make sure that all your tables are present in the new database and the old database is empty to avoid data loss. Then you can execute the DROP DATABASE statement in the following manner:
DROP database old_Name_Of_DB;
Explanation: where old_Name_Of_DB is the name of your old database that existed and you wanted to rename.
2. Dump and restore the database
One of the methods that can be followed to rename the database for small databases easily and effectively is to dump the database to create a backup file. Then to restore the backup file with the new name of the database that you wished to have. But note that this method works well only with small databases as it creates a problem if the data is sufficiently big and large while backup and restoration.
Step 1: The first step is to create a new database for which you can make the use of the following query:
mysql -u uname -p "pass" -e "CREATE DATABASE new_Name_Of_DB"
Explanation: where new_Name_Of_DB is the name which you wanted to assign to the existing name and uname is the username of MySQL and pass is the password of your user login if set.
Step 2: The next step is to export the backup file that dumps the existing database with the name old_Name_Of_DB.
mysqldump -u uname -p "pass" old_Name_Of_DB > exportedDatabaseFile.sql
Step 3: Now. You will have to import the database backup that restores the data from the exported file, in my case exportedDatabaseFile.sql to the new database named new_Name_Of_DB in my case using the following query statement:
mysql -u uname -p "pass" new_Name_Of_DB < exportedDatabaseFile.sql
Step 4: Finally, you can drop the old database using the following query statement.
mysql -u uname -p "pass" -e "DROP DATABASE old_Name_Of_DB"
3. script to be executed on command-line
You can even execute the following one-line script instead of converting each table’s name as in the first method. The script statement is somewhat like the following where you can replace the old_Name_Of_DB and new_Name_Of_DB with your existing and new database names.
$ mysql -u uname -p "pass" old_Name_Of_DB -sNe 'show tables' | while read table; do mysql -u uname -p "pass" -sNe "RENAME TABLE old_Name_Of_DB.$table TO new_Name_Of_DB.$table";
We cannot make use of the RENAME DATABASE command in the newer versions of MySQL for renaming the database. However, there are different alternatives available that can be followed to rename the database. Some of them are renaming the names of tables to new database names, while another method suggests dumping the database to create the backup file and then restoring it with another name that is the new name of the database. One of the techniques that can be used if all of the tables inside your database are of MyISAM type is to shut down the MySQL server, rename the database folder name to a new name with which you wish to rename the database, and the restart MySQL server.
This is a guide to the MySQL rename database. Here we an introduction to MySQL rename database, how to do it, methods with an explanation. You can also go through our other related articles to learn more –