While managing your WordPress website on a daily basis you may come across few errors but one of the most frustrating one is the one that says ‘error establishing a database connection. It is frustrating because it turns the whole page blank with just a database connection error message.
This error message can trigger by itself sometimes even when you didn’t make any changes to the code or WordPress files as there are several possible causes behind it.
So, let’s dig deeper into this topic and explore all the possible solutions to fix database connection error on WordPress websites.
We suggest you to backup WordPress before you start experimenting with any of the solutions that are mentioned here to fix database error.
What triggers establishing a database connection error?
There are several possible causes that trigger ‘error establishing a database connection’ message as mentioned below:
- Your Database login credentials are wrong or have been modified and does not match your WordPress database user and password.
- The database server with your web hosting has stopped or not responding.
- The database server has been upgraded by the Web Hosting company
- Your WordPress database been corrupted.
- Web Hosting Server has some technical issues or errors due to which WordPress is unable to establish connection with the database.
- There could be other reasons as well that rarely occur.
Possible solutions to fix error establishing a database connection problem
First try to figure out if you are getting the same error message on both the front-end and the Admin area (backend) of your website and if the error message is same then you need to check config.php file for the login credentials mismatch.
It is explained below how to access and modify config.php file for WordPress.
But before we move onto that step let’s take a look at the scenario that you are only getting ‘error establishing a database connection’ message at the backend i.e. WordPress Admin side and in that case you can try repairing your WordPress database to fix the error.
You can do this by adding the following line in your wp-config.php file. Add it just before the line that reads ‘That’s all, stop editing! Happy blogging’.
1 | | define(‘WP_ALLOW_REPAIR’, true); |
Once you are done with it, you can see the settings by visiting this url: http://www.yourwebsite.com/wp-admin/maint/repair.php
A user does not need to be logged in to access this functionality when that line is added to the wp-config.php file. This is because it is used to repair the corrupt database and a user can often not login when the database is corrupt.
After the database repair is complete, make sure to remove that line from your wp-config.php.
If the above solution fixes the ‘error establishing a database connection’ for you then you can stop here. But in case the problem still persists then continue with the other possible fixes below.
Access and modify config.php file for WordPress.
WP-Config.php is the most important file of your WordPress installation as it contains database connection credentials. It helps in establishing a database connection.
If you changed the root password or updated the database user password, then you will need to make changes in this file as well. First thing you should always check is if everything in your wp-config.php file looks same as below:
1
2 3 4 |
define(‘DB_NAME’, ‘database-name’);
define(‘DB_USER’, ‘database-username’); define(‘DB_PASSWORD’, ‘database-password’); define(‘DB_HOST’, ‘localhost’); |
Check your DB_Host value as it not always localhost because many web hosts prefer the localhost IP instead of the ‘localhost’. However, with the most popular WordPress hosting providers such as bluehost, siteground etc. and with some of the best WordPress Web hosting providers ‘localhost’ works.
Try to replace the ‘localhost’ with the IP as below and it will fix the problem.
1 | define(‘DB_HOST’, ‘127.0.0.1:8889’); |
IP’s may vary for online web hosting services and you should check it with your Web Hosting Provider.
If you have made changes and tested as mentioned above and you are still getting ‘error establishing a database connection’ message then there might be an issue with the web server or database server itself.
Testing the availability of the Web Host (MySQL Server)
Before you get in touch with your Web Hosting Company to resolve the ‘error establishing a database connection’ let’s find out if the MySQL server is available and running.
If your website traffic suddenly goes up then your MySQL server may not be able to handle the load and generates error for the users.
To rule out any doubts you can conduct the connection test by yourself. Access the cPanel through your web hosting account and access phpMyAdmin and connect the database.
If it connects then we need to check if your database user has sufficient permission and for this you need to create a new file ‘testconnection.php’ and paste the following lines of code in it:
$link = mysqli_connect(‘localhost’, ‘username’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysqli_error());
}
echo ‘Connected successfully’;
mysqli_close($link);
?>
Make sure you replace the username and password with the database username and password of your WordPress database.
If running the above script connects successfully then it means that your user has sufficient permission and there is something else that is wrong that’s triggering the error message.
You need to re-check your wp-config file to make sure that the credentials there are correct. Check for typos as well as that causes errors many times.
In case you are unable to connect to the database by going to phpMyAdmin then it simply means that there is something with the server.
If you see something like this Access denied for user ‘foo’@’%’ (using password: YES) when connecting to the MySQL server through the testconnection.php as mentioned above or you are getting the access denied error when connecting your phpMyAdmin then you it’s time to get in touch with your Web Hosting Company.
These are few of the possible solutions that you can try when getting ‘error establishing a database connection’ message.
Don’t panic if you see that weird connect error message. Just try the above mentioned solutions and it will fix the problem in no time.
How to install WordPress Theme
How to convert pdf to word on Mac
How to migrate WordPress from an old web host to a new one
List of Websites for downloading Elementor Templates for WordPress
21 Best Websites to Download free WordPress Themes
23 Best Web Hosting for WordPress
12 Keyword Research tools for SEO
Leave a Reply