Repair WordPress database settings in wp-config.php
Check and repair WordPress database settings in wp-config.php, including database name, user, password, host, charset, and table prefix.
When to use this guide
Use this guide when WordPress shows Error Establishing a Database Connection, especially after a migration, password rotation, host move, database rename, or restore from backup.
Before you start
- Back up
wp-config.phpbefore editing. - Open the real WordPress root, not
wp-config-sample.php. - Do not paste database passwords, salts, or keys into public tickets or screenshots.
- Use the hosting panel or secret manager as the source of truth for credentials.
Step 1: Locate the database constants
define('DB_NAME', 'database_name_here');
define('DB_USER', 'username_here');
define('DB_PASSWORD', 'password_here');
define('DB_HOST', 'localhost');
Expected output is one active set of constants. If a managed host uses environment variables or an included config file, follow that pattern rather than adding conflicting constants.
Step 2: Compare each value
| Constant | What to verify | Common migration issue |
|---|---|---|
DB_NAME | Exact database name | Old database name after restore |
DB_USER | User assigned to the database | User exists but lacks privileges |
DB_PASSWORD | Password from host panel or secret store | Password rotated in panel but not file |
DB_HOST | Host, port, or socket required by provider | Using localhost when host requires a remote hostname |
$table_prefix | Prefix matching imported tables | Imported tables use wp_ but file expects another prefix |
Step 3: Test the database login safely
mysql -h DB_HOST -u DB_USER -p DB_NAME
Expected output is a successful MySQL/MariaDB prompt. If login fails, the credentials or privileges are wrong. If login works but WordPress fails, check host-specific socket/port syntax, table prefix, PHP database extensions, or whether WordPress is reading a different config file.
Step 4: Check charset and collate without guessing
Most sites should keep their existing DB_CHARSET and DB_COLLATE. Do not change charset during an outage unless a migration guide or host support confirms it. Incorrect charset changes can make recovery harder.
Managed hosting notes
Some providers store database credentials outside editable files or regenerate wp-config.php. In those environments, compare panel values, database user permissions, and selected PHP app root before editing manually.
Expected outputs
After a correct credential repair, direct database login succeeds, WordPress stops showing the database connection message, and the access log changes from a database failure response to a normal page load or a different actionable error. If direct login fails with Access denied, fix user/password/privileges. If it fails with Unknown database, fix the database name or restore/import the missing database. If it fails with connection timeout, check DB_HOST, firewall, socket, port, and database service health.
If it still fails
Confirm that the WordPress files you edited are the files served by the domain. Multi-site, staging-to-production copies, symlinks, and container mounts often leave an old wp-config.php active in a different path. Also confirm that the table prefix matches the imported tables; a valid login with a wrong prefix can lead to a different WordPress error after the database connection is restored.
Verify
Reload the site and wp-admin once. Confirm the database connection error disappears, then check the HTTP status and WordPress debug log. If the site changes to a 500, switch to WordPress 500 troubleshooting.
Rollback or escalate
If the edit fails, restore the backup of wp-config.php. Escalate with the redacted database constants, database host, database name, table prefix, migration date, and whether direct database login works. Never send the real DB_PASSWORD or salts in plain text.
Review notes
- Last reviewed
- 2026-05-05
- Reviewed by
- FaultForge Editorial Team, Web operations reviewer
- Tested on
WordPress wp-config.php database constants, migration credential checks, table prefix review, secret redaction, and managed-hosting database panel comparison.