How to Fix “Error Establishing a Database Connection” in WordPress
Encountering the “Error Establishing a Database Connection” in WordPress can be very frustrating.
This error means your WordPress site can’t connect to the database, preventing visitors from accessing your site.
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 guide, we’ll cover what causes this error, how to fix it, and steps you can take to avoid it in the future.
TL;DR
- The most common reason for database authentication issues is incorrect credentials – So check them!;
- Repair an unreadable database with the built-in WordPress repair file: define(‘WP_ALLOW_REPAIR’, true);
- Troubleshoot corrupt files;
- Check with your web hosting service provider if there are issues with your database server
Understanding the Error Establishing a Database Connection in WordPress
Before we dive into the fixes, let’s get to the core of the problem. When you see an error establishing a database connection, it generally means your WordPress website can’t communicate with the MySQL database.
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 Docker
You could also encounter error establishing a database connection in WordPress Docker environments. Similar to traditional WordPress setups, this error can cause significant downtime.
What Causes the ‘Error Establishing a Database Connection’?
Incorrect Login Credentials
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 and you’ll see the error message.
Corrupted WordPress files
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.
Database Corruption
This happens more often than you’d think, especially if your site has been a victim of hacking attempts.
Database Server Issues
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. Try to monitor your website uptime and performance to realize how severe the issue is.
Also, If you’re getting an error message like “this either means that the username and password information in your wp-config.php file is incorrect or that contact with the database server at localhost could not be established. This could mean your host’s database server is down,” then it’s worth looking into server issues.
Monitor, Backup and secure your website with WP Umbrella
Install WP Umbrella on your websites in a minute and discover a new way to manage multiple WordPress sites.
Get Started for freeStep-by-Step Solutions for Database Connection Errors
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.
Step 1: Verify Your Database Credentials
In most cases, an error establishing a database connection in WordPress occurs due to incorrect login credentials in the wp-config.php
file.
Ensure that the Database Name, MySQL database username, MySQL database password, and MySQL hostname are correctly entered.
Database Name
// ** MySQL settings ** //
/** The name of the database for WordPress */
define('DB_NAME', 'xxxxxx');
MySQL database username
/** MySQL database username */
define('DB_USER', 'xxxxxx');
MySQL database password
/** MySQL database password */
define('DB_PASSWORD', 'xxxxxxxxx');
MySQL hostname (server)
/** MySQL hostname */
define('DB_HOST', 'localhost');
You need to check for each element if the values are correct and match your server settings.
Where to find the database name
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.
Database user name and password
If your database name is already correct but you still get this error, you need to verify your user name and password.
It can be done by creating a new PHP file in WordPress’ root directory and entering the following code. You can name the file 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.
Step 2: Repair a Corrupt Database
Sometimes, your database is just corrupted. This can happen 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, then 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.
Step 3: Troubleshoot Corrupted Files
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!
Download the latest WordPress package and replace the existing files, except for wp-content
and wp-config.php
.
This won’t have any effect on your plugins, themes, or media, only the WordPress installation itself.
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!
Step 4: Consult Your Hosting Provider
Finally, if you’ve exhausted all options and are still seeing the error, it may be time to talk to your hosting provider and ask for some explanations or logs. High traffic or restrictions on your database server could be the culprits.
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.
Preventive Tips to Avoid Database Errors in WordPress
Pro Tips
Consider using WP Umbrella for automated backups, site monitoring, and database health checks.
After fixing the error, it’s essential to follow some best practices to prevent similar issues in the future.
1. Use Strong Database Credentials
Ensure your database credentials are strong and unique. Avoid using common or default credentials.
2. Regularly Update WordPress, Plugins, and Themes
Outdated plugin can introduce bugs that may lead to database errors. Keep WordPress, themes, and plugins up to date to reduce the risk of compatibility issues.
3. Optimize Your Database Periodically
Routine database optimization can prevent corruption and improve your site performance. You can use WP Umbrella database optimization feature to keep all your databases clean in 1-click.
4. Choose Reliable Hosting with Adequate Resources
Opt for a solid hosting provider with a plan that matches your site’s traffic and resource needs. Cheap shared hosting provider are often a bad idea.
5. Implement a Backup Solution
Set up regular, automated backups for your site, including the database. Having recent backups ensures you can quickly restore your site if an error occurs.
FAQ: Error Establishing a Database Connection in WordPress
This error occurs when WordPress cannot communicate with your database, usually due to incorrect credentials, a corrupted database, or server issues.
Database credentials are located in the wp-config.php file, which you can access via FTP or your hosting control panel.
Database corruption can happen due to sudden server shutdowns, plugin conflicts, or excessive database load. Regular optimization helps prevent it.
Better Safe Than Sorry
Error establishing a database connection in WordPress doesn’t have to be the end of the world, or your website. Following these steps should help you resolve the problem and get your site back up.
And remember, the best offense is a good defense; regularly back up your website with WP Umbrella to avoid future headaches.