Fix Error Establishing a Database Connection

Fix Error Establishing a Database Connection

I had a customer come to me recently with an issue they were facing with their WordPress site. They were receiving the dreaded “Error Establishing a Database Connection” most the time when visiting their site. This error did not happen every time so they did not have an actual database related issue but pretty often. I started to look at the web host Digital Ocean trying to narrow down the issue. I searched around and found a bunch of sites had the same issue that we were experiencing “Error Establishing a Database Connection”.

I tried several plugins and verified that our wp-config.php file had the correct settings. Then I found a solution that worked without the need of plugins that would consume server resources.

The issue was because their site was being XML-RPC attacked using up all of the database workers. I know this because I looked at the server DB workers we had 20 at that time and they were all being used and timing out. So I increased the workers to 50 and well they were all immediately filled up and now page refresh time was also affected.  I reverted that change and looked for a solution and that fix was super simple.

I did the following to resolve their issue.

Placing the code below at the top of the .htaccess file located in the root of your WordPress site.

# Block WordPress xmlrpc.php requests
<Files xmlrpc.php>
order deny,allow
deny from all
allow from 123.123.123.123
</Files>

Did this fix work for you, if so let us know in the comments below?

Add comment