Skip to content

Repairing a corrupted cPanel database map without losing customer grants

When the cPanel dbmap and MySQL grant tables disagree, the UI lies about what an account owns. `dbmaprepair` reconciles them — but the order of operations matters.

A cPanel customer logs into MySQL Databases and sees a database they don’t own, or doesn’t see one they do. The application works fine — credentials are correct, queries succeed — but the cPanel UI is confused. Backups for the account skip the missing database. WHM > List Accounts shows wrong database counts. The dbmap (cPanel’s per-account database registry) has drifted from MySQL’s actual grant tables. dbmaprepair is the tool that reconciles them, but running it without understanding what it does can re-introduce permissions that you deliberately revoked.

What’s happening

cPanel stores per-account database metadata in two places:

  • /var/cpanel/databases/<user>.json (newer format) or /var/cpanel/databases/users.db.cache (older).
  • MySQL grant tables: mysql.user, mysql.db, mysql.tables_priv.

cPanel maintains both. When they diverge, it’s usually because:

  • Someone ran raw GRANT or DROP USER as MySQL root, bypassing cPanel.
  • A database was created or dropped directly via mysql, not via the API/UI.
  • An account was moved between servers and the dbmap copy didn’t.
  • A failed JetBackup restore left partial state.

dbmaprepair rebuilds the dbmap from MySQL’s actual state. It does NOT modify MySQL — it brings the cPanel view into agreement with MySQL. So if MySQL has a grant that shouldn’t be there (legacy user nobody remembered to drop), dbmaprepair writes it into the dbmap as a legitimate grant.

How to verify

whmapi1 list_dbs user=acmecorp
ls -la /var/cpanel/databases/acmecorp.*
mysql -e "SHOW DATABASES LIKE 'acmecorp\\_%'"
mysql -e "SELECT User, Host FROM mysql.user WHERE User LIKE 'acmecorp\\_%'"
mysql -e "SELECT Db, User, Host FROM mysql.db WHERE User LIKE 'acmecorp\\_%'"
/scripts/dbmap_check  # cPanel's own check

Compare what whmapi1 says the account owns to what MySQL actually has. Differences are dbmap drift. dbmap_check runs cPanel’s verification; it reports inconsistencies but doesn’t fix them.

The fix

  1. Take backups first — both filesystem and a mysqldump:
    cp -r /var/cpanel/databases/ /backup/dbmap-$(date +%F)/
    mysqldump --all-databases --single-transaction | gzip > /backup/mysql-$(date +%F).sql.gz
  2. Audit the discrepancy. Before running dbmaprepair, understand which way the drift goes:
    • dbmap missing items that MySQL has: someone created a database/user directly in MySQL, cPanel didn’t know.
    • dbmap has items MySQL doesn’t: someone dropped a database/user directly in MySQL, cPanel still thinks it exists.
    • Both have the same items but with different permissions: cPanel says ALL, MySQL says SELECT only (or vice versa).
  3. For drift caused by raw MySQL operations, decide whether to keep the MySQL state or restore the cPanel state:
    • If a customer asked for a privilege removed and someone did it via raw SQL, you want to keep MySQL state — run dbmaprepair.
    • If a runaway script created users you don’t recognize, you want to remove them from MySQL, then run dbmaprepair to clean dbmap.
  4. Repair the dbmap for one user:
    /usr/local/cpanel/bin/dbmaprepair --user=acmecorp
    Inspects MySQL’s grants for acmecorp_% users and writes them into the dbmap. Existing dbmap entries that don’t match are overwritten.
  5. Repair for all users:
    /usr/local/cpanel/bin/dbmaprepair
    Iterates over all accounts. Heavy on a host with hundreds of users — runs for several minutes.
  6. Verify post-repair:
    whmapi1 list_dbs user=acmecorp
    /scripts/dbmap_check
    The check should report clean. The list should match MySQL’s reality.
  7. For “the customer wants a database back that I dropped” — restore from JetBackup (separate article). dbmaprepair only reconciles the registry; it doesn’t recover data.
  8. For prevention, never run raw GRANT, CREATE USER, DROP USER, CREATE DATABASE, DROP DATABASE against acmecorp_% namespace from root mysql. Use uapi or whmapi1. This is documented in the grants article but bears repeating — every dbmap repair is downstream of someone bypassing the API.

For environments where dbmap drift is suspected across many accounts, Stack Harbor’s WHM server management team runs dbmap_check on a scheduled cadence and audits the diffs against expected state before repair, so legitimate state changes aren’t silently overwritten.