The error establishing a database connection in WordPress usually means the credentials in your wp-config.php file no longer match your database. It happens most often after a migration or a host change. The second most common cause is a database server that is down or overloaded. Diagnose which one you have before changing anything.

We fix this error on client sites often enough to know the fastest route. It is not to start editing files. It is to spend two minutes working out what actually broke.

Most guides hand you a list of eight fixes and let you try them at random. That approach turns a five-minute credential fix into an afternoon. A couple of those fixes also carry real data-loss and security risk when you run them blind. This guide diagnoses first, then fixes.

What causes the error establishing a database connection?

This error appears when the PHP-to-MySQL handshake fails completely. WordPress runs its code through PHP and stores your content in MySQL. The connection details live in wp-config.php.

When WordPress cannot connect, it cannot fetch anything. The whole site goes down at once: front end, wp-admin, REST API, all of it. The failure sits at the connection level, not the content level. That is why one broken value takes an entire site offline.

Six root causes account for almost every case:

  • Incorrect database credentials in wp-config.php (DB_NAME, DB_USER, DB_PASSWORD, or DB_HOST)
  • A database server that is down, unresponsive, or overloaded
  • Corrupted database tables
  • Corrupted WordPress core files
  • A plugin or theme conflict, usually right after an update
  • Insufficient privileges for the database user

The next section tells these apart. Each one has a different fix. Running the wrong fix wastes time, or creates a new problem.

How do you diagnose the error establishing a database connection?

Spend two minutes narrowing the cause before you touch a file. Three quick checks tell you most of what you need, and they cost nothing. Skipping this step is the most common reason a simple fix turns into hours of guessing.

Check the front end against wp-admin

Load your homepage, then load yoursite.com/wp-admin. When both show the standard error, you are likely looking at wrong credentials or a downed server.

Sometimes the front end shows the standard error while wp-admin shows something else: “One or more database tables are unavailable. The database may need to be repaired.” That is a different problem. Your database is reachable, but a table is corrupted. Skip the credential fix and go straight to the repair section below.

A blank white page with no error message at all is a third thing entirely. That is the white screen of death, and it has its own causes.

Ask what changed right before it broke

The trigger usually names the cause. A recent migration points at credentials, because they rarely carry over. A plugin or theme update points at a conflict. An error that appears only during traffic spikes, then clears on its own, points at a server hitting its connection limit rather than anything in your files.

Check whether the server itself is reachable

Log into your hosting panel and open phpMyAdmin. If you can see and browse your database tables, the server is running. The problem is on your side: credentials, corruption, or files.

If phpMyAdmin will not connect either, the database server is likely down. That one belongs to your host, not to you.

Back up before you change anything

One rule applies to every fix below. Back up your files and database first.

Every step changes something load-bearing. A recent backup is what lets you undo a mistake instead of living with it. Our WordPress backup guide covers the setup. If you cannot reach wp-admin to run a backup plugin, export the database from phpMyAdmin and download your files over FTP.

How to fix incorrect database credentials in wp-config.php

Fixing credentials means matching four values in wp-config.php to what your database actually uses. This is the most common cause of the error establishing a database connection. Start here unless your diagnosis pointed clearly elsewhere.

The wp-config.php file sits in your site’s root directory. That is the folder holding wp-admin, wp-content, and wp-includes. Open it with your host’s file manager or over FTP. The official wp-config documentation covers what each value does.

Inside, find these four lines:

  • DB_NAME: the database name
  • DB_USER: the database username
  • DB_PASSWORD: that user’s password
  • DB_HOST: the database server address

One wrong character in any of them breaks the connection. Compare each value against the real database details in your hosting panel, then correct any mismatch. After a migration this is almost always the problem. The new host assigns a new database name, a new user, and sometimes a different host value.

Watch DB_HOST specifically. Many guides assume it is always “localhost.” Plenty of hosts use a specific server name or an IP address instead. When your other three values are correct and the error persists, a wrong DB_HOST is the usual culprit. Your host’s documentation lists the right value.

