DigitalOcean Error Establishing a Database Connection Fix

DigitalOceanI’ve worked on a lot of sites that were hosted on DigitalOcean at this point so I’ve seen many VPS issues that have had to be resolved. I’ve used Ubuntu 14.04 the most on these setups, so this post will use Ubuntu commands. One issue that can have many different causes is the classic “Error Establishing a Database Connection” issue that can occur on sites running WordPress. Assuming your site was previously running fine and you haven’t been messing with the database settings within wordpress, this error is most likely just a result of your server having crashed. Now the fun part is trying to find out why and how to fix it.

Stopping your DigitalOcean MySQL Server from Crashing

1. Add Swap Space

Most times I’ve found that just adding swap space is enough of a fix assuming the server doesn’t have swap space allocated yet. You can check if there is any swap space on your server using the “free -m” command. That will show you the free and used swap space. If you only see zeros for swap, I would start by adding swap space as a fix. DigitalOcean provides their own easy to follow guide for adding swap space that I recommend following step by step.

2. See if You are Out of Disk Space

Running out of physical disk space is another possible cause. This one is more easily spotted if you try to restart mySQL and you get an “Unknown instance:” message. If you see that message, then you should enter this code to see how much disk space you have available:
df -h /

If you see that it’s all used up, you’ll need to upgrade your plan. So shutdown the server and up your droplet plan to the next level. Once the upgrade is finished, restart mySQL again and it should be as good as new!

3. Check for Brute Force Attacks

Now if swap space wasn’t the fix you were looking for and you had disk space left, I’ll go over a less common problem that I recently encountered that was causing this same error. I could tell that this error was different for two reasons.

  • I already had swap space on this server and it had been running fine for a while.
  • Every time I restarted mySQL it would almost immediately crash again.

The problem was actually the result of a brute force attack against the WordPress xmlrpc.php file. If your problem sounds similar, you can check to see if it is a brute force attack by looking at the apache logs:
cd /var/log/apache2/
cat access.log

The Fix

You’ll be looking for a ton of POST requests to xmlrpc.php from about 1 to 3 different IP addresses. It will be pretty obvious once you see it in the logs. All we need to do to stop this from happening is to ban the offending IP address(es):
iptables -A INPUT -s -j DROP

Just use the above code with the IP address that you want banned swapped in. You can confirm that the ip is on the ban list:
iptables -L INPUT -v -n

There was a feature to block XML-RPC requests at the server level added to DigitalOcean one-click installs in December 2015. All installs after that date can use the following code to prevent all future XML-RPC brute force attacks:
sudo a2enconf block-xmlrpc
Then, just restart apache:
sudo service apache2 restart


While a great number of problems can cause the “Error Establishing a Database Connection” message to show up, these two tips can certainly help a lot in diagnosing and fixing the error. Adding swap space is the most common fix for it, and brute force attacks are just a less commonly discussed possibility that should be considered.

Speak Your Mind