MySQL
Database Management
Rename Database
SQL Commands
Schema Name Change

How do I rename a MySQL database (change schema name)?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

MySQL does not support a simple RENAME DATABASE command in modern versions, so “renaming a schema” is always a migration workflow. The safest pattern is to create a new schema, move or import objects, verify, then drop the old schema. The right method depends on downtime tolerance, object count, and whether cross-schema object references exist.

Why There Is No Direct Rename Command

Historically, direct database rename caused too many integrity and metadata risks across storage engines and server versions. Today, MySQL expects administrators to use explicit migration steps instead of one destructive shortcut. That means your rename process should be planned like a controlled cutover.

Method 1: Dump and Restore

For many environments, dump-and-restore is the clearest and safest approach.

1) Create target schema

sql
CREATE DATABASE new_schema CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;

2) Export old schema

bash
mysqldump -u app_user -p --routines --triggers --events old_schema > old_schema.sql

3) Import into new schema

bash
mysql -u app_user -p new_schema < old_schema.sql

4) Validate counts and key objects

sql
SELECT COUNT(*) FROM new_schema.some_table;
SHOW TRIGGERS FROM new_schema;
SHOW PROCEDURE STATUS WHERE Db = 'new_schema';

5) Re-point app and remove old schema after validation

sql
DROP DATABASE old_schema;

This method is straightforward and preserves object definitions when dump options are set correctly.

Method 2: Move Tables With RENAME TABLE

If you need a faster in-server move for tables, create new schema and rename table by table.

sql
CREATE DATABASE new_schema;
RENAME TABLE old_schema.orders TO new_schema.orders;
RENAME TABLE old_schema.customers TO new_schema.customers;

This is fast for table data movement, but there are caveats:

  • Views, routines, and events are not automatically migrated by table renames.
  • Stored routine definitions can contain schema-qualified references that still point to old schema.
  • Foreign keys and view dependencies need careful validation.

For complex databases, dump-and-restore is often less error-prone.

Generate Rename Statements Automatically

For large table sets, generate SQL safely from metadata.

sql
1SELECT CONCAT(
2  'RENAME TABLE `old_schema`.`', table_name,
3  '` TO `new_schema`.`', table_name, '`;'
4) AS stmt
5FROM information_schema.tables
6WHERE table_schema = 'old_schema'
7  AND table_type = 'BASE TABLE';

Review generated statements before execution. Do not run metadata-generated SQL blindly in production.

Privileges and Application Cutover

Schema rename workflows usually require privilege updates.

sql
GRANT SELECT, INSERT, UPDATE, DELETE ON new_schema.* TO 'app_user'@'%';
FLUSH PRIVILEGES;

Then update application configuration:

  • connection string schema name.
  • migration tool config.
  • reporting jobs and ETL scripts.

A clean cutover includes read and write smoke tests before deleting old schema.

Downtime and Consistency Strategy

If writes continue during migration, old and new schemas diverge. Choose one strategy:

  • short maintenance window and full cutover.
  • replication-style dual-write logic during transition.
  • read-only freeze before final export.

For most teams, planned maintenance window is simplest and safest.

Verification Checklist

Before final drop of old schema:

  1. Table counts match expected values.
  2. Critical queries run from app against new schema.
  3. Routines, triggers, and events exist and compile.
  4. Privileges and monitoring dashboards are updated.
  5. Backup exists for rollback.

Keeping this checklist explicit avoids irreversible cleanup mistakes.

Common Pitfalls

A common pitfall is moving tables and assuming routines and views moved automatically. Another issue is forgetting schema-qualified references inside SQL code, which still point to the old schema after migration. Teams also sometimes drop old schema before full application verification, which removes rollback options. Privilege mismatches are frequent when new schema grants are incomplete. Finally, large migrations without a downtime or consistency plan can lead to data drift between old and new schemas.

Summary

  • MySQL schema rename is a migration process, not a single command.
  • Dump-and-restore is usually the safest default for full object coverage.
  • RENAME TABLE can be fast but does not migrate all object types.
  • Update privileges and application config as part of cutover.
  • Verify thoroughly before dropping the old schema.

Course illustration
Course illustration

All Rights Reserved.