
Seeing the words “Error establishing a database connection” on your site is alarming. Your pages vanish. Your visitors see a plain white screen. And it looks like everything is broken. The good news is that a wordpress database connection error is one of the most fixable problems in WordPress. In most cases, the cause is small — a wrong password in a config file, a database server having a bad moment, or a table that needs a repair. This guide walks you through 8 fixes in order, from the simplest checks to the last-resort options, so you can reconnect your site step by step.
- Quick Checks — Do These First
- Diagnose Table: Symptom, Cause and Fix
- 1. Check Whether wp-admin Still Works
- 2. Check Your Host’s Server Status Page
- 3. Verify the Database Credentials in wp-config.php to Resolve the WordPress Database Connection Error
- 4. Look for Stray Characters in wp-config.php
- 5. Confirm the Database Exists and the User Has Privileges
- 6. Repair Corrupted Database Tables
- 7. Fix Post-Migration Mismatches
- What to Avoid
- FAQ
- Conclusion
Quick Checks
- Try reaching your WordPress dashboard. Go to
yoursite.com/wp-admin. If the dashboard loads but the front end shows the error, your database is probably corrupted — skip to Fix 6. - Confirm the error is real, not your browser. Open the site on your phone or in a private window. If pages will not load in any browser, check our guide on Chrome pages not loading to rule out a browser problem first.
- Think about what changed recently. Did you migrate hosts, change a hosting password, or install a new plugin? The answer points straight at the cause.
- Check your hosting account’s status page. If the database server is down, every site on that server fails at once — and only your host can fix it.
- Do not panic and do not start deleting things. A wordpress database connection error almost never means your content is gone. Your posts and pages are still sitting in the database.
- Make a backup before you touch anything. Download a copy of
wp-config.phpand, if you can, a database export from your hosting panel. You will need them if a fix goes wrong.
Diagnose Table
| Symptom | Likely cause | Go to fix |
|---|---|---|
| Error appears right after moving hosts or changing a password | Wrong database credentials in wp-config.php | Fix 3 |
| Error on both the site and wp-admin, host status page shows an incident | Database server down or overloaded | Fix 2 |
| wp-admin works but the front end shows the error | Corrupted database tables | Fix 6 |
Error started after editing wp-config.php by hand | Typo or stray characters in the file | Fix 4 |
| phpMyAdmin shows the database missing or “access denied” | Database deleted or user lost its privileges | Fix 5 |
| Error appeared after a migration, credentials look right | Wrong DB_HOST value or table prefix mismatch | Fix 7 |
| Nothing above works and you have a backup | Restore the database from backup | Fix 8 |
1. Check Whether wp-admin Still Works
This is the fastest diagnostic you have. It costs nothing and changes nothing.
- Type
yoursite.com/wp-admininto your browser (replaceyoursite.comwith your real domain). - Watch what happens. There are two possible outcomes.
- If the login page appears, your database credentials are fine. WordPress can reach the database, because the dashboard needs the database to load. The problem is more likely corrupted tables affecting the front end. Move on to Fix 6.
- If the same error appears, WordPress cannot connect to the database at all. The cause is usually wrong credentials or a database server problem. Continue with Fix 2.
This one check saves you from editing files you do not need to edit.
2. Check Your Host’s Server Status Page
Before you change a single file, rule out a server problem. A wordpress database connection error is often not your fault at all.
- Open your hosting provider’s status or network page in a new tab. Most hosts publish one, and many also post updates on their social accounts.
- Look for any reported incident involving MySQL, MariaDB, or database servers.
- Ask yourself: are other sites on the same host failing too? If a friend’s site on the same host shows the same error, the whole database server is down.
- If there is an incident, stop here and wait. Contact your host’s support if you want an estimated fix time, but do not edit your files — the problem will clear when the server recovers.
- If the status page shows everything healthy, the problem is inside your own site. Continue to Fix 3.
Why this matters: editing wp-config.php during a server outage changes nothing and can introduce new mistakes. Always check the server first.

