While working with the databases, many times you may need to rename a database. For security purposes, MySQL had dropped the direct command to rename a database from MySQL 5.1.23. So there is no direct command to the T-SQL statement available for renaming a database in MySQL server.
You can follow one of the below instructions to rename a MySQL database with the help of cPanel, phpMyAdmin, or command line as per the availability. After renaming the database, remember that you need to reconfigure the permission on the new database for the users. In this tutorial, you will find three methods to rename a MySQL database.
1. Rename MySQL Database with Command Line
As you know that there is no direct command or SQL statement available for renaming the database in the MySQL server. But you can still change the database name using backup and restore options.
- First, take a backup of the current database:
mysqldump -u root -p old_db > old_db.sql
- Then create a new database with the desired name in the MySQL server.
mysqladmin -u root -p create new_db
- Finally, restore the backup taken above to the newly created database.
mysql -u root -p new_db < old_db.sql
You have a new database with a new name. Verify the new database and make sure that restore completely and functioning properly.
2. Rename MySQL Database with phpMyAdmin
phpMyAdmin is the most popular web application used for managing MySQL databases. It provides you an option to rename the database in the MySQL server.
All done. Here phpMyAdmin will create a new database with a new name and copy all content from the old one. After that drop the old database.
3. Rename MySQL Database with cPanel
cPanel is a web-based control panel for the CentOS and Redhat Linux systems. It is popular among shared hosting providers. You can easily rename a MySQL database with the help of cPanel.
The cPanel offers the easiest way to rename a MySQL database.
- Log in to the cPanel.
- Go to the Databases section and click MySQL Databases.
- Scroll down to this page, you will find the list of databases under the Current Databases section.
- Click the
"rename"button in front of the database to be rename.
- Provide a new database name and click Proceed.
That’s it. You have successfully renamed a MySQL database 😎