Skip to content

Renaming a cPanel database when MySQL has no RENAME DATABASE — the dump-and-reload pattern

MySQL never implemented RENAME DATABASE because of corner cases. On cPanel, the pattern is dump-create-load-drop with dbmap updates at both ends.

A customer asks to rename acmecorp_temp to acmecorp_production. The natural SQL would be RENAME DATABASE. MySQL never shipped it — and MariaDB removed an early implementation in 10.4 because of edge cases with triggers, foreign keys, and stored procedures referencing the old name. cPanel’s UI offers no native rename either. The path is: create the new database, dump the old, load into new, update app config, drop the old. With the cPanel dbmap to keep in sync at each step.

What’s happening

The reason RENAME DATABASE doesn’t work in practice: every stored procedure, trigger, foreign key, and view that references the database by name needs updating too. SQL identifiers are not just strings — they have references that travel with them. MySQL’s choice was to not ship a partial solution; renaming becomes an application-aware operation.

On cPanel, you also have the dbmap to think about. The new database needs to be registered under the account so the cPanel UI, backups, and account migrations all see it.

How to verify

Before:

whmapi1 list_dbs user=acmecorp
mysql -e "SELECT TABLE_SCHEMA, COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA='acmecorp_temp' GROUP BY TABLE_SCHEMA"
mysql -e "SHOW TRIGGERS FROM acmecorp_temp"
mysql -e "SHOW PROCEDURE STATUS WHERE Db='acmecorp_temp'"
mysql -e "SELECT CONSTRAINT_SCHEMA, CONSTRAINT_NAME, REFERENCED_TABLE_SCHEMA FROM information_schema.REFERENTIAL_CONSTRAINTS WHERE REFERENCED_TABLE_SCHEMA='acmecorp_temp' OR CONSTRAINT_SCHEMA='acmecorp_temp'"

The triggers, procedures, and foreign keys are the things that will need attention after rename. Cross-database references are rare in cPanel deployments but exist.

The fix

  1. Create the new database under the same account, via the API:
    uapi --user=acmecorp Mysql create_database name=production
    This creates acmecorp_production and registers it in the dbmap.
  2. Create a user for the new database (or reuse an existing one):
    uapi --user=acmecorp Mysql create_user name=production password='strong-password'
    uapi --user=acmecorp Mysql set_privileges_on_database user=acmecorp_production database=acmecorp_production privileges=ALL
  3. Dump the old database:
    mysqldump --single-transaction --routines --triggers --events --hex-blob --default-character-set=utf8mb4 acmecorp_temp > /tmp/rename-source.sql
  4. Adjust any references in the dump before importing. If procedures or triggers reference the old database name explicitly:
    sed -i 's/`acmecorp_temp`/`acmecorp_production`/g' /tmp/rename-source.sql
    sed -i 's/acmecorp_temp\./acmecorp_production./g' /tmp/rename-source.sql
    The second pass catches unquoted references in stored procedure bodies. Inspect the diff before importing.
  5. Load the modified dump:
    mysql acmecorp_production < /tmp/rename-source.sql
  6. Update the application to point at the new database name. Application restart usually required. Verify by hitting the app.
  7. Verify functional parity: row counts on key tables, trigger fires, stored procedure invocations all behave the same. This is where the work earns its keep — the rename isn’t complete until the application running against the new database is provably equivalent.
  8. Drop the old database after a confidence period:
    uapi --user=acmecorp Mysql delete_database name=temp
    This removes acmecorp_temp from MySQL and from the dbmap. Confirm with whmapi1 list_dbs user=acmecorp.
  9. Update backups, monitoring, and any cron that referenced the old database name. The old name disappears from cPanel’s view but external references don’t update themselves.

For environments where database renames are part of regular ops (staging promotions, ownership changes), Stack Harbor’s managed cPanel operations team scripts the create-dump-modify-load-verify-drop sequence with a checklist for cross-database references.