Wordpress

Fixed “Error establishing a database connection” WordPress error

Error establishing a database connection

WordPress 5.2 has just come out with several improvements and new features. One of the features is the ability to detect website errors and notify the admin by sending an automated email. This feature is extremely useful and solves issues that otherwise were very difficult to notice for admins.

But there a few errors that are not detected by WordPress new feature hence admins need to find out the cause to fix the issue. One such common WordPress error is ‘Error establishing a database connection”. I have seen a lot of WordPress users asking it in different forums so I thought to write a few solutions to resolve this issue.

What is “Error establishing a database connection”?

As the error is self-explanatory that the website could not connect to the site database. For those who don’t know WordPress uses the PHP MySQL extension to talk to the database. For the site to work, you should have PHP + Mysql installed on the server and properly configured with WordPress.

In this article, I am going to mention a few possible reasons that may be causing the error.

Wrong Database credentials

Remember when you setup your WordPress website and it asked for the database credentials including the database name, username and the user password. WordPress saved these credentials and used it to authorize itself on the database.

It is possible that the credentials have been changed but WordPress is still using the old username or password for authentication. If this is the case, then you should update new credentials in WordPress configuration.

Update database credentials in WordPress

To update database credentials in WordPress, site admins need to edit ‘wp-config.php’ saved in the root of the WordPress installation.

  • Connect to the server using FTP client “Filezilla’
  • Open wp-config.php & add new database username and password
  • Save wp-config.php & exit

Now reload the page and the error should have gone.

If the error is still there, do not panic. Move to the next solution.

Database user privileges withdrawn from using database

If your username and password is correct, then check if the database user has privileges over the database. It is possible if somebody else is managing your server, or you mistakenly withdrew user privileges. In this case, WordPress would not be able to connect to the database.

Give user privileges over the site database

To fix the privileges issue, simply login to your Mysql root account using the following command –

mysql -u root -p

Enter the mysql root user password. If you do not have password for mysql root user, then you can login using sudo.

sudo mysql

Now enter your system user password and it’ll log you into your mysql root user.

After you are logged into your root account, enter the following command to grant privileges to user over the site database –

grant all privileges on database_name.* to 'username'@'localhost';

Where database_name = name of site database

Now the user has all privileges over the site database. If this was the issue in your case, the issue should have been fixed.

What? Does the issue still persist? No problem. Here is the last scenario.

Database server has stopped running

If any of the above solutions did not work for you, then it is possible that the mysql server has crashed. The database server stops running or crashes when the server does not have enough resources to save information.

If your server has 1GB or less memory then there are high chances that the database would crash every now and then. You can check the server logs for more details.

To fix the issue you can start the server just by typing the following command in the terminal –

sudo service mysql start
OR
sudo systemctl mysql start

But this is a temporary fix. If your server memory is low after some time the server would crash again. So to fix it permanently, either upgrade your server memory to 2GB or more or create a swap space.

Create swap space

Swap partition is created when the server is installed. If you are using digitalocean or any other cloud service, it’s likely that your server has no swap partition.

But there is no issue. You can create a swap anytime. The system will use a swap partition to save application data whenever it runs out of memory.

I have written an article on how to create swap file. Follow the article and create a swap file of at least 2GB. The swapfile should at least be twice the size of system memory. So if your server ram is 1GB, at least create swap space of 2GB.

Conclusion

So there you have it. These are the possible reasons for WordPress error ‘Error establishing a database connection’. Most of the time, the first solution fixes the issue but if it does not, there is no risk in trying other solutions.

If this article has helped you fix the error, let me know in the comment section below and share this article.

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This website uses cookies to improve your experience. We'll assume you're ok with this. Thank you! Accept Read More