3. Verify the Database Credentials in wp-config.php to Resolve the WordPress Database Connection Error
Wrong credentials are the single most common cause of this error. They break most often after a site migration or after a hosting password change. WordPress stores four values in wp-config.php:
DB_NAME— the name of your databaseDB_USER— the database usernameDB_PASSWORD— the database passwordDB_HOST— the database server address (oftenlocalhost)
If any one of them is wrong, WordPress cannot log in to the database, and you get the error.
Back up first. Before editing, download a copy of wp-config.php to your computer. If you make a mistake, you can restore it in seconds.
- Open your hosting control panel and launch its File Manager (or connect with an FTP client).
- Find
wp-config.phpin your site’s root folder — the same folder that containswp-adminandwp-content. - Download a copy to your computer as a backup. This step is not optional.
- Open
wp-config.phpfor editing and find these four lines:
define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_database_user' );
define( 'DB_PASSWORD', 'your_database_password' );
define( 'DB_HOST', 'localhost' );
- Now open your hosting panel’s MySQL Databases (or Databases) section and compare. Check the exact database name, the exact username linked to it, and re-enter the password carefully.
- Re-type the password by hand rather than pasting it. Pasted passwords often carry invisible spaces, and one invisible space is enough to break the connection.
- Confirm
DB_HOST. On most shared hosting it islocalhost, but some hosts use a different address. Your host’s documentation or database section shows the correct value. - Save the file, then reload your site.
If the error disappears, you are done. If it stays, the credentials were not the problem — continue.

4. Look for Stray Characters in wp-config.php
This fix takes two minutes and catches a sneaky cause: invisible characters in wp-config.php that break the file before WordPress can even read your credentials.
- Open
wp-config.phpin a plain-text editor (Notepad, VS Code, or your host’s File Manager editor — never a word processor). - Scroll to the very top of the file. The first characters must be
<?phpwith nothing before them — no spaces, no blank lines, no hidden characters. - Scroll to the very bottom. Many copies of the file end with
?>followed by blank lines. A blank line after the closing tag sends output to the browser too early and can trigger strange errors. Deleting the closing?>entirely is safe and is actually what WordPress itself recommends. - Look for any odd symbols or duplicated lines around the database defines, especially if you edited the file recently.
- Save and reload your site.
Hand-edited config files are a classic source of this error. A single stray space before <?php is invisible in most editors but very real to PHP.
5. Confirm the Database Exists and the User Has Privileges
Sometimes the credentials in wp-config.php are correct, but the database itself is missing or the user no longer has permission to reach it. This can happen after a failed migration, a hosting account change, or an accidental deletion.
- In your hosting control panel, open phpMyAdmin.
- Look at the database list on the left. Find the database name from
DB_NAME. - If the database is missing, it may have been deleted or never imported. Restore it from a backup (see Fix 8) or re-import your database export.
- If the database exists, click on it and confirm the tables are there. You should see tables like
wp_posts,wp_options, andwp_users(the prefix may differ). - Go back to your hosting panel’s MySQL Databases section. Check that your
DB_USERis listed as a user of that database with all privileges ticked. - If the user is missing or its privileges were reduced, re-add the user to the database and grant all privileges.
- Reload your site and test.

If phpMyAdmin itself shows “access denied”, the problem sits with your hosting account, not WordPress — contact your host’s support with that exact message.
6. Repair Corrupted Database Tables
If wp-admin loads but your site’s front end shows the error, corrupted tables are the prime suspect. WordPress ships with a built-in repair tool for exactly this situation. Use it carefully: it is powerful, and it must not be left switched on.
Back up your database first. Export a copy from phpMyAdmin before running any repair. A repair changes your data, and you want a way back.
- Open
wp-config.phpand add this line just above the comment that says/* That's all, stop editing! Happy publishing. */:
define( 'WP_ALLOW_REPAIR', true );
- Save the file.
- Visit
yoursite.com/wp-admin/maint/repair.phpin your browser. - You will see a simple page with two buttons: Repair Database and Repair and Optimise Database. Click Repair Database first.
- Wait for the process to finish. It lists each table and its result. Note any table it could not repair.
- Reload your site and check whether the error is gone.
- Now remove the line you added. Open
wp-config.phpand deletedefine( 'WP_ALLOW_REPAIR', true );, then save.
That last step is critical. The repair page needs no login. Anyone who finds its address can run repairs on your database while the line is present. Switch it on, run the repair, and switch it off immediately — every time.

