Error establishing a database connection in WordPress: what it means

Your site shows a single line: "Error establishing a database connection". WordPress could not open the database that holds your pages, settings and accounts. Your content is usually intact: this guide shows how to find which of the four possible causes is at play, and how to fix it.

Published on 5 October 2026 By 8 minute read

All guides

In short

  1. The message means WordPress could not connect to the database server with the details in wp-config.php. Your content is usually intact.
  2. First check that the database server is up: your host’s status page, other sites on the same account, phpMyAdmin.
  3. Compare the four values DB_NAME, DB_USER, DB_PASSWORD and DB_HOST in wp-config.php with those in your hosting panel.
  4. Test those credentials outside WordPress: the MySQL error message names the exact cause.
  5. If the dashboard mentions unavailable tables, run the WordPress repair tool, then switch it off again.
  6. If the error comes and goes, look at load: too many simultaneous connections, or bots swamping the site.

What the message says

For every page it serves, WordPress opens a connection to its MySQL or MariaDB database using four values written in wp-config.php, at the site root:

wp-config.php
define( 'DB_NAME', 'database_name' );
define( 'DB_USER', 'username' );
define( 'DB_PASSWORD', 'password' );
define( 'DB_HOST', 'localhost' );

"Error establishing a database connection" means that connection failed. Four causes produce it: credentials that no longer match, a database server that is down or unreachable, a server that is overloaded, or a damaged database. The sections below go through them from the easiest to check to the rarest.

1. Is the database server responding?

If the server is at fault, open a ticket with your host, giving the time the failure started and the exact message. There is nothing to change on the site in the meantime.

2. Are the wp-config.php credentials right?

This is the most frequent cause after moving a site, changing the database password in the hosting panel, or restoring a backup. Compare the four values in wp-config.php with the databases section of your hosting panel:

A password containing a single quote (') cuts off the PHP string around it in wp-config.php. Put a backslash in front of it (\'), or choose a password without a single quote.

Test the credentials outside WordPress

WordPress shows a generic message. MySQL, on the other hand, says exactly what is wrong. Two ways to ask it, with the values from wp-config.php:

From the command line, if you have SSH access

Terminal (you are prompted for the password next)
mysql -h localhost -u username -p database_name

With WP-CLI, wp db check reads wp-config.php directly and checks the tables with those credentials.

With a temporary PHP file

Upload this file to the site root under a name that is hard to guess, open it in your browser, then delete it straight away: it contains the database password.

db-test-a7k2.php
<?php
mysqli_report(MYSQLI_REPORT_OFF);
$link = @mysqli_connect('localhost', 'username', 'password', 'database_name');
echo $link ? 'Connection OK' : 'Failed: ' . mysqli_connect_error();

The first line matters: since PHP 8.1, a failed connection throws an exception by default, and the page would stay blank instead of showing the MySQL message.

MySQL answersThe causeThe fix
Access denied for user 'username'@'localhost' (using password: YES)The username and password pair is refused, or the user may not connect from this host.Reset the password in the panel and copy it into DB_PASSWORD; check DB_USER.
Access denied for user 'username'@'localhost' to database 'database_name'The user can connect but has no rights on this database.Attach the user to the database in your hosting panel, with all privileges.
Unknown database 'database_name'The connection works, but no database has that name.Fix DB_NAME, prefix included; check that the database still exists.
Too many connectionsThe server has reached its maximum number of simultaneous connections.See the section on intermittent errors.
Connection refused or No such file or directory (code 2002 from PHP), Can't connect to MySQL server (command line)No server answers at that address: the server is down, or DB_HOST is wrong.Check DB_HOST in the panel; otherwise, contact your host.
Connection OKThe credentials are right.Look at the database itself: next section.

3. A damaged database: the repair tool

When the credentials are right but tables are damaged, the public site shows the same connection error, and the dashboard a more precise one: "One or more database tables are unavailable. The database may need to be repaired." WordPress ships a tool for this. Add this line to wp-config.php:

wp-config.php
define( 'WP_ALLOW_REPAIR', true );

Then open https://www.your-site.com/wp-admin/maint/repair.php and run "Repair Database". Remove the line as soon as the repair is done: while it is there, anyone can reach that page, without logging in.

Before any repair, export the database (wp db export, or the Export tab in phpMyAdmin). If the repair fails, your host can restore the database from its backups.

4. An error that comes and goes: load

An error that shows up at certain times and then clears on its own points to an overloaded server rather than a wrong setting. The usual causes:

The fixes: block abusive login attempts, add page caching, disable xmlrpc.php if no application uses it, and ask your host for the logs covering the time of the error.

A website whose technical side we run

With Serenity by Simafri, we create your professional website, host it, secure it and keep it up to date. You write to us, we take care of everything. Domain name and professional email included.

Discover Serenity by Simafri

Frequently asked questions

Has the database connection error wiped my site?

Usually not. The message says WordPress could not connect to the database, not that the database is gone. If phpMyAdmin still shows the site tables, your pages, posts and settings are in place.

Why did the error appear after I moved my site?

Because the new hosting uses different database credentials: database name, user, password and sometimes server address. Copy the values from the new panel into DB_NAME, DB_USER, DB_PASSWORD and DB_HOST in wp-config.php.

Do I need to reinstall WordPress to fix this error?

Reinstalling WordPress changes neither the wp-config.php credentials nor the state of the database server, which are what cause this message. Test the credentials first, then the server, then the integrity of the tables.

The error only appears on some days. Where does it come from?

From overload rather than a setting: too many simultaneous connections, a traffic spike or bots. The access logs and the resource usage figures in your hosting panel show what was happening when the error occurred.

Contact us