Fixing “error establishing database connection” in WordPress
Database connection error brings down the whole blog.All posts and pages cannot be accessed and that means you loose traffic which converts to revenue.
What “error establishing database connection” means?
WordPress content management system has back-head and front-head.Front head is responsible for display of content that is retrieved from the database.
There are different types of Databases .One example is popular MYSQL Server which is open source.Another one SQL Server designed by Microsoft.
PHP Scripting language sits in between Web Server(Apache,Tomcat,ISS) and Database Server connecting the two.If PHP cannot establish successfull connection between the database server and Web-server PHP Returns database connection error.
Possible cause of this error
There are three possible cause of this error.
- Database login details are incorrect-Database connection String requires username and password. Incase the combination is not correct,Database Server won’t accept connection.
- Database Corruption-Upload of bad plugins can make the database to be corrupted.
- Database Server is down-If the server that hosts your database is down,connection cannot be completed.
How to Troubleshoot “error establishing database connection”
1.Find out where the error is occurring.
Database error can occur in different instances.For Example, your website may be showing as usually but when you try to access WP_Admin the connection error pop ups.Also your website can be down but when you try to login as admin you get a different error.
These are the signs of database corruption.One or two of your tables have been corrupted and they need repair.
Open the file and add below code to it.
define( 'WP_ALLOW_REPAIR', true );
Once you add below code,you will be able to repair the database by going to browser and tying www.yourwebsitedomanin.com/wp-admin/maint/repair.php Replace yourwebsitedomanin.com with your domain.Below screen will appear.
Click on any button to repair the database.Once you are done,make sure you delete the Repair code because it can be accessed by anyone.
If this does not solve your problem,try next troubleshooting step.
2.Check Database Login details
If above option does not apply to you,then try to check your database Login details.What could change database login details?
Changing database name or user information can cause this error.
Login to your host and check wp-config.php file.At the top of the file you will see login credentials that connect to the database.Below is example.
Above code has four variables.
- Database name
- Login Username
- Login Password
- Database Host
If one of these variables are not correct,WordPress will not connect.Login to your hosting cpanel and Click PHPMyAdmin.
PHPMyAdmin is a interface that allows you to manage your databases.Select the database and click to open the tables.Locate “wp_options” table and open it.Check the username and password if they are correct.
You can test connection in two ways.
- Create a connection file using details in wp_config.php file and try to connect.
- Create a new username and password,then update wp_config file.
If you try above steps and still continue getting error,Contact your web hosting support.