It is a common issue and one that is actually part of running modern WordPress built website to have Errors for connecting to your database. WordPress is a dynamic content management system that resides in a Database (usually MySQL) and there are many different ways you can troubleshoot Errors when establishing a Database Connection for websites built on WordPress.
First Step for Fixing Database Connection Issue
Realize that more often than not, this error has nothing to do with your WordPress site but rather the web hosting service you are using may be down temporarily (e.g. server reboot, maintenance or upgrade). That means first thing we should do is double check if the MySQL server is up and operating normally and that you can login to phpMyAdmin
And also make sure that you can access phpMyAdmin to double check if you can login to your Database through your web hosting account.
Second Option for Fixing Database Connection Errors
Simply browse in to your MySQL Database options to see if your Database WordPress is built upon is visible, and also check the user for that Database exists. At this stage you should see both the Database Name and the User of that Database. If you can’t, then you need to restore your WordPress site. However, if all is good at this stage, proceed to next step.
Checking your Database is also an option provided by most modern web hosting service providers such as (Siteground, Hostgator, Crazy Domains, inMotion Hosting, goDaddy) simply locate this option through your cPanel > MySQL Databases
If you are not able to login and MySQL server using phpMyAdmin and get Error: 2002 (CR_CONNECTION_ERROR) error message means:
Can’t connect to local MySQL server through socket ‘%s’ (%d) (learn more about Client Error Codes and Messages
What if you don’t know the name of Database your WordPress site is built upon? Or that everything else seems to be okay? Then, browse in to Web Hosting Account > File Manager > public_html > wp-config.php (Right Click and Download a backup) and triple check the details in the default WordPress configuration file (as shown in the image below)
How to Fix Error Establishing a Database Connection in WordPress Video Lesson
Once again, database connection errors are actually normal for short period of time, if you’ve followed all the troubleshooting procedures and you still see Error Establishing a Database Connection Message in your internet browser. Then, contact your web hosting provider as they will be able to assist you further.