DigitalOcean WordPress Database Connection Cannot Be Reestablished

0

This happened again on one of my servers. The last time, it had to do with the lack of a swap file and then running out of physical memory. Usually a reboot helps which you do like this:

  1. Rebooting a Digital Ocean image by ssh into the machine and then sudo service apache2 restart and if that doesn’t work reboot the droplet with sudo shutdown now -r
  2. More in depth debugging. If you don’t mind having a server down or you were smart enough to have a load balancer so you can run two servers at once (yes, that’s on my list), you can check things like the amount of memory available. But you use sudo netstat -plt to see if mysql is running and restart with sudo service mysql start. If that works, then you can grep the log looking for low memory zgrep -a "allocate memory" /var/log/mysql/error.log* and you can see what is going on. You can also use free -h to see how much memory you really have. In our case, a 1GB machine had 151MB available. Which really isn’t very much. so a 2GB machine is probably better.
  3. Swapfile for safety. If you are running out of memory, they don’t recommend using a swapfile because it degrades the SSD, so it’s better to just upgrade the droplet. Personally, I like the swapfile as a soft failure. But you can see if you have any with sudo swap on --show and if return nothing, then you don’t have it. The double check is to run free -h and see if Swap has anything. The do a df -h to see if you have disk space. In our case, of our 25GB SSD, we were 80% free so no problem there. Then you allocate a swapfile that is equal to your memory, so sudo fallocate -l 1G /swapfile, then make it read only and start it sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo mkswap on. Finally, if this works sudo free -h should show it and make this permanent by adding a running echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab to add a line to the table.
  4. Resize Droplet. Of course, the better long term thing to do is to upgrade the droplet as WordPress in DigitalOcean really needs more that 1GB. For a website you use quite a bit, you probably want more like a $10/month site with 2GB and 50GB of SSD.

Related Posts

© All Right Reserved