How to Fix the Error Establishing a Database Connection in WordPress

Dindin Solehudin
WordPress wp-config database settings for DB_NAME, DB_USER, DB_PASSWORD and DB_HOST to fix the database connection error

"Error establishing a database connection" is one of the most alarming messages a WordPress site can show. The whole site disappears behind a single line of text, for visitors and for you. The cause, however, is usually simple: WordPress cannot talk to its database, either because the login details are wrong, the database server is down or overloaded, or a table is damaged.

This guide explains how WordPress connects to its database and walks you through the checks that fix this error in nearly every case.

How WordPress Connects to the Database

Every page view starts with WordPress reading four values from wp-config.php: the database name, the database user, that user's password, and the database host. It uses them to open a connection to MySQL or MariaDB, where all your posts, pages, Elementor layouts and settings live. If any of those values is wrong, or the server at the other end does not answer, WordPress stops and shows the error.

That means there are only a few places to look:

  • The credentials in wp-config.php.
  • The database server itself.
  • The database tables.
  • The site URL settings stored in the database.
How WordPress connects to its MySQL database using four credentials from wp-config.php

Step 1: Check Whether the Whole Server Is Affected

Open your hosting control panel and check for status messages or maintenance notices. If you host several sites on the same account, see whether they all show the error. If every site is down at once, the database server is the likely problem and your host needs to fix it. Contact support with the time the error started.

A quick extra test: try loading /wp-admin. If the admin area shows a different message, such as "One or more database tables are unavailable," the database is reachable but some tables need repair, which is covered in Step 4.

Step 2: Verify the Database Credentials

This is the most common cause after a migration, a password change, or a hosting change. Open wp-config.php with your hosting file manager or SFTP and find these lines:

define('DB_NAME', 'database_name');
define('DB_USER', 'database_user');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');

Compare each value with the database details in your hosting panel, usually under a section called MySQL Databases.

  • Database name and user: many hosts add a prefix such as your account name followed by an underscore. The full name must match exactly.
  • Password: if you are not sure, reset the database user's password in the hosting panel and paste the new one into wp-config.php.
  • Host: it is usually localhost, but some hosts use a separate address. Check your host's documentation.
  • User permissions: make sure the user is actually assigned to the database with all privileges.

Step 3: Restart or Wait Out an Overloaded Database

On shared hosting, a sudden traffic spike, a heavy plugin, or a bot attack can exhaust the database's connection limit. The error then appears and disappears on its own. Signs of this include the error showing only at busy times or only on some page loads.

  • Ask your host whether the database hit a connection or resource limit.
  • Enable page caching so most visitors are served without database queries.
  • Block abusive bots with a firewall.
  • Consider a plan with more resources if your traffic has grown, as explained in how to choose WordPress hosting.

Step 4: Repair Corrupted Tables

Tables can become damaged after a crash or a failed update. WordPress has a built-in repair tool. Add this line to wp-config.php:

define('WP_ALLOW_REPAIR', true);

Then visit yoursite.com/wp-admin/maint/repair.php and choose Repair Database. When it finishes, remove the line again, because the repair page does not require a login while it is enabled. You can also repair tables from phpMyAdmin by selecting them and choosing Repair table.

Steps to fix the WordPress database connection error: server status, credentials, load, repair tables, site URL, restore

Step 5: Check the Site URL After a Migration

If the error appeared right after moving the site or changing domains, the credentials may be fine but the site URL stored in the database may point to the old address. Open phpMyAdmin, find the wp_options table (the prefix may differ), and check the siteurl and home values. For Elementor sites, also run the Replace URL tool afterwards, as described in moving an Elementor site to a new domain.

Step 6: Restore From a Backup

If the database itself is badly damaged, the fastest path is restoring a recent backup of the database. Choose a copy from before the error started and follow the order in backing up and restoring WordPress. Restore files and database from the same point in time to avoid new mismatches.

Quick Diagnosis by Symptom

  • Error after editing wp-config.php: a typo in the credentials or a missing quote.
  • Error after migration: wrong database name, user prefix, host, or site URL.
  • Error that comes and goes: an overloaded database server or a connection limit.
  • Error on all sites in the account: the database server is down. Contact your host.
  • Admin shows "tables unavailable": table corruption. Run the repair tool.

What to Tell Your Host

If you need your host's help, a precise message gets a faster answer. Include the exact error text, the time it started, whether it affects all pages or only some, whether other sites on the account are affected, and any recent changes such as a migration, a plugin installation or a password reset. Ask them to check the database server status, the connection limit, and the error logs for that time.

How to Prevent It

  • Keep automatic database backups, ideally daily, with one copy off-site.
  • Use page caching to reduce database load.
  • Clean up post revisions, expired transients and spam comments occasionally to keep the database lean.
  • Write down database details in a password manager when you migrate or change hosts.
  • Monitor uptime so you hear about the error before your visitors tell you.

A Note on Managed and Cloud Databases

Some hosts run the database on a separate server or a managed database service. In that case the DB_HOST value is a hostname or IP address with a port, not localhost, and firewall rules must allow your web server to connect. If you move to such a setup, test the connection on a staging copy first and keep the old credentials until the new ones are confirmed working.

Frequently Asked Questions

Is my content lost when this error appears?

Almost never. The content is still in the database. WordPress simply cannot reach it right now. Once the connection is restored, everything returns exactly as it was.

Can a plugin cause this error?

Indirectly. A badly written plugin can run so many queries that the database runs out of connections, especially under traffic. If the error started after installing a plugin, disable it and watch whether the problem stops.

Why does the error show only on the front end?

Some caching setups serve cached pages to visitors while the admin area needs live database access, or the reverse. Clear all caches after fixing the connection so visitors see the working site. Our guide on why Elementor changes are not showing explains cache layers.

Wrapping Up

The database connection error means WordPress cannot reach its data. Check whether the server is down, verify the four credentials in wp-config.php, reduce load, repair tables, check the site URL after migrations, and restore a backup if needed. Work through those steps in order and your site will usually be back online quickly.

Comments

Popular Posts

Customer Lifetime Value Calculator
Customer Lifetime Value Calculator

Tools

Customer Lifetime Value Calculator

Calculate Customer Lifetime Value easily with our free tool. Learn how to retain customers, boost profits, and grow your business today.

Cash Flow Calculator
Cash Flow Calculator

Tools

Cash Flow Calculator

Master your cash flow with our free calculator. Easy step-by-step guide to track income, expenses, and net cash flow. Gain financial clarity...