The most common type of error and very vague type of error in WordPress is Error establishing a database connection. It's the most annoying type of error as well.
One of my sites running good and suddenly one day I found that it started showing this error. A few things that will help you to solve this error are listed here.
Case 1: Check Database credentials
You would have been prompted to enter a database hostname, database name, database username, and password when installing WordPress. These credentials are saved in the wp-config.php file in your WordPress installation. Checking that the database credentials in your wp-config.php file are right is a great first step in troubleshooting database link errors.
Case 2: Error in Linux
This came as a surprise for me when I tried logging into the AWS EC2 instance when faced with this error. I tried to open the wp-config.php file with the following command:
sudo nano wp-config.php
Checking all the credentials look good. Closing the file with Ctrl + X command showed me a surprise error.
This error “sudo: unable to resolve host ip-xx-xx-xx-xx: Temporary failure in name resolution” at first looks like a small error.
Fixed this and the detailed steps I have instructed here: AWS error – sudo: unable to resolve host ip-xx-xx-xx-xx: Temporary failure in name resolution
Once fixed, my WordPress got back to its original state and working good again.
Case 3: Update and Reboot Server
In cases where I had my own VPS or dedicated server or when testing on localhost, sometimes this error comes up all of sudden. The first step I do is to update the linux:
sudo apt upgrade
And then reboot the linux to make sure the update is properly applied.
This usually removes any small glitches in the system which prevent in connection between php and mysql.
Case 4: Clear Website Cache
In an interesting case, I found that the cache location of Nginx was full and it resulted in an error like this.
To check the disk usage in linux, you can use this command:
sudo df -H
To check the disk usage of you Nginx cache directory, you can use this command
sudo du -sh /var/www/nginx-cache
This will show you the size of the cache file, if it's making your disc full, it can cause WordPress Error establishing a database connection.
To clear the Nginx cache, you can use this Nginx plugin or delete the nginx cache folder manually and restart nginx.
If you had installed WordOps for managing your server stack as I described in this post: Install and Manage WordPress with Nginx and PHP-FPM on AWS EC2 Micro Instance then you can find or set your own custom Nginx cache location in the Fastcgi.conf file located in
sudo nano /etc/nginx/conf.d/fasctcgi.conf
This file has a line which looks like this
fastcgi_cache_path /var/www/nginx-cache levels=1:2 keys_zone=WORDPRESS:50m inactive=6h max_size=356M;
The cache location in the above line is /var/www/nginx-cache which can be changed as per your requirement. The size of the Nginx cache can also be defined with max_size variable as seen above.
As you can see, there are many options for resolving the WordPress database relation error. Invalid credentials in the wp-config.php file are the most common. The best way to begin is by double-checking that those are right. The last thing you want for your website is for it to go down. So, hopefully, one of the measures mentioned above assisted you in getting your site back up. Always keep in mind that you can always restore your site from a backup if necessary.
Have you received the message “Error establishing a database connection” on your website? If that's the case, did you manage to find a solution? Let us know in the comments section below.