How do you find the correct database details?

Read them from your hosting control panel. In cPanel, open MySQL Databases. You will see the exact database name and its assigned users. Confirm that the user holds privileges on that database.

If you are unsure of the password, reset it in the same panel. Paste the new value into wp-config.php. The goal is an exact match on all four values, with no stray spaces or typos. Save the file, upload it back if you edited it locally, and reload your site.

How to repair a corrupted WordPress database

If your diagnosis showed the “tables are unavailable” message in wp-admin, your database needs repair rather than new credentials. A corrupted table produces the same database connection error on the front end. WordPress ships with a built-in repair tool that you enable with one line.

Open wp-config.php and add this line just above the “That’s all, stop editing” comment:

define(‘WP_ALLOW_REPAIR’, true);

Save the file, then visit yoursite.com/wp-admin/maint/repair.php in your browser. You will see two buttons: Repair Database, and Repair and Optimize Database. Repair alone is faster and usually enough. Repair and Optimize also cleans up the tables, but it takes longer, so save it for when plain repair does not clear the error.

Here is the part most guides bury, and it matters. The repair page needs no login. Anyone can reach it while that line sits in your config, which makes it a live security hole.

The moment the repair finishes, go back into wp-config.php. Delete the WP_ALLOW_REPAIR line and save the file again. Do not skip this. An open repair endpoint is exactly what automated scanners look for.

Is your database server down or overloaded?

If phpMyAdmin would not connect during your diagnosis, the database server itself is the problem. That one is often out of your hands, and it produces the error establishing a database connection on every site sharing that server.

On shared hosting, many sites draw on the same database resources. A neighbor’s traffic spike, or your own, can push the server past its limits and drop connections. The error appears, then clears on its own once load falls.

To confirm, open phpMyAdmin again after a few minutes. Intermittent access almost always means a resource ceiling rather than a broken setting. Contact your host. Tell them when the error started, say you suspect the database server, and ask them to check whether it is down, overloaded, or hitting a max-connections limit.

A downed or overloaded server is genuinely their job. No amount of file editing on your end will solve it. If the site strains its resources regularly, the underlying fix is performance work, and our guide to fixing a slow WordPress website covers where that load usually comes from.

How to rule out a plugin or theme conflict

A plugin or theme with faulty code can trigger this database connection error, usually right after you install or update one.

To test plugins while you still have wp-admin access, deactivate them all and reload the site. If the error clears, reactivate them one at a time until it returns. That names the offender. Our guide to fixing a plugin that is not working covers the same isolation method in more detail.

If wp-admin is locked, FTP still works. Rename the wp-content/plugins folder to plugins_old, then reload your site. WordPress cannot find the plugins, so it deactivates them all at once.

If that clears the error, rename the folder back to plugins. The individual plugin subfolders stay deactivated, so you can re-enable them one by one and isolate the culprit. To test a theme the same way, switch the active theme through the database in phpMyAdmin.

How to replace corrupted WordPress core files

Corrupted core files can cause a database connection error after a failed FTP transfer, a security compromise, or a host-side problem. The fix is to replace those files with fresh copies. It stays safe as long as you touch only the right ones.

Download a clean copy from WordPress.org. Upload everything except the wp-content folder to your site’s root over FTP, and tell the client to overwrite existing files. Our guide to replacing WordPress core files walks through the process step by step.

The honest tradeoff sits in the root-level files. Replacing core is low risk for your content, since wp-content keeps your themes, plugins, and uploads. It can still overwrite custom edits to files like .htaccess or a hand-edited wp-config.php. Save copies of those before you overwrite, then reapply your changes afterwards. This is exactly why the backup step is not optional.

One more thing worth checking. If core files were corrupted by a compromise rather than a bad transfer, replacing them treats the symptom. Look for signs your site was hacked before you call it fixed.

What if the error establishing a database connection keeps coming back?

