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.
wp-config.php. Your content is usually intact.DB_NAME, DB_USER, DB_PASSWORD and DB_HOST in wp-config.php with those in your hosting panel.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:
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.
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.
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:
DB_NAME and DB_USER: many hosts prefix them with the account name (account_wordpress). The prefix is part of the name.DB_PASSWORD: when in doubt, set a new password for the user in the panel, and copy it into the file.DB_HOST: localhost at many hosts, though not all. Some give a server name or an address, sometimes followed by a port (:3306). The exact value is in the 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.
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:
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.
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.
<?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 answers | The cause | The 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 connections | The 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 OK | The credentials are right. | Look at the database itself: next section. |
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:
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.
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:
wp-login.php or xmlrpc.php, visible in your host’s access logs.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.
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.
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.
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.
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.
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.
Simafri
Let's talk about your website
Tell us about your project in a few words: we will get back to you quickly.
Thank you! Your request has been sent. We'll get back to you shortly.
Prefer email? Write to us at support@simafri.com.