How to fix "Error establishing a database connection" error in WordPress
If you have ever been accosted by such unpleasant error message as the one in the picture above I'm sure you'd agree with me that it's not the nicest of predicaments to be in when it comes to the business of running a website.
What exactly does this error message mean?
WordPress was written using PHP and MySql. MySql is responsible for handling the storage retreival of information on the website server while PHP handles the display, manipulation and usage of the information stored to the satisfaction of the programmer.
When there's an "error establishing a database connection" error it simply means PHP is having a hard time connecting to the MySql database to receive, process and use information.
What Causes This Error to Occur
There are many specific reasons why PHP may be having issues with connecting to a database to retreive information, but most commonly the error can be seperated into the following categories
- Wrong login details supplied in the wp-config file or some other place, this also includes situations where the login credentials were recently changed.
- Malicious code from plugins could cause the database to be corrupted which will take down the database and generate an error.
- This issue could also arise if there's anything wrong with the server hosting the database.
What to do in case of this error
Checking for Front end and Back end
The first thing to do after discovering this error is to determine whether the error is occurring on the front and back end of the website as it commonly does, though in some cases it might happen on either of the two fronts without including both.
If the error is not occurring on both fronts it usually means that the database may just need a fast repair as opposed to what it would require if both the front and back end were affected.
Should this be the case then the database can easily be repaired by adding the following line of code just before the line that says "That’s all, stop editing! Happy blogging" in the wp-config file.
define('WP_ALLOW_REPAIR', true);
After which you can visit the following URL to finalize the repair
http://www.yoursite.com/wp-admin/maint/repair.php
After finishing with the repair it is usually advisable to remove this line of code from the wp-config file.
Checking the wp-config.php file
The wp-config.php file is used to specify the details required by the WordPress CMS to gain access to the WordPress database. It is one of the most important files in WordPress.
Any alterations made to the login details of the WordPress database has to be updated in this file else the website will lose acces to the database storing its information.
Check the code and values specified in the wp-config file to see if they correspond to the ones you set for the website. If these values don't correspond, change them to the right values.
The code that usually contain these details usually comes in this format
define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');
If it happens that you are still getting this error after trying the first two approaches it is best to contact your website host at this point.
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @gotgame I am @utopian-io. I have just upvoted you!
Achievements
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x