A recurring database connection error points to a root cause you have not fixed yet. It is not a one-time glitch.

If the error returns under traffic and clears when things quiet down, you are hitting your host’s connection or resource limits. The real fix is more resources or better hosting, not another round of file edits. Shared plans are the usual setting for this pattern.

If it recurs without a traffic pattern, look for three things: a plugin that repeatedly corrupts tables, a database damaged beyond a quick repair, or a host with an unstable database server. Our roundup of common WordPress errors helps when the symptoms keep shifting.

At this point the useful question changes. It stops being “how do I clear the error today” and becomes “what keeps breaking.” That is often where a developer earns their fee. Chasing an intermittent failure across plugins, hosting limits, and database health is slow, careful work, and guessing at it makes things worse.

Ongoing monitoring catches this pattern earlier. Our WordPress care plans exist for sites that cannot afford to discover an outage from a customer email.

When should you call a developer or your host?

Call your host when phpMyAdmin will not connect, when other sites on the server are also down, or when you suspect a server-level problem. Those belong to them.

Call a developer when the database connection error recurs, when a fix risks data loss you are not comfortable with, or when credentials, repair, plugins, and core files have all failed to produce a stable result.

There is no shame in stopping. This error takes your whole site offline, so every hour it stays broken costs you visitors and trust. A wrong move during troubleshooting can turn a quick fix into lost data. If the site is business-critical and you are guessing, paying someone is almost always cheaper than a mistake. Knowing when to hand it off is a skill, not a failure.

Conclusion

Most cases of the error establishing a database connection come down to four values in wp-config.php, a corrupted table, or a server that needs your host’s attention. Diagnose first. Check the front end against wp-admin, ask what changed, and see whether phpMyAdmin connects.

Match the fix to the cause. Correct the credentials, run the repair tool and then remove the WP_ALLOW_REPAIR line, or escalate to your host. Back up before every step. A recurring error is a different problem from a one-time one, and it needs a root cause rather than another round of edits.

Frequently asked questions

Can a plugin cause the error establishing a database connection?

Yes. A plugin with faulty code, most often one you just installed or updated, can trigger this error or corrupt your database. Test by deactivating all plugins, either from wp-admin or by renaming the wp-content/plugins folder over FTP. If the error clears, reactivate them one at a time.

Will I lose data fixing this error?

Usually no, provided you back up first and follow the steps carefully. Correcting credentials and running the repair tool do not delete content. The real risk sits in replacing core files, which can overwrite custom edits to root files. A full backup before you start is what lets you undo any mistake.

Why does this error happen after migrating to a new host?

Because database credentials rarely carry over. A new host assigns a new database name, a new username, and sometimes a different DB_HOST value. The old values in wp-config.php no longer match. Update DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST to the new host’s details, and the site typically reconnects.

How do I fix the error without cPanel or wp-admin access?

Use FTP. Connect with an FTP client and open wp-config.php in your site’s root to check the database credentials. You can also rename the wp-content/plugins folder to test for conflicts, or replace core files. If nothing reaches the database at all, the server is likely down and your host must resolve it.

Is this error my fault or my hosting provider’s?

It depends on whether the database server responds. Open phpMyAdmin from your hosting panel. If you can browse your tables, the server is fine and the cause is on your side. If phpMyAdmin also fails, or other sites on the server are down, the database server is the problem and your host owns it.

How long does it take to fix this error?

A credential fix often takes five minutes once you have the correct database details. A database repair adds a few minutes. Server-level problems depend on your host’s response time. Recurring or intermittent errors take longest, because that work is a diagnosis rather than a single edit.

Need this fixed properly, without the guesswork?

Some sites are too important to experiment on, and a database connection error can keep returning no matter how many fixes you try. That is the work we do best. Survyc is an AI-first digital agency that handles WordPress fixes, migrations, and ongoing maintenance for agencies and the clients they stand behind. We work white label as part of your team, so your client sees their site fixed and you get the credit. Contact us or email info@survyc.com and tell us what you need.