← All posts

WordPress “Error establishing a database connection”: causes and the fix, step by step

Your site shows nothing but "Error establishing a database connection"? How to tell within minutes whether the database server, the credentials or the tables are to blame, and fix it without making things worse.

WordPress "Error establishing a database connection": causes and the fix, step by step

Key points

  • This message means WordPress can’t reach MySQL or MariaDB: the database server is down, the credentials in wp-config.php no longer match, or the tables are damaged.
  • A tiny test script and the MySQL error code (1045, 1049, 2002…) pinpoint the exact cause within minutes.
  • When the error keeps coming back, the server is running out of something: disk, memory, connections. A lasting fix means monitoring and caching.

Few WordPress outages are as unsettling for a site owner as this one: no theme, no menu, just one line on a white page, “Error establishing a database connection”. The admin is gone too. The good news is that your content is almost always intact; WordPress just can’t read it any more. Here’s the order we check things in, from most to least likely, and what not to do in a panic.

What the message means

WordPress keeps everything except files in a MySQL or MariaDB database: posts, pages, settings, users, WooCommerce orders. On every visit it opens a connection using four values from wp-config.php: DB_NAME (the database name), DB_USER and DB_PASSWORD (the user and password), and DB_HOST (the server address, often localhost).

If that connection fails, WordPress can’t render anything and shows this generic message. Three families of causes cover nearly every case we see:

  • the database server isn’t answering: stopped, overloaded or unreachable;
  • the credentials no longer match: password changed, half-finished migration, edited file;
  • the database answers but something’s wrong inside: corrupted tables, wrong table prefix, empty database.

If the message mentions unavailable tables instead, the connection works: skip to step 3.

Step 1: check that the database server is running

This is the most common cause when the site worked yesterday and nobody changed anything. On shared hosting, check your host’s status page and open phpMyAdmin: if that can’t connect either, only a support ticket will move things forward.

On a server you run yourself (VPS, dedicated server), a few commands give you the answer:

# Is the service running? (mariadb or mysql depending on the distribution)
sudo systemctl status mariadb

# Is it answering?
mysqladmin -u root -p ping

# Why did it stop? The two usual suspects: a full disk, or memory
sudo journalctl -u mariadb --since "2 hours ago" | tail -n 50
df -h
sudo dmesg -T | grep -iE "out of memory|killed process"

On small servers we very often find one of two things. A full disk: logs that keep growing, backups piling up on the server itself; MySQL can no longer write and stops. Or a lack of memory: during a traffic spike or a wave of bots, the system kills the hungriest process, which is often the database. Restarting the service brings the site back, but the outage will return until the cause is dealt with.

Step 2: test the credentials in wp-config.php

If the server is fine, it’s the credentials. That’s the classic outcome after a migration, a change of host, or a “security password reset” in the hosting control panel that changes the MySQL user’s password without touching wp-config.php. To settle it without guessing, this small script tries to connect with the same values and prints the exact error code:

<?php
// test-db.php : drop it in the site root, open it once, then DELETE it.
// Copy the values from wp-config.php (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME).
mysqli_report( MYSQLI_REPORT_OFF );

$db = @mysqli_connect( 'localhost', 'db_user', 'db_password', 'db_name' );

if ( $db ) {
    echo 'Connected, server ' . mysqli_get_server_info( $db );
} else {
    echo 'Failed ' . mysqli_connect_errno() . ' : ' . mysqli_connect_error();
}
  • 1045 (Access denied): wrong user or password, or a user with no rights on that database. Reset the MySQL user’s password in your host’s panel, check its rights on the database, and copy it into wp-config.php.
  • 1049 (Unknown database): the database has a different name. On shared hosting, the name is often prefixed with the account name (account_wp rather than wp): check in phpMyAdmin.
  • 2002: the server can’t be reached at that address. Either the service is down (back to step 1) or DB_HOST is wrong. Some hosts run the database on a separate server with its own address, shown in the panel. Also try 127.0.0.1 instead of localhost: the former goes over the network, the latter through a socket file that isn’t always where PHP expects it.
  • 1040 (Too many connections): the database works but refuses new connections. That’s a load problem; see below.

Two details waste time. A password containing an apostrophe or a backslash must be escaped properly in wp-config.php. And if DB_HOST includes a port (localhost:3307), WordPress understands it but mysqli_connect in the test script doesn’t: pass the port as the fifth argument instead. Remember to delete the script once you have your answer.

Step 3: tables, prefix and the remaining causes

If the connection succeeds but WordPress still shows an error, look inside the database. With SSH access and WP-CLI, two commands check and repair the tables, and two more confirm the prefix:

# Check, then repair the tables (WP-CLI uses the credentials in wp-config.php)
wp db check
wp db repair

# The prefix WordPress expects…
wp config get table_prefix
# …does it match the tables that exist?
wp db tables --all-tables

Without WP-CLI, WordPress ships a repair tool: add define( 'WP_ALLOW_REPAIR', true ); to wp-config.php, open /wp-admin/maint/repair.php, run the repair, then remove the line straight away: that page works without logging in. Export the database first.

The prefix is a frequent culprit after a migration: the tables are called wp8x_posts while $table_prefix is 'wp_', or the other way round. WordPress connects, can’t find its tables and behaves as if it weren’t installed. Align the variable with the tables that actually exist; whatever you do, don’t run a fresh install, which would create a second, empty set of tables.

Finally, if the error comes and goes, a few minutes at certain times of day, it’s almost always load: a wave of bots, a heavy scheduled task, a plugin firing too many queries, or a backup locking tables.

Stopping it from happening again

Once the site is back, a few measures spare you a repeat of that morning. On our maintenance contracts, this is what we always put in place:

  • Uptime monitoring that checks a page every few minutes and alerts by email or text. It tells you before your customers do, and the exact time of the outage helps the diagnosis a lot.
  • A disk space alert, and backups shipped off the server instead of piling up on it.
  • Page caching: logged-out visitors get ready-made pages without touching the database. It’s the single most effective protection against outages caused by traffic spikes.
  • Filtering abusive bots at the server or web application firewall level, and hunting down plugins that fire lots of slow queries.
  • Hosting sized for the real site: a busy WooCommerce store doesn’t have the same needs as a brochure site. If the database still saturates with caching in place, the server is too small.

Finally, don’t “reinstall” WordPress or rush to restore an old backup until you know the cause: the data is usually intact, and a hasty restore can wipe out the last few days of orders.

FAQ

Have I lost my posts and orders?

Almost never. The message means WordPress can’t read the database, not that the database is gone. As long as you haven’t deleted anything or restored over it, the data is there: you need to restore the connection, not rebuild the site.

The error shows up for a few minutes and then disappears. What now?

That’s a server running out of headroom. Note the times, compare them with access logs and scheduled tasks, and add page caching.

Can a hack cause this error?

Rarely, but if the credentials changed and nobody on your side did it, or unfamiliar files appear, treat it as a compromise: change every password and scan the site before bringing it back online.

Is your site showing a database error, or does it keep coming back? We get the site back up, find the cause, and set up monitoring, caching and backups as part of our WordPress maintenance plans.

Share LinkedIn Facebook X WhatsApp
</>

webtestudio

Web agency: custom WordPress sites, premium themes and SaaS software.

Discuss your project →