Skip to content

How to fix "Error establishing a database connection" in WordPress

By Ash, Founder, HosteazePublished 3 min read

This error means WordPress can't connect to the database that holds your content. The cause is usually wrong details in wp-config.php (database name, username, password or host), a database server that's down or overloaded, or damaged database tables. Check the four wp-config.php values first. WordPress's own guide names them as the usual cause.

Two network cable plugs lying apart on a black background, a connection waiting to be made.
Credit: 955169 via Pixabay

Every page on a WordPress site is built from the database: posts, pages, menus, settings. When WordPress can't log in to that database, it can't build anything, so it shows this one line instead. The fix is usually a single wrong value in one file.

What causes the database connection error?

CauseTypical triggerHow to tell
Wrong details in wp-config.phpYou moved hosts, restored a backup or changed the database passwordError appears right after the change
Database server down or overloadedServer problem or traffic spikeError comes and goes, or your host's status page shows an issue
Database limit reachedYour hosting account hit a database size or connection limitHost's control panel or support confirms it
Damaged tablesA crash or failed updateThe dashboard says "One or more database tables are unavailable"
Compromised siteAttacker changed files or passwordswp-config.php has changed and you didn't change it

How do I check the details in wp-config.php?

define( 'DB_NAME', 'database_name_here' );
define( 'DB_USER', 'username_here' );
define( 'DB_PASSWORD', 'password_here' );
define( 'DB_HOST', 'localhost' );

A quick way to test the username and password: log in to phpMyAdmin with them. If phpMyAdmin rejects them, so will WordPress.

What if the details are correct?

Then the database server is the next suspect. Try loading the site again in five minutes, since a server restart or brief overload often clears on its own. If it doesn't, contact your host and ask whether the database server is running and whether your account has hit a database limit. WordPress's own troubleshooting guide lists both as common causes.

How do I repair the WordPress database?

If you see "One or more database tables are unavailable", the tables may need repairing. WordPress has a built-in tool for this.

How do I stop it coming back?

Keep a record of your database details somewhere safe, so a password change doesn't leave you guessing. If the error keeps returning under traffic, your site may be outgrowing its resources; why is my WordPress site slow covers how to tell. If the screen is blank rather than showing this message, see how to fix the white screen of death instead. And if wp-config.php changed without you touching it, follow what to do when your WordPress site is hacked.

Related guides: What is web hosting?, What is cPanel?.

Frequently asked questions

Is my content lost when I see this error?

Almost certainly not. The error means WordPress can't reach the database, not that the database is gone. Once the connection is fixed, your posts and pages come straight back.

Why did the error appear on its own when I hadn't changed anything?

Then the problem is probably on the server side. The database server may be down, restarting or overloaded by a traffic spike, or your account may have hit a database limit. Wait a few minutes, then contact your host if it persists.

Can a hacked site cause this error?

It can, if an attacker changes wp-config.php or the database password. WordPress's own troubleshooting guide lists a compromised site as a possible cause once you've ruled out wrong details and server problems.

Sources

  1. WordPress Advanced Administration Handbook, Common WordPress errors
  2. WordPress Advanced Administration Handbook, Editing wp-config.php
  3. WordPress Advanced Administration Handbook, How to install WordPress

Hosting made for WordPress

See how our WordPress hosting is set up.

See WordPress hosting