The WordPress error establishing a database connection is an error that happens when your WordPress website cannot connect to your WordPress database.
When there is an error establishing a database connection, you will see this screen when you visit your WordPress website on your browser:
Don’t panic! The error establishing a database connection is easy to fix and we will show you how to fix it in this guide.
How does the error establishing a database connection happen?
Every WordPress website is made up of two main components:
- WordPress files
- WordPress database
Your WordPress files are located under your public_html root directory. Think of your WordPress files as shells that receive data to serve to your website visitors.
Each WordPress website also has a MySQL database, that is normally located in an application called phpMyAdmin. Think of your database as a system that organizes and stores your website data.
Your WordPress database feeds your website data to your WordPress files to make your website work.
The error establishing a database connection happens in WordPress when your WordPress database does not properly connect to your WordPress files, triggering the error screen when you or anyone else visits your website.
Your WordPress database connects to your WordPress installation through a file called wp-config.php (remember that file, it’s important).
Inside of your public_html root directory, locate your wp-congif.php file. If you are looking for the wp-config.php file for a subdirectory, click on the right folder within public_html.
WordPress files are always named the same, so you want to make sure you’re in the right folder.
Inside of the wp-config.php file, there are four key pieces of information required to properly connect your WordPress database to your WordPress files.
- Database name (DB_NAME)
- Database user (DB_USER)
- Database password (DB_PASSWORD)
- Database server (DB_HOST)
When you click open your wp-config.php file, you will see these four lines listed under a section called MySQL settings.
When the wp-config.php credentials cannot connect to your WordPress database, you will see the error establishing a database connection.
The error establishing a database connection is very common and normally happens when the MySQL credentials in your wp-config.php aren’t correctly put in, the database server on your web hosting company is down, or your WordPress database is corrupted / broken.
Now that you know how the error happens, let’s get to solutions on how to fix the error establishing a database connection in WordPress.
Keep reading to learn how to fix the error establishing a database connection on your WordPress website with step by step instructions.
Fix the error establishing a database connection
Below you’ll find tried and tested fixes to the error establishing a database connection, these solutions will work on any WordPress website and you can perform them on your own.
Solution 1 – Database credentials
You need to make sure the database credentials in your WordPress wp-config.php file match your WordPress database.
Normally the database credentials never change. However, if you’ve recently migrated your website to a new web host, performed changes to your WordPress database, or got hacked, the database credentials may have changed.
To check your database credentials are correct, login to your hosting control panel at your web host, where your WordPress website is hosted.
Go to your File Manager:
Inside of your File Manager, click on public_html:
If you’re fixing your primary domain name, stay in the main public_html directory. If you’re fixing an addon or subdomain WordPress website, click on its folder within public_html.
Once you’re in the correct WordPress folder, select the wp-config.php file, and either click Edit or double click on it to open it up.
Once you’ve opened up your wp-config.php file, scroll down to the MySQL settings and you will see the database name, user, password, and database server listed.
Keep your tab open on this page, the next step is to make sure your database credentials match.
Open up a new tab and go back to your hosting control panel main page and click on MySQL Databases.
On the MySQL Databases page, you will find a list of all your existing databases and database usernames. Normally, the database name and database username are the same, however sometimes they can be different. You can verify that by checking the MySQL settings in the wp-config.php file.
Look under the Current Databases section to find your database.
If you scroll down, you’ll find the Current Users section, which lists the Database Username and also contains a button to reset the database username password.
You can change the database username password to the one you see listed next to DB_PASSWORD in the wp-config.php file, just to make sure they match.
Once you’ve cross-checked and confirmed the database credentials in the wp-config.php file under MySQL Settings and your database credentials match, make sure to save the changes in your wp-config.php file.
Then, open a new tab on your browser and visit your website.
If the error is gone, you’ve solved it.
If you’re still seeing the error establishing a database connection screen on your website, let’s keep troubleshooting.
Solution 2 – Database hostname
If you’ve confirmed that your wp-config.php and database credentials are an exact but still see the error establishing a database connection, it may mean the database hostname is incorrect.
Go back to your wp-config.php file and look at the MySQL hostname, aka DB_HOST.
If you see an IP Address or localhost next to DB_HOST in the wp-config.php file, it’s possible the IP Address or localhost is not the correct hostname.
If you’re unsure, you can contact your WordPress hosting company and ask them what the correct database hostname is and they will confirm if you can leave localhost or put in a different IP Address for the MySQL hostname.
Once you’ve confirmed the database hostname, make the necessary adjustment in your wp-config.php file, click save, and check your WordPress website.
If you still see the error establishing a database connection, you’ll have to try a different solution.
Solution 3 – Database repair
The error establishing a database connection may also mean your database is corrupted or requires repair.
Database corruption is complex, and this solution isn’t a full proof method for fixing it, however it does run a manual repair from your WordPress application which can solve the error.
To run a manual WordPress database repair, go back to your wp.config.php file and look for the following line:
/* That's all, stop editing! Happy publishing. */
Once you’ve found the ‘/* That’s all, stop editing! Happy publishing. */’ line, add this code snippet right above it:
define('WP_ALLOW_REPAIR', true);
Inside of your wp-config.php, it will look like this:
Make sure to save changes, and go to the following link:
https://www.yourowndomainname.com/wp-admin/maint/repair.php
Make sure to replace ‘yourowndomainname.com’ with your actual domain name.
You’ll get the following screen:
If for any reason you don’t see the WordPress repair database page, go to an incognito tab and it should appear.
Click on the Repair and Optimize Database button to run the operation. Make sure not to click away or reload your page while the WordPress database repair runs.
When the repair database operation completes, you’ll get a confirmation message and an important notice telling you to remove the define(‘WP_ALLOW_REPAIR’, true); code snippet from your wp-config.php file.
Once you’ve removed the code snippet from you wp-config.php file, save your changes, and visit your website from an incognito window.
If the error error establishing a database connection is gone, you’ve solved the problem. However, if you’re still seeing it, let’s look at another possible solution.
Solution 4 – Database connectivity
If you’ve gone through the solutions listed above and still get the error establishing a database connection on your WordPress website, it’s possible the MySQL server at your web hosting company is down or overloaded.
If the server at your WordPress hosting company is overloaded or experiencing downtime, it could create database instability and trigger the error establishing a database connection on your WordPress website.
The most obvious way to verify the problem is to ask or check on your web hosting provider’s status page if they’re experiencing database server issues.
If they confirm database problems, wait until the problem is resolved before visiting your website again.
Another way you can check if the database server at your web hosting company is down or unstable is by going to your hosting control panel and clicking on the phpMyAdmin app:
If you click on the phpMyAdmin app and it doesn’t open, that’s a good indicator that your web host’s database server is down, overloaded, or experiencing intermittent connectivity issues.
If your phpMyAdmin app opens and you’re able to connect to your database, there may be a permissions problem, you can test that our pretty easily.
Go to your File Manager, go to the public_html folder, and create a new .php file called ‘testdatabaseconnection.php’
Once the file is created, select it, click “Edit” and put in this code snippet:
<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>
Once you’ve put the code snippet into the ‘testdatabaseconnection.php’ file, make sure to put your own MySQL database setting username (DB_NAME) and password (DB_PASSWORD).
These are the same ones found in the wp-config.php file.
Once you’ve done that, save changes and go to the following URL:
https://www.yourowndomainname.com/testdatabaseconnection.php
Make sure to replace ‘yourowndomainname.com’ with your actual domain name.
If you’re able to load the link on your browser, it means there are no permissions issues on your database user settings.
At this point, you’ll want to go back to Solution 1 and make sure your database credentials are correct inside of your wp-config.php file.
Solution 5 – WordPress link
If you’re still getting the WordPress error establishing a database connection after troubleshooting with the solutions above, there is another fix you can try.
Go to your web hosting control panel and click on phpMyAdmin.
On the left-hand side, click on your database from the list to open up the database tables and locate wp_options
Under the option_name column, you’ll see one row for a siteurl and another row for home. Verify that the correct domain name is indicated in those rows.
If the siteurl and home are correct, you don’t need to make any changes. If they don’t match, you can click on the edit pencil icon and put in the correct domain name.
We hope this guide and step by step solutions have showed you how to fix the error establishing a database connection in WordPress.
If your WordPress website is still showing the error establishing a database connection screen after you’ve troubleshooted, it might be time to ask for some professional help.
You could always reach out and ask your WordPress hosting provider if they can troubleshoot for you or point you in the right direction.
The other option is to hire a professional WordPress developer to dig in and fix the problem for you. You can find WordPress developers on freelancing platforms like UpWork, Freelancer, and Codeable.