7. Fix Post-Migration Mismatches
Migrations cause this error even when the credentials look right, because two other settings often change silently when a site moves hosts.
The DB_HOST value. Your old host may have used localhost while the new one uses a specific address like mysql.examplehost.com. Check the new host’s documentation and update DB_HOST to match.
The table prefix. WordPress reads this line in wp-config.php:
$table_prefix = 'wp_';
The prefix must match the actual tables in your database. If your backup used wp_ but the new install created wp2_, WordPress looks for tables that do not exist and throws the connection error.
- Open phpMyAdmin and look at your table names. Note the real prefix (everything before
posts,options,users). - Open
wp-config.phpand compare it with$table_prefix. - If they differ, change
$table_prefixto the real prefix and save. - Reload your site.
Also re-check DB_NAME, DB_USER, and DB_PASSWORD after any migration — database names and usernames often gain a new prefix on the new host (for example newhost_wpdb instead of wpdb).
8. Restore From a Backup or Ask Your Host
If none of the fixes above worked, you are dealing with something deeper: a badly damaged database, a server-level fault, or credentials that no one can verify. This is the point to stop experimenting and use your safety net.
- Restore your most recent backup. Most hosting panels offer one-click restores of both files and databases. Restore the database to a point before the error appeared.
- If you keep your own backups, import the database SQL file through phpMyAdmin’s Import tab.
- Contact your host’s support if you have no backup. Describe exactly what you tried: the error text, that wp-admin does or does not load, and that the credentials check out. Ask them to check the database server logs and to restore the database from their own server backups — most hosts keep these.
- While you wait, you can enable WordPress debug mode to capture the real error message for support. Add these lines to
wp-config.php:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
Then reproduce the error and read wp-content/debug.log. The official WordPress debugging guide explains what each message means. Remove or set these back to false when you are done — debug output should never stay on a live site.
Restoring a backup feels drastic, but it is faster and safer than a tenth round of guessing. And if your host restores from their own copies, you may lose nothing at all.
What to Avoid
- Never delete database tables to “fix” the error. Deleting tables destroys content. The error means WordPress cannot reach the database, not that the tables should be removed.
- Never leave
WP_ALLOW_REPAIRenabled. The repair page needs no login. Remove the line the moment your repair finishes. - Never edit
wp-config.phpwithout a backup. One wrong character can take your site down. Download a copy first, every time. - Do not paste your database password into public forums or comment sections. If you need help, share the error text and what you tried — never credentials.
- Do not install random “database fixer” plugins while the site is down. You cannot reach wp-admin to install them properly anyway, and shady plugins can make things worse.
- Do not change the table prefix on a live site on a guess. A wrong prefix disconnects WordPress from every table at once. Verify the real prefix in phpMyAdmin first.
FAQ
What does “Error establishing a database connection” actually mean?
It means WordPress tried to talk to its MySQL or MariaDB database and failed. Your posts, pages, settings, and user accounts all live in that database, so when the connection fails, WordPress has nothing to display and shows this plain message instead.
Can I still log in to wp-admin when this error appears?
Sometimes. If the dashboard loads while the front end shows the error, the connection works but some tables are damaged — run the repair in Fix 6. If wp-admin shows the same error, the connection itself is broken — start from Fix 1.
Will I lose my content?
Almost certainly not. A wordpress database connection error is a connection problem, not a data-deletion problem. Your content is still in the database in the vast majority of cases. That said, always keep backups — they are your guarantee, not your hope.
How do I know whether it is my host’s fault?
Check the host’s status page for database incidents, and ask whether other sites on the same host are failing. If the whole database server is down, every affected site shows the same error at the same time. In that case only the host can fix it, so save your energy and contact support.
Is the built-in database repair tool safe?
Yes, when used correctly. Back up the database first, add the WP_ALLOW_REPAIR line, run the repair, then remove the line immediately. The danger is not the tool — it is leaving the repair page open to the public internet.
What should I do if none of the 8 fixes work?
Restore from a backup, then contact your host with full details of what you tried. Enable WP_DEBUG and WP_DEBUG_LOG temporarily so support can see the real underlying error in wp-content/debug.log. The official WordPress troubleshooting FAQ also covers edge cases beyond these eight fixes.
Conclusion
A wordpress database connection error looks catastrophic but is usually a small, specific problem: wrong credentials, a grumpy database server, or a table that needs repairing. Work through the fixes in order, back up before each change, and resist the urge to delete or reinstall things at random. Most readers will have their site back by Fix 3 or Fix 6.
If this guide helped, explore more troubleshooting on our blog, learn what we do on the about page, or start from the homepage to browse every guide by category.
