Skip to content
01

WordPress “Error establishing a database connection”: diagnosis and fixes

Your site shows a blank page with “Error establishing a database connection”? Here is how to tell within five minutes whether the problem comes from the credentials, the MySQL server or corrupted tables.

Walid Sadfi
4 min reading
Share:

The message appears alone on a blank page: Error establishing a database connection. WordPress could not reach MySQL, and without a database it has nothing to show: no posts, no settings, no dashboard. The whole site is offline, but your content is almost never lost. This is a connection problem, not a deletion.

The three possible causes

  • The connection credentials in wp-config.php are wrong: password changed at the host, migration not carried over properly, a typo.
  • The database server does not answer: MySQL stopped, flooded with connections or out of memory.
  • Some tables are corrupted, often after an abrupt server shutdown or a full disk.

Step 1: find out whether MySQL answers

Open /wp-admin/ as well as the home page. If the dashboard shows a different message saying tables need repairing, go straight to step 3. If other sites hosted on the same server are also down, the MySQL server is at fault: your host has to step in. With SSH access, one command is enough to check:

mysqladmin ping -h localhost -u username -p

Step 2: check the credentials

Open wp-config.php at the root of the site and compare these four values with those shown in your host's control panel. A single difference is enough to cause the error.

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

DB_HOST deserves particular attention. Depending on the host, it is not always localhost: some use a dedicated address, a specific port, or in a Docker setup the name of the database service. After a migration, it is the value most often forgotten.

To test the credentials without guessing, WP-CLI attempts a connection with exactly the values from the file:

wp db check

Step 3: repair corrupted tables

WordPress includes a repair tool, disabled by default. Add this line to wp-config.php, open /wp-admin/maint/repair.php, run the repair, then remove the line immediately: the page can be reached without logging in while it is active.

define( 'WP_ALLOW_REPAIR', true );

From the command line, wp db repair does the same job. Back up the database before any repair, even while the site is down.

When the error keeps coming back

An error that appears then goes away on its own almost always signals a server running out of breath: too many simultaneous connections, a traffic spike, a bot crawling the site or a slow query blocking the others. Raising the limits only postpones the problem. You need a page cache, an object cache, and to identify slow queries. If your shared hosting is hitting its limits, hosting sized for WordPress often settles the matter for good.

Frequently asked questions

Are my posts lost?

In the vast majority of cases, no. The message only says WordPress cannot connect. The data stays in the database as long as it has not been deleted.

Why did the error appear when I changed nothing?

Your host may have changed the database password, moved the MySQL server or suspended the account. A full disk also causes this error. Check your host's emails and the disk space before changing anything.

Need a developer to fix the problem or keep it from coming back? Discover our WordPress maintenance service.

Author

Walid Sadfi

Walid Sadfi is the founder of Evolurise, a team of engineers deployed on site with its clients: custom development (WordPress, Drupal, React, Laravel), AI agents, DevOps and team training. With 8 years of experience, he supports SMEs and large companies, from understanding the problem to deploying the solution. On this blog he shares practical, field-tested insights.