How to rename a MySQL database (change schema name)
Codemia Knowledge Hub ·
MySQL has no RENAME DATABASE command in modern versions, so renaming a schema is always a migration: create the new schema, move or import the objects, verify, then drop the old one. Which method fits depends on how much downtime you can accept, how many objects there are, and whether objects reference each other across schemas.
Why there is no rename command
A direct database rename historically caused too many integrity and metadata problems across storage engines and server versions. MySQL now expects administrators to use explicit migration steps, so plan the rename as a controlled cutover.
Method 1: dump and restore
For most environments this is the clearest and safest approach.
1) Create the target schema
2) Export the old schema
3) Import into the new schema
4) Check counts and key objects
5) Re-point the application, then drop the old schema
With the right dump options (--routines --triggers --events), this preserves object definitions.
Method 2: move tables with RENAME TABLE
For a faster move inside the same server, create the new schema and rename tables into it one by one:
This moves table data quickly, with caveats:
- Views, routines and events are not migrated by table renames. Moving tables and assuming the rest came along is a common mistake.
- Stored routine definitions can contain schema-qualified references that still point to the old schema.
- Foreign keys and view dependencies need careful checking.
For complex databases, dump and restore is usually less error-prone.
Generating the rename statements
For many tables, generate the SQL from metadata:
Review the generated statements before running them. Do not run metadata-generated SQL blindly in production.
Privileges and application cutover
The new schema needs its own grants, and incomplete grants are a frequent problem:
Then update application configuration:
- the schema name in connection strings
- migration tool config
- reporting jobs and ETL scripts
Run read and write smoke tests against the new schema before deleting the old one.
Keeping data consistent during the move
If writes continue during the migration, the old and new schemas diverge. Pick one strategy:
- a short maintenance window and full cutover
- replication-style dual writes during the transition
- a read-only freeze before the final export
For most teams, a planned maintenance window is the simplest and safest.
Checklist before dropping the old schema
- Table counts match expected values.
- Critical queries run from the app against the new schema.
- Routines, triggers and events exist and compile.
- Privileges and monitoring dashboards are updated.
- A backup exists for rollback.
Dropping the old schema before the application is fully verified removes your rollback option.
Related reading
- How do I rename fields when performing search/projection in MongoDB?
- How do I restore a dump file from mysqldump?
- How do I retrieve my MySQL username and password?
- How do I return the index of the target element in a Python array?
- How do I see all foreign keys to a table or column?
- How do I select an entire row which has the largest ID in the table?
- How do I set the Hibernate dialect in SpringBoot?
- How do I set the time zone of MySQL?
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.