Knowledge

How to rename a MySQL database

#Databases

Unlike renaming a table, you can't rename a database in MySQL sadly enough. So there is no SQL query you can execute to achieve this. However, it is possible with a workaround.

Published by Mark van Eijk on September 24, 2024
Updated on September 25, 2024 · 1 minute read

  1. Create a newly named database
  2. Dump & import
  3. Drop the old database

The most important step: To get going with this, first create a full backup of your MySQL database to make sure you don't loose any data!

Create a newly named database

This step involves creating the database you want to rename the old database to:

CREATE DATABASE `NEW_DATABASE_NAME`;

Dump & import

Dump and import the old database into your new database:

mysqldump -u USERNAME -p OLD_DATABASE_NAME | mysql -u USERNAME -p NEW_DATABASE_NAME

Drop the old database

If your really sure, you can (with the backup on hand) remove the old database safely:

DROP DATABASE `OLD_DATABASE_NAME`

Subscribe to our newsletter

Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!

Related articles

Stream MySQL backup directly to S3 bucket

Unlike renaming a table, you can't rename a database in MySQL sadly enough. So there is no SQL query you can execute to achieve this. However, it is possible with a workaround.

Read more →

Exporting database in MySQL using command line

Unlike renaming a table, you can't rename a database in MySQL sadly enough. So there is no SQL query you can execute to achieve this. However, it is possible with a workaround.

Read more →