Seeing the “error establishing a database connection” message on your website is one of the worst ways to start your day.
Although the error looks frightening, the good news is that it’s fixable. Also, the error is quite common and could be showing due to many reasons.
What does the error mean?
To fix the error lets first understand what the error means. A database is basically an organized group of structured information and data. A connection to the database management system i.e., MySQL is made using the PHP programming language.
So when we see the error message, it basically means that your WordPress website is unable to connect to MySQL using the PHP commands.
Few steps you can take to fix the database connection issue
1. Check Database Credentials
Many times the error is simply shown due to wrong database credentials. You can try checking your database credentials in the wp-config.php file. You will mainly be looking for lines that match these.
define( ‘DB_NAME’, ‘database_name’ );
define( ‘DB_USER’, ‘your_username’ );
define( ‘DB_PASSWORD’, ‘your_password’ );
If any field doesn’t match with the correct one then you can change them here and save the file. Your connection to the database should now be restored.
Although, if the connection was working earlier and no changes were made to the wp-config.php file then wrong credentials will most likely not be the issue.
2. Repair your WordPress database
The WordPress database is fairly complex and one small error can corrupt the database. To check if there is any issue with the database, visit your WordPress admin dashboard. If you see any error on this page then you will need to repair your database.
To do the database repair, edit your wp-config.php file and add the below line just before the last line.
Once WordPress is done repairing the database, visit your website and see if the error has been fixed. If the error has been fixed then remove the line we added to the wp-config file and save.
3. Contact your web hosting provider
Many times there is some issue with the hosting provider. You can simply contact them and ask to verify if there is any database server issue from their end. Sometimes the hosts also fix the error for you if there is some other issue unrelated to them.
If the first two steps fail then the last step should be your best option. The hosting provider should be able to pinpoint the problem with your database and fix it. It is important to only host your website with good hosting providers to avoid such errors and regularly take backups of your database. A backup can save you from many troubles.