Error establishing a database connection (WordPress)

May 25, 2014 80.1k views
Hi there, I'm having a really hard time figuring this out... My website shows up "Error establishing a database connection" on occasion. This happens about once a day. As soon as I reboot the droplet, the problem's gone. I've tried adding the following 2 lines in my wp-config file, as suggested on some other forums, but it didn't work: define('WP_MEMORY_LIMIT', '64M'); define('WP_ALLOW_REPAIR', true); From what I read around the web, it seems that 512mb of RAM is perfectly fine for a wordpress multisite, if set correctly - though I'm having a hard time with a single (low content) site, without many plugins. I've also disabled/deleted the plugins that could potentially eat up a lot of memory, such as wp-backup. If you have any suggestions for me, I'd be more than thankful! Please notice that I'm pretty new with all this, and as it's my first time running VPS, I do have a bit hard time understanding how everything works, at the moment...
  • We have about 20 accounts on DigitalOcean and over the past two months ALL OF THEM have been experiencing this issue, even on droplets with 4GB RAM. We're migrating all of our sites elsewhere...

  • I have the same problem as well. I often restart the server but each time I restart, same thing happen the next day and this is happening 50% of my websites on DigitalOcean. I do have website hosting elsewhere and they works perfectly fine with lower monthly cost.

    I'm moving all my websites elsewhere as well. It's just ridiculous.

  • I have also experiences this issue over the last week. Does anyone have any idea what might cause this? Anyone got some alternative places to host?

  • Same issue for me! Glad I am no the only one.. i tried swap files and editing wp-config, but no help

  • Same issue here. While the DigitalOcean team has been very helpful, I just can't get this site to function properly. I am going to have to migrate as well.

  • am having the same issue now. seems this is a problem on Digital Ocean. restarting the mysql helped for now but I hope there is a more permanent solution.

  • Having the same issues here too. The server was stable, this just started happening.

  • Same issue here for about the past week on both of mine. One is even a brand new install

  • same issue here and it is getting annoying to say the least. Now I know this is a problem with many droplets not just mine.

  • Similar issue, have tried the above recommendation but the issue still persists, is there an alternative solution ?

  • Same issue with 2GB on multiple sites with very little traffic every 2-3 days, only happens on Digital Ocean and just too much hassle for what its worth, moving sites elsewhere.

  • same here on its own droplet...

  • Same issue here! my both sites are down.

  • Same problem. Started a couple of days ago. Wordpress site.

  • ya, I have 3 dropplets, and this is only happening on one, the one with the lowest load, all 3 are 1gig, all three are wordpress, all 3 droplets are in new york 3. this is the only droplet that is not wordpress multisite, the others are.

  • Same issue started a few weeks back. Upgrade to 2GB RAM hoping the problem would go away. No, it came again today. Have to restart mysqld. Very ignoring and can lose customers. It's just a single WP site. Any solution?

  • Me too. MySQL restart works fine but I'm not really up for dropping into my websites every day to see if they're still working.

  • I am in the same boat as well. Digital Ocean has to have something to say on the matter. I REALLY don't want to migrate my site but like you all will be left with no other choice.

  • Can anyone else confirm whether the addition of swap (according to @kamaln7's suggestion) resolves the problem?

    Currently I'm seeing this occur about once a week on my 1 GB RAM / Ubuntu 14.04 instance.

  • Same here with WP on 1GB / Ubuntu 14.04.

    Things I've tried that had no effect (site never changes from DB error message)

    • Restarting mysql service
    • Rebooting
    • I can log into mysql as root, (and the wordpress user) fine over SSH
    • There are no errors in the mysql log, and nothing interesting in apache2's log
    • This site does not have lots of plugins, and gets next to zero traffic.
    • Droplet is not swapping - htop shows about 200mb of the 1GB in use.

    Waiting on support for ideas.

  • I have same happened two time in last couple days, it was working flawless until two days ago !

  • It has happened to me on at least two occasions in the last week or so. Have to say that I hadn't had that issue when I was with Ramnode.

  • Same for me, have a simple standard wordpress and woocommerce running, in the mean time on a 4 GB ram droplet and after 1-3h it shuts down with that database error. I don't know what i'm paying for and support sends me links to caching community do it yourself pages - just annoying - I was very happy with digital ocean so far

  • Same problem here on both of my sites!! Will be migrating my sites to another provider and closing my D.O account tomorrow.

  • Same problem for me, since a couple of days ... I will close my account too il nothing is done by the digital ocean team...

  • The fact that DO isn't on threads like this helping people or making public statements about something that is so clearly not working is a huge turn off. I really enjoyed using their servers for Python web apps, but I'll be shortly migrating everything somewhere else. I'd honestly rather give my money to godaddy at this point since my sites at least worked there.

  • Same over here. I'm opening support tickets and receiving bullshit answers. Nobody at DO seems to be aware of this issue.

  • Having the same issue here. Using Ubuntu WordPress on 14.04 which only has admin traffic and isn't launched yet. MySQL is now crashing every 24 hours. Come on DO. Is there nothing to suggest here? Used Bluehost for 10 years without fail, but figured I'd try some new juice. It would have been easier to configure a machine on AWS, at least they have functional support team.

  • same issue, problem just started happening, have been in touch with support, restarts not working and an upgrade didn't help

  • Same issue here. WTF.

  • This is becoming cumbersome. I don't buy it; Everyone simultaneously cannot be experiencing spikes in traffic and/or increase in ram utilization.

  • Same problem here, works for a bit after I restart the droplet, came back this morning and it's down again. Looks like it's time to migrate elsewhere...

  • I got he same issue, pretty annoying. Reboot helps, this problem keeps popping up once a week.

  • Same issue, suddenly 2 out of my 5 droplets have this error.

  • Same boat here... It's really frustrating having to sudo reboot my server every time Jetpack notifies me my sites are down (every day and a half or so). Hope we get some leadway on working towards a fix. I'll be researching in the meantime. Digital Ocean, any thoughts? It would would be great to get a representatives input on the issue. I haven't experienced issues like this before with other webhosts, so I'm considering switching, but I cannot deny I love the speed of your SSD servers.

  • Getting exactly the same problems, what a crock of sh*t DO, why don't you DO (see what i did there?) something about it?

  • Hey there! We have some solutions for this issue in the Answers below. Definitely check them out, especially the replies by @kamaln7 and @nestchris, as well as my own.

  • I have the same problem and tried a number of suggestions, including upgrading to a 1GB droplet, adding a swap file etc.. same problem.

    I ended up creating an account here , transferred a copy of my site and it cleared up the problem, improved the performance dramatically and provided us with 24/7 support. This is one of those things where you get what you pay for.

  • Also having the same issue. This happens fairly frequently

  • Hi @curiousgeorge, check out our community article here or my response in the "Answers" section below.

    by Jon Schwenn
    WordPress is a popular and powerful CMS (content management system) platform. Its popularity can bring unwanted attention in the form of malicious traffic specially targeted at a WordPress site. There are many instances where a server that has not been protected or optimized could experience issues or errors after receiving a small amount of malicious traffic. This guide will show you how to protect WordPress from XML-RPC attacks on an Ubuntu 14.04 system.
  • I've had the same problem. Tried for weeks, went through all the suggested solutions without any success. Digital Ocean support weren't able to help either. I've migrated the Wordpress to another provider now, but shortly after the migration, have had the same problem there. So my current hypothesis is that Wordpress has become very memory hungry in more recent versions, and/or there's specific memory leaks with some common plugins. I've just increased memory to 2GB and swap to 2GB to see if the extra headroom fixes it.

  • I had the exact same thing. Switched Wordpress over to AWS hosting and the site has been stable since.

  • Same problem here.

25 Answers

EDIT: If you're fairly certain you're not suffering from an xml-rpc attack, check out our article about debugging Wordpress database errors.

EDIT: We now have a community article covering xml-rpc attacks!

Hey there,

As @nestchris said, we have started seeing an influx of brute-force amplification attacks, both on our service and the rest of the internet at large. These attacks exploit the XML-RPC functionality in WordPress, as described on

You can see if you are being taken down by this attack by running one of the following two commands on your droplet, depending on which webserver you're running:

grep xmlrpc /var/log/apache2/access.log
grep xmlrpc /var/log/nginx/access.log

If you get any results there, it should be easy to fix this issue. If you've spun up a WordPress One-Click install droplet since Dec 2015, there's a small config file which you can enable to block such attacks. We don't enable it by default because it breaks some commonly used plugins (like Jetpack). Here are the commands you can run:

sudo a2enconf block-xmlrpc
sudo service apache2 restart

Alternatively, Jetpack has functionality to block these requests without impacting other plugins. Check out their Protect module.

If you're running a WordPress droplet older than Dec 2015 and you don't want to use the Jetpack plugin from Automattic, you'll need to configure the droplet manually to block requests to xmlrpc.php:

Apache2 (goes inside your VirtualHost config):

<files xmlrpc.php> 
order allow,deny 
deny from all 

Nginx (goes inside your server{} block):

location /xmlrpc.php { 
deny all; 

Alternatively, if you are not having this issue due to an XML-RPC brute force attack, your Droplet might not have enough memory to deal with your traffic reliably. We recommend users run WordPress on a 1GB droplet or larger.

In order to help reduce your memory usage, you'd first want to check for any plugins using a large amount of memory. There are a lot of memory hungry plugins out there! You may also want to optimize your configuration files.

Below are some great resources that we commonly recommend in support tickets, explaining how to do so, depending on your setup:




If none of this works, you may have no choice but to resize to a larger sized droplet that can handle your traffic.

Platform Support Specialist

by Matthew Nuzum
Apache is an amazingly powerful and capable web server. In order to make initial setup as easy as possible, it comes with numerous modules pre-installed. This makes it a great choice for new projects when you need to quickly be productive, but as your site grows you may start to bump into performance problems. This tutorial will cover a few things you should do to optimize performance if you’re running Apache with your Django app.
  • Thanks for this Eris! Good to finally hear from someone about this issue. My droplet was spun up before Dec 2015, and was running Jetpack with protect enabled but I am still encountering the problem, I'm fairly sure there were no traffic spikes either. Will try manually blocking requests to xmlrpc.php though.

    • No problem! If you have Jetpack enabled with Protect, then you should not have to worry about the issue I outlined above. I would not recommend manually blocking xmlrpc.php if you're using Jetpack, because that will break Jetpack. It also probably won't do anything.

      If you want, you could verify this by checking your access log as I outlined above, but I would honestly approach this issue as if you're simply running out of memory. The optimization links towards the end of my post are useful to this end, as are caching plugins (like wp super cache). It could also be that one of your plugins has a memory leak, which is actually fairly common. You may want to disable plugins one by one and see how that affects your memory usage.

      Platform Support Specialist

  • I love you for this! <3

  • If the issue is caused by XML-RPC spam, then another alternative to Jetpack is the Wordpress plugin "All in One WP Security", which has - in addition to its many other security features - a setting to block XMLRPC (WP Security > Firewall > Basic Firewall Rules > Enable Pingback Protection).

  • Thank you. It was helpful.

  • Hello, I've run sudo a2enconf block-xmlrpc before install JetPack, and now I'm getting a error "Your JetPack has a glitch".
    errors details: "The Jetpack server could not communicate with your site's XML-RPC URL..."

    I think it's because it's blocked. How can I 'unblock' it again to install JetPack?


    • You can unblock xml-rpc by running sudo a2disconf block-xmlrpc and restarting apache2 again (sudo service apache2 restart)

  • Alright. I'm literally fuming.

    This is happening CONSTANTLY. As in less than 5 minutes apart.

    I've done everything I can find and it is still happening. The site has 0 traffic. It's literally just gotten started. Please. Tell me what to do... I can't take any more.

    • If you tried everything, I'd recommend opening a support ticket. This way, a member of our support team can give you advice tailored specifically to your Droplet. It's possible that this is not related to the XML-RPC issue that many wordpress blogs face.

      • I have opened a ticket and now I am waiting, but in the mean time I can hardly do anything on the site because I must constantly restart the server, and Jetpack keeps emailing me every few minutes about the site going down.

It seems like MySQL is running out of RAM and crashing. You could add swap and see if that helps (, or upgrade your droplet to the next plan so that there's more RAM available for MySQL.
by Etel Sverdlov
Linux swaps allow a system to harness more memory than was originally physically available. Here's how to set up a linux swap file on Ubuntu 12.04
  • Seems to me, that company never fix this to make you move to higer plans. Looking for alternatives to move.

    • I'm beginning to feel that way was well. I've already upgraded but I refuse to upgrade any further on my current budget.

  • One concern with adding swap is that swap is not real memory - its disk. Even the fast SSD disks are an order of magnitude slower than memory can be.

    Consider adding swap as a bandaid to get you through until you can upgrade to a larger size OR optimize your configuration.

As we know, a shared host like Bluehost has more direct control over your server. On DO, you are required to maintain the server, and that means things that you might not need to deal with on other hosts come up here.

I was made aware of this exploit by the DO team.

After implementing the apache.config solution, my problems stopped.

[Apache VirtualHost Config]
<files xmlrpc.php>
order allow,deny
deny from all

This attack is affecting all wordpress sites, but if you were using another host before, you may not have noticed. In my experience with Bluehost, I have had them suspend my account because I was attacked by comment spam, I suspect something similar to that would have occurred in this instance as well.

Unfortunately DO has been very slow to address this issue with the community as it seems everyone is in the dark about it.

  • I believe a community article should be in the works for this. In the meantime, we've since updated the base WordPress One-Click to have an easy way to block these attacks. Unfortunately, we can't enable it by default because it breaks some commonly used plugins (like Jetpack). Anyone who created a WordPress droplet since December 2015 should have this config file that they are able to enable. We recommend it all the time on Support.

    Here are the commands you can run to do so:

    sudo a2enconf block-xmlrpc
    sudo service apache2 restart

    Alternatively, Jetpack has functionality to only whitelist certain IPs for the API. If you have Jetpack Protect enabled, you should not need to worry about XML-RPC attacks.

I have had the same problem and its not a fault of digital ocean. A little bit knowledge in server maintenance is what is actually required.

Ubuntu 14.04

Check if your mysql is running
mysqladmin -u root -p status

and then enter your mysql root password

if it's running properly u get a message
Uptime: 4 Threads: 1 Questions: 62 Slow queries: 0 Opens: 51 Flush tables: 1 Open tables: 45 Queries per second avg: 15.500

or an error message

mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!

This error message is the reason for you to get Error establishing a database connection

fix this by starting the mysql with the command
sudo start mysql

I hope my solution helps you in fixing your issue.

please tired all week my site falls due this connection error will not change cloud service more

Problem solve with this code.. Very simple :)

sudo start mysql

Can someone recommend a good Wordpress host because this is a fucking disaster.

I Have done all of these things; spoke to support who kindly suggested it ; and i am STILL getting this issue. I am thinking about leaving DO because this is a business I am running and random error establishing a connection shouldn't be happening, especially if I go to sleep and find out at 1AM it went down and didn't find out until I got up at 7AM! This has been going on for now three months. I also upgraded and I STILL have the issue. Please DO , fix it, I have tried on my end and it is STILL happening. I am frustrated about it. I have opened a support ticket and sill no fix.

Thanks Kamal, swapfile seems to be working for now :)

If you're faced with the excruciating Database Connection Error on your WordPress website, there are a number of factors which could be causing it. Perhaps you overlooked something in trying to fix it. Also check out this blog post which shares some good tips to overcome the Database Error. URL: Error establishing a database connection in WordPress

Having the same issue now as well. Worked fine for about 3 weeks now getting this issue multiple times per day and restarting the whole server works but is not a solution... Unreal that this hasn't been solved by now.

My problem was Mysql user privileges. My user didn't have database privileges.
This simple piece of code helped me. Try this before anything.

$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
echo 'Connected successfully';

me too... i don't know what happen, when i try to open my website, the text "Error establishing a database connection" was there... hope there are some support from DO.

I am not a real tech guy, but I was able to fix our website,, by turning off the power of the droplet and turning it on.

Support forums where amateurs try to fix supplier problems are an excellent way to cut support costs. What a smart industry paradigm.

Digital Ocean posted this helpful article to hell stop XML-RPC attacks, that can cause errors like mentioned on this thread.

I installed Jetpack (and turned on "Protect" as recommended as the Method 1) and it appears to be working so far.

Link to the helpful article:

by Jon Schwenn
WordPress is a popular and powerful CMS (content management system) platform. Its popularity can bring unwanted attention in the form of malicious traffic specially targeted at a WordPress site. There are many instances where a server that has not been protected or optimized could experience issues or errors after receiving a small amount of malicious traffic. This guide will show you how to protect WordPress from XML-RPC attacks on an Ubuntu 14.04 system.

This error can also be triggered because of a DOS attack, please make sure you are not the victim of this attack before investigating for other causes. Learn more here

Every time i did anything within the Wordpress admin, like adding a plugin it would topple my server. The database connection would be stopped for about 2minutes after any update and i'd have to wait for the page I was editing to reappear again. Drove me nuts.

After powering off the server and resizing it from 512mb to 1gb I can now manage 2 Wordpress sites with ease without it falling over every 5 mins when editing. On one website, I have woocommerce and buddy press running, which i've found is too much for the 512mb server setup.

As i'm new to managing my own server, I underestimated the server requirements needed for managing Wordpress websites & all the hungry plugins.

I'm relieved I found a solution (albeit one requiring more spend on the server set-up) In the process of elimination I added more memory for mysql, checked many log files for attacks and errors. Added additional defence against brute force attacks.

Here's my error logs from this exact issue:

170503 7:58:36 [Note] Plugin 'FEDERATED' is disabled.
170503 7:58:36 InnoDB: The InnoDB memory heap is disabled
170503 7:58:36 InnoDB: Mutexes and rw_locks use GCC atomic builtins
170503 7:58:36 InnoDB: Compressed tables use zlib 1.2.8
170503 7:58:36 InnoDB: Using Linux native AIO
170503 7:58:37 InnoDB: Initializing buffer pool, size = 128.0M
InnoDB: mmap(137363456 bytes) failed; errno 12
170503 7:58:37 InnoDB: Completed initialization of buffer pool
170503 7:58:37 InnoDB: Fatal error: cannot allocate memory for the buffer pool
170503 7:58:37 [ERROR] Plugin 'InnoDB' init function returned error.
170503 7:58:37 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
170503 7:58:37 [ERROR] Unknown/unsupported storage engine: InnoDB
170503 7:58:37 [ERROR] Aborting
170503 7:58:37 [Note] /usr/sbin/mysqld: Shutdown complete

  • I've resolved this pesky issue by setting up swap space on the droplet.
    By default, swap is set to 0 on Ubuntu Wordpress Droplets

You can use this script to restart your server whenever it gives database connection error.

Nothing helped me guys. The only solution i got, power off the droplet and power on again. It will solve your problem. I got result.

Have another answer? Share your knowledge.