How to Fix Error Establishing Database Connection?
Last Updated: September 24, 2020 in Guides
If you have been using WordPress for a while now, or have browsed the internet for quite some time, you must have come across a good deal of error messages. One such common error message is Error Establishing Database Connection. This is a very common error and can arise due to a wide number of reasons.
So, how can you fix Error Establishing Database Connection messages? And why do such errors happen? Read on to learn more!
What do You Mean by Error Establishing Database Connection?
The implied meaning of this error is obvious. Your website is having trouble connecting to the database. As such, WordPress cannot read or write to the database and therefore will not function. Let us first learn this in detail.
Essentially, WordPress operates on two mediums. The code is written mostly in PHP, but it interacts with a MySQL database for reading and storing data. Now, if the WP core fails to connect to the database, this error is displayed. Obviously, it means the problem can arise due to various reasons, such as:
- Your database credentials have been changed
- A plugin or theme is causing database connectivity issues
- Your database is corrupted.
If it is because of a plugin or theme, you should try deactivating all plugins and reverting to the default theme. Then, activate plugins one at a time to locate the faulty one. While it is very rare, database issues can arise due to poorly coded WordPress plugins.
Repairing the Database
Try to find out one simple thing: can you access your wp-admin section? If you cannot, skip this section. This means your database is alright and you just need to check connectivity issues.
However, if you can access wp-admin or are getting a different error message in wp-admin -- say “One or More of your Database Tables are Corrupt”, then you need to fix the database.
To do that, open your wp-config.php
file, and add this line of code:
define('WP_ALLOW_REPAIR', true);
Thereafter, you need to visit mysite.com/wp-admin/maint/repair.php
where mysite.com is your domain name. The WP database repair wizard will handle the rest and your errors will be gone.
Once done, be sure to delete the line that you added to wp-config.php
file! It is very crucial to delete it because the database repair wizard is publicly accessible.
If this does not work, let us try something else.
Checking wp-config.php
Credentials
The wp-config.php
file contains configuration credentials related to your WordPress website and its database. Obviously, if something is incorrect in this file, your site will not work.
Once you open the file, look for entries such as this:
define('DB_NAME', 'name'); define('DB_USER', 'username'); define('DB_PASSWORD', 'password'); define('DB_HOST', 'localhost');
Here, name, username, password will correspond to your database credentials. Further, localhost can also be an IP address, depending on your web hosting provider’s settings.
It is safe to assume that you will not be remembering your database credentials instantly. Your web hosting provider can tell you how to locate the details. Most budget web hosts offer phpMyAdmin as a tool to locate database details, whereas managed web hosts tend to have special links for database details in their admin panel.
Once you have acquired database credentials, compare them with the values in wp-config.php
file. If there are any discrepancies, be sure to edit the file and input the correct values.
Thereafter, your website will be able to connect to the database easily. The error will be resolved.
Conclusion
If everything looks fine in the wp-config.php
file as well as elsewhere, chances are your web hosting provider’s SQL server is causing difficulties. It might be a temporary glitch, or it might be a server downtime issue. The best way out is to check with your web host or find ways to troubleshooting your SQL Server blocking.
With that said, remember, you should not panic when you encounter such error messages. Error Establishing Database Connection does seem like a nightmare, but it can be easily resolved by going through your database credentials.
Lastly, everything said and done, make sure you have a backup of your website at hand. If things do go wrong and errors cannot be fixed, you can still restore the backup and get back to action. In this manner, even the most complicated of errors will not hamper your online presence for long.