Are you facing the Establishing a database connection error in WordPress?
One of the most common and feared errors WordPress users encounter is the “error establishing a database connection”
It means that your site can no longer communicate with your WordPress database, resulting in your entire site going down.
There is no reason to ignore this and you need to resolve it as soon as possible as it could affect your sales, traffic, and business reputation.
In this article, I’ll talk about some of the most common causes of this error and the easiest ways to resolve them.
Let’s get started!
Table of contents
Your WordPress site stores all data, including post data, page data, meta information, plugin settings, login information, etc. in your MySQL database.
As soon as someone visits your website, PHP runs the code on the page and queries the data from the database. The information is then displayed in the browser from the database.
If for some reason this doesn’t work properly, you get the error establishing a database connection message as shown below.
As the connection is not working properly, no data can be retrieved to render the page, so the entire page is blank.
The front end of your site will be broken, and you will not be able to access the WordPress dashboard. Problematic, isn’t it?!
WordPress must use a specific login and password to access the database. If they change for any reason, WordPress cannot retrieve anything from the database.
Among other reasons, WordPress files can be corrupted due to a failed update. This can include anything from the plugins to the themes and even the core WordPress software update.
It might be caused by a rogue plugin or hacker messing up the database internally.
Many things can go wrong on the web host’s end, such as the database being overloaded or being unresponsive due to too many concurrent connections.
A shared host can encounter this problem quite often as the same resources are used by a number of users on the same server. I encourage you to monitor your website uptime and performance to realize how often this happens.
Before you begin troubleshooting the error, we always advise you to make a backup.
Lots of the recommendations below involve manipulating data in your database, so you must avoid making matters worse.
Even if you think you are tech-savvy, it’s a good idea to always take a backup before attempting to fix anything on your WordPress site.
To troubleshoot the connection database error, the very first thing you need to do is make sure your database credentials are correct.
That is the most likely cause of the error establishing a database connection message. In particular after people switch hosting providers.
In WordPress, the connection details are stored in a file called wp-config.php located in the root directory of WordPress.
There are four key pieces of information required in wp-congif.php for the connection to succeed.
// ** MySQL settings ** //
/** The name of the database for WordPress */
define('DB_NAME', 'xxxxxx');
/** MySQL database username */
define('DB_USER', 'xxxxxx');
/** MySQL database password */
define('DB_PASSWORD', 'xxxxxxxxx');
/** MySQL hostname */
define('DB_HOST', 'localhost');
You need to check for each element if the values are correct and match your server settings.
The database information can generally be found in your hosting admin panel or in PhpMyAdmin.
If you struggle to get your database credentials, you can reach out to your hosting provider’s support.
If your database name is already correct but you still get this error, you need to check both your user name and password as well.
It can be done by creating a new PHP file in WordPress’ root directory and entering the following code. You can name the credentials_test.php, but it doesn’t really matter.
<?php
$test = mysqli_connect('localhost', 'db_user', 'db_password');
if (!$test) {
die('MySQL Error: ' . mysqli_error());
}
echo 'Credentials are good! Well done.';
mysqli_close($testConnection);
Make sure to replace db_user and db_password by your own credentials. Save and upload the file.
Then browse to the file on your WordPress site: https://mysite.com/credentials_test.php.
Whenever you get a “MySQL Error: Access Denied” then you know your username or password is wrong and you will need to reset your credentials and update your wp-config.php file accordingly.
Make sure to delete this file credentials_test.php afterward.
You also may need to clean your database if it has become corrupt.
This can happen from time to time because hundreds of tables are constantly being added and removed by new themes and plugins.
If you receive the following error when trying to access your WordPress dashboard, your database is corrupt: “One or more database tables are unavailable”.
If that is the case, WordPress has a database repair mode that you can use. Just add this piece of code at the end of the wp-config.php file.
define('WP_ALLOW_REPAIR', true);
Then access this URL: https://yoursite.com/wp-admin/maint/repair.php
You will have a choice to repair the database or fix the database and optimize it.
In order to expedite your site recovery, we recommend going with the restore database option as it is quicker.
Remove the line of code from your wp-config.php file after performing the database repair mentioned above.
If you are still having connecting issues on your WordPress website, proceed to the next troubleshooting step.
Corruption of your files is another possible cause of the error establishing a database connection message.
No matter whether this occurs as a result of a misconfigured FTP server, a hacker gaining access to your site, or a host issue, you can fix it quickly!
In a nutshell, you’re going to replace WordPress’ core on your site.
This won’t have any effect on your plugins, themes, or media, only the WordPress installation itself.
This will require you to download the last version of WordPress from WordPress.org.
Unzip the file on your computer once it’s been downloaded.
Then, you will need to delete both the wp-content folder and wp-configuration-sample.php file.
Next, overwrite the existing files with the new ones by uploading the rest via SFTP to your website. Replacing those problematic files will ensure you get a fresh copy that is free from corruption and preserves all your media and content.
Empty your web browser cache, refresh the page and go on your WordPress site to see if the error still exists.
If it does, keep reading!
If none of the above solutions work for you, you may want to talk to your hosting company because it could be related to an issue with your database server.
This problem is indeed likely to occur if there are too many concurrent connections to your database at once. The reason for this is that many hosts limit the number of simultaneous connections they allow.
Caching plugins like WP Rocket can help minimize your site’s database interactions.
If the issue is with your database server, I’ll advise you to upgrade your current hosting plan or directly move to Kinsta.
The 500 internal server error on WordPress is probably one of the worst you can get because your entire site is down. The 500 internal server errors are very often the consequence of an error in the PHP code.
Step-by-step guide on how to fix the 502 bad gateway error in WordPress.
In this article, I’ll show you how to easily turn off PHP errors in WordPress.