WordPress 5.2 has just come out with several improvements and new features. One of the features is detecting website errors and notifying the admin by sending an automated email. This feature is handy and solves issues that otherwise were very difficult to notice for admins.
But there are a few errors not detected by WordPress’s new feature; hence admins need to find the cause to fix the issue. One such common WordPress error is ‘Error establishing a database connection”. I have seen many WordPress users asking about it in different forums, so I wrote 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. You should have PHP + Mysql installed on the server and properly configured with WordPress for the site to work.
In this article, I will mention a few possible reasons that may be causing the error.
Wrong Database credentials
Remember when you set up your WordPress website and asked for the database credentials, including the database name, username, and user password. WordPress saved these credentials and used them to authorize itself on the database.
Server admin may have changed the credentials, 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 are 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 a MySQL root user password, you can log in using sudo.
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 the 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, it should have fixed the issue.
What? Does the issue persist? No problem. Here is the last scenario.
Database server has stopped running
If any of the above solutions did not work for you, the MySQL server might have 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, there are high chances that the database would crash 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 will 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, your server likely 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 a 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 a swap space of 2GB.
So there you have it. These are the possible reasons for the 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.