Renaming a database becomes necessary when a schema name no longer matches an application, environment, or ownership model. A predictable database name keeps connection strings, permission scopes, backups, and monitoring aligned, reducing the chance of pointing automation at the wrong data.

In MySQL and MariaDB, a “database” is a schema namespace for tables, views, routines, triggers, and events. There is no single atomic RENAME DATABASE statement, so a rename is performed by creating a new schema and copying objects from the old schema, commonly by exporting with mysqldump and importing with mysql.

The copy is not instantaneous, so plan for a maintenance window on busy systems and enough disk space for a full SQL dump. Stored programs, events, and views that embed fully-qualified names such as old_db.table can keep pointing at the old schema until edited, and database-scoped privileges must be recreated on the new schema. Dropping the old schema permanently deletes data, and replicated servers can generate large binary logs during import.

Steps to rename a database:

  1. Quiesce application writes to the source schema during the export window.

    --single-transaction captures a consistent snapshot for transactional InnoDB tables, but pausing writes prevents post-snapshot drift before cutover.

  2. Read the source schema character set and collation.
    $ mysql --user=<admin_user> --password --execute "SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME='<old_db>';"
    Enter password:
    +----------------------------+------------------------+
    | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME |
    +----------------------------+------------------------+
    | utf8mb4                    | utf8mb4_unicode_ci     |
    +----------------------------+------------------------+

    Add --host and --port when connecting to a remote server.

  3. Create the target schema using the desired character set and collation.
    $ mysql --user=<admin_user> --password --execute "CREATE DATABASE <new_db> CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
    Enter password:

    No output indicates the schema was created.

  4. Dump the source schema to a SQL file including routines, triggers, and events.
    $ mysqldump --user=<admin_user> --password --single-transaction --quick --routines --triggers --events <old_db> > db-rename.sql
    Enter password:

    Avoid --databases for renames, since the dump can include a USE statement that restores into the original schema.

  5. Import the SQL dump into the target schema.
    $ mysql --user=<admin_user> --password --database=<new_db> < db-rename.sql
    Enter password:

    Importing into a non-empty schema can fail or create a partial copy; recreate the target schema to retry cleanly.

  6. Compare object counts between the source and target schemas.
    $ mysql --user=<admin_user> --password --execute "
    SELECT
      s.schema_name,
      (SELECT COUNT(*) FROM information_schema.tables t WHERE t.table_schema=s.schema_name AND t.table_type='BASE TABLE') AS tables,
      (SELECT COUNT(*) FROM information_schema.tables v WHERE v.table_schema=s.schema_name AND v.table_type='VIEW') AS views,
      (SELECT COUNT(*) FROM information_schema.routines r WHERE r.routine_schema=s.schema_name) AS routines,
      (SELECT COUNT(*) FROM information_schema.triggers tr WHERE tr.trigger_schema=s.schema_name) AS triggers,
      (SELECT COUNT(*) FROM information_schema.events e WHERE e.event_schema=s.schema_name) AS events
    FROM (
      SELECT '<old_db>' AS schema_name
      UNION ALL
      SELECT '<new_db>' AS schema_name
    ) s;
    "
    Enter password:
    +-------------+--------+-------+----------+----------+--------+
    | schema_name | tables | views | routines | triggers | events |
    +-------------+--------+-------+----------+----------+--------+
    | old_db      |     42 |     1 |        2 |        3 |      0 |
    | new_db      |     42 |     1 |        2 |        3 |      0 |
    +-------------+--------+-------+----------+----------+--------+

    Differences indicate missing routines/triggers/events in the dump, or errors during import.

  7. List the existing database-scoped grants for the application account.
    $ mysql --user=<admin_user> --password --execute "SHOW GRANTS FOR '<app_user>'@'<app_host>';"
    Enter password:
    +--------------------------------------------------------------------------------------------------+
    | Grants for app_user@app_host                                                                      |
    +--------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO `app_user`@`app_host`                                                       |
    | GRANT SELECT, INSERT, UPDATE, DELETE, EXECUTE, EVENT, TRIGGER ON `old_db`.* TO `app_user`@`app_host` |
    +--------------------------------------------------------------------------------------------------+
  8. Grant equivalent privileges on the new schema to the application account.
    $ mysql --user=<admin_user> --password --execute "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EXECUTE, EVENT, TRIGGER ON <new_db>.* TO '<app_user>'@'<app_host>';"
    Enter password:

    Match the privilege list to the SHOW GRANTS output; some applications require CREATE ROUTINE, ALTER ROUTINE, EXECUTE, EVENT, or TRIGGER.

  9. Switch application configuration to the new schema name.

    Update DSNs, ORM connection strings, backup jobs, and scheduled tasks that reference old_db.

  10. Drop the old schema after validation is complete.
    $ mysql --user=<admin_user> --password --execute "DROP DATABASE <old_db>;"
    Enter password:

    DROP DATABASE permanently deletes data; keep the SQL dump in durable storage before removing the old schema.

Discuss the article:

Comment anonymously. Login not required.