error establishing database connection in wordpress

WordPress: Error establishing a database connection

Table of Contents

The most common type of error and very vague type of error in WordPress is Error establishing a 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.

SUDO: UNABLE TO RESOLVE HOST
WordPress: Error establishing a database connection 2

This error “sudo: ip-xx-xx-xx-xx: 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 , 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.

sudo reboot

This usually removes any small glitches in the system which prevent in connection between php and .

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 .

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.

Summary

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 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.

Share This Article:
Facebook
Twitter
LinkedIn
Pinterest
WhatsApp
Email
Reddit
Related Articles:
Effective Link Building Services For SEO
Backlinks
Effective Link Building Services For SEO

If you are a startup or newbie in the online business world, then it is obvious that one of your first priorities will be to optimize your web presence. One way through which you can do this is by implementing effective SEO techniques and strategies for boosting traffic on your

Read More »
add linkedin profile badge to wordpress
WordPress
Add LinkedIn Profile Badge And Card To WordPress

LinkedIn is an important social network to connect with many other businesses and work professionals worldwide. It is quite popular in English-speaking countries. A lot of professionals have found their job opportunities through LinkedIn. Also, many HRs and companies are always posting job openings here. If you have a good-looking

Read More »
semrush review
Search Engine Optimization (SEO)
A Review Of SEMRush – The Best SEO Tool?

There are a lot of services that claim to be able to tell you everything about your website in order to improve its performance. Some even offer software to help you with this task. One of them is called SEMrush and has been around since 2009. It’s one of the

Read More »
Subscribe to our newsletter
Chat With Our SEO community members On discord

2 Responses

  1. Updating and rebooting helped in my case. Fixed the Mysql error. Maybe it was an update with some new fixes in MySQL. Thanks.

  2. Very helpful in clearing this error of WOrdPress. Helped me save a time with the checklist in this blog post. Bookmarking it for future references.

Leave a Reply

Your email address will not be published.

This site uses User Verification plugin to reduce spam. See how your comment data is processed.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: