If you surfed the web for some time, you saw this error at least a few times. Error establishing a database connection in WordPress is one of the causes of many reasons which irritates you. This could be terribly frustrating, especially if it happened alone without you changing anything, as a WordPress beginner. It took a little more than 20 minutes to detect the problem and fix it.
Before doing the database changes make sure you’ve backup your site data.
Why this Error Occurs?
Well, in short, because WordPress could not connect to the database, you get this error. Now, the reason why WordPress cannot connect to a database may vary.
You may have incorrect or changed your database login credentials. Your database server might be unsatisfactory. Your database could have been corrupted.
In our experience, this error happens most of the time because of some kind of server error, but there could also be other factors. See what can be done to resolve this problem.
Check the WP-Config file
The most important single file for all WordPress installation is probably the WP-Config.php. This is where you specify the WordPress details for connecting your database.
You also need to change this file if you have changed your root password or user password for the database. You should always check to see if everything is the same in your wp-config.php file.
You may not always remember your DB Host value as a localhost. It will be different depending on the host.
Some people suggested that they replace their localhost with the IP. This kind of problem is common when you run WordPress in a local server environment. For instance, when changed to the IP, the DB host value may seem to work on MAMP.
IP ‘s for web hosting services will vary. If everything in this file is correct (check for typos), it is fair to say that something is wrong at the end of the server.
Check the Web Host (MySQL Server)
When your site is swarming with a lot of traffic you often notice this error in connecting your database. Essentially, the load cannot be handled by your host server (especially if your hosting is shared hosting).
Your site will get really slow and even make the error for some users. So the best thing you should do is contact your hosting provider on the phone or live chat and ask if your MySQL server is responsive.
You can do a few things for those users who want to test if the MySQL server runs on their own. To find out if they have a problem, test other sites on the same server.
If you also have the same error, you certainly have something wrong with your MySQL server. You just go to your cPanel to try to access and connect with phpMyAdmin if you do not have a website on the same hosting account.
If you can connect, we must check that your database user has enough permission. Create and paste the following code into a new file called testconnection.php:
Maintain replaced username and password. If the script has been connected successfully, that means that your user has sufficient permission and something else is wrong.
Return to your wp-config file to ensure that everything is correct (types are scanned again).
You know that your server has something to offer if you can not connect to a database by going to phpMyAdmin. It does not mean you are running down your MySQL server. It might mean that your user hasn’t enough permission.
If you receive the denied access error, you should contact your Host immediately to fix the error when you connect to PhpMyAdmin or testconnection.php results.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials and more.