WordPress Database Connection Error: Safe Fix
Diagnose a WordPress database connection error safely by checking the database service, wp-config.php credentials, access, and table health in order.
- Published

The WordPress “Error establishing a database connection” means WordPress cannot complete a connection to the database named in `wp-config.php`. Check whether the database service is available first, then compare the four connection values with the hosting account; repair tables only when the evidence points to damaged tables rather than a failed login or stopped server.
Quick diagnosis
The timing and scope narrow the cause:
- If the error started immediately after a migration, password reset, restore, or `wp-config.php` edit, verify the database name, user, password, and host first.
- If several sites using the same hosting account fail at once, or the error began without a site change, check the database service, account quota, and host status before editing files.
- If the public site shows the connection error but `/wp-admin/` reports that database tables may need repair, the server may be reachable while one or more tables are unhealthy.
- If the error appears only intermittently, save timestamps and ask the host to check connection limits, restarts, resource pressure, and database logs. Repeatedly changing credentials will not fix an overloaded service.
This message is different from a WordPress critical error. A critical error means PHP reached WordPress and encountered a fatal failure; a database connection error happens earlier, while WordPress is trying to reach or select its database.
Before you change the database
Confirm that you have:
- a restorable backup of both the site files and database;
- SFTP, SSH, or the host’s secure file manager;
- access to the hosting database panel or a support contact;
- the exact time the failure began and any change made just before it;
- a safe place to record values without pasting passwords into tickets, screenshots, or chat.
Do not delete tables, create a new empty database, import an old backup, or enable a public repair endpoint as a first response. Those actions can turn an availability problem into data loss.
1. Confirm the database service is available
Open the hosting status and database section. Check whether the database server is running, the account has reached a storage or connection limit, and other applications on the same database host can connect.
On a managed host, ask support to confirm:
- the database service is accepting connections;
- the correct database hostname and port for this site;
- the database has not been suspended or exceeded a quota;
- the named database and user still exist;
- the first relevant server-side error at the incident time.
Expected observation: if the database service is stopped or the account is suspended, restore that service before editing WordPress. A correct `wp-config.php` cannot connect to an unavailable database server.
2. Compare the four WordPress connection values
Download a backup copy of `wp-config.php`, then open the file in a plain-text editor. Compare these constants with the values shown by the host:
```php define( ‘DB_NAME’, ‘database_name’ ); define( ‘DB_USER’, ‘database_user’ ); define( ‘DB_PASSWORD’, ‘database_password’ ); define( ‘DB_HOST’, ‘database_host’ ); ```
Compare character for character. Database names and users may include an account prefix, and `DB_HOST` is not always `localhost`. WordPress documents host forms that can include a port or socket when the provider requires one.
Do not paste real credentials into a public command, log, screenshot, or support thread. If the values already match the control panel, save no change and continue.
3. Check whether the configured login works
If SSH and WP-CLI are already available on the server, run this from the WordPress root:
```bash wp db check ```
The command reads the database constants from `wp-config.php` and runs the database check utility. A successful result confirms that those credentials can reach and inspect the configured database at that moment. An authentication, hostname, socket, or connection-refused error points back to access or service availability; record the exact error without sharing the password.
If WP-CLI is not installed, use the host’s database connection test or ask support to test the existing user against the existing database. Do not install a new database tool on a production server during an outage solely for this step.
4. Restore access without replacing content
If the database user or password is wrong, correct access in the hosting panel:
- Confirm the user is assigned to the intended database with the required privileges.
- If necessary, reset that user’s password through the host’s approved control.
- Update only `DB_PASSWORD` in the backed-up `wp-config.php`.
- Upload the file through the secure connection and reload the site once.
If a migration changed the database host, name, or user, update only the value the host confirms. Keep WordPress authentication keys, salts, table prefix, and unrelated settings unchanged.
Expected observation: the front end and `/wp-admin/` connect again. If the error remains, restore the previous `wp-config.php` copy unless the host has confirmed the new credentials are authoritative.
5. Repair tables only when a check identifies damage
Do not assume every connection error means database corruption. A repair cannot fix a stopped service, wrong password, incorrect host, missing user, or blocked connection.
If `wp db check`, the hosting database tool, or a specific WordPress admin message identifies damaged tables, take a fresh database backup and ask the host whether the table engine supports repair. With WP-CLI already installed, the official repair command is:
```bash wp db repair ```
Run it only against the confirmed database and preserve the output. If it reports a table it cannot repair, stop and escalate rather than repeating the command or dropping the table.
6. Investigate an intermittent return
When the site recovers and fails again, collect evidence before changing configuration:
- the UTC timestamp and affected URLs;
- whether wp-admin and other sites on the account failed too;
- the database and PHP error-log entries from the same minute;
- connection-count, storage, CPU, or memory alerts available from the host;
- whether a backup, import, deployment, or security rule was active.
Ask the host to correlate those timestamps with database restarts, rejected logins, connection exhaustion, storage limits, or network failures. A transient outage should not be reported as fixed merely because one reload succeeds.
Verify the recovery
The incident is resolved only when:
- the home page, an uncached internal page, and `/wp-admin/` load;
- `wp db check` or the host’s equivalent connection test succeeds;
- a new post can be saved on staging or a low-risk production check confirms writes work;
- the PHP and database logs show no recurring connection failure;
- the site remains available after the normal page and object caches are restored;
- the backup and final credential change are documented securely.
Rollback and escalation
If a `wp-config.php` edit does not restore access, put back the saved copy. If a password reset breaks another application that uses the same database user, restore the prior credential through the host or update every authorized consumer together.
Escalate with the hostname, incident time and time zone, affected front-end and admin URLs, exact database error with secrets removed, confirmed `DB_HOST` form, WP-CLI or host test result, and the last relevant database-server log entry. Restore a full backup only when the database content itself is known to be damaged or lost; a restore is not the normal fix for a refused connection.
For a PHP fatal error that appears after the connection is restored, continue with the WordPress critical error guide rather than making more database changes.
Source and version notes
This guide was source-reviewed on October 17, 2026, against the WordPress Advanced Administration Handbook, the current `wp-config.php` reference, and official WP-CLI database check and repair documentation. It was not reproduced against a live hosting account, so database panel labels, hostnames, supported repair operations, and escalation controls require confirmation in the target environment.


