Error establishing a database connection (WordPress)

May 25, 2014 81.6k 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...
43 comments
  • 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.

  • Show 39 more comments
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 Securi.net

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:

Apache:
grep xmlrpc /var/log/apache2/access.log
Nginx:
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 
</files>

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:

Apache:
https://www.digitalocean.com/community/tutorials/how-to-optimize-apache-web-server-performance
https://github.com/gusmaskowitz/apachebuddy.pl

Nginx/php-fpm:
https://www.digitalocean.com/community/tutorials/how-to-optimize-nginx-configuration
http://blog.chrismeller.com/configuring-and-optimizing-php-fpm-and-nginx-on-ubuntu-or-debian

MySQL:
http://mysqltuner.com/
http://www.percona.com/blog/2014/01/24/mysql-server-memory-usage-2/

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

Best,
Eris
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.

      Best,
      Eris
      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?

    Thank

    • 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 (https://www.digitalocean.com/community/articles/how-to-add-swap-on-ubuntu-12-04), 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.
https://blog.sucuri.net/2015/10/brute-force-amplification-attacks-against-wordpress-xmlrpc.html

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

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

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.

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

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, www.clienteleitsolution.com, 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: https://www.digitalocean.com/community/tutorials/how-to-protect-wordpress-from-xml-rpc-attacks-on-ubuntu-14-04

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

Solved.
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.
https://github.com/Gurnoor30196/Launch-Mysql

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.