WordPress / MySQL keeps crashing on Ubuntu / Digital Ocean

I host a couple of WordPress sites on Ubuntu hosted on Digital Ocean droplets. I use their standard WordPress application (pre built WordPress on Ubuntu) with the smallest size Droplet possible. It has just 512MB Ram and 20GB SSD Disk and 1 logical core – i.e. it isn’t very quick.

Anyhow, I started to have a few problems with MySQL crash. I manifested itself by WordPress complaining that it could not connect to the database. I could fix the problem by restarting MySQL – ‘sudo service mysql restart’.

The longterm fix is to give MySQL more memory but I didn’t really want to do that. So, I opted to provide the server with more swap space. Given that Digital Ocean server are SSD’s this would be quite quick.

https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04

Here is how you add swap to an Ubuntu server:

1 – Check you what swap you have.

sudo swapon -s

If you get blank response then you don’t have any swap. Digital Ocean doesn’t seem to provide swap by default.

2 – Check how much disk space you have.

df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            234M     0  234M   0% /dev
tmpfs            49M  528K   49M   2% /run
/dev/vda1        20G  2.3G   17G  12% /
tmpfs           245M     0  245M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           245M     0  245M   0% /sys/fs/cgroup
tmpfs            49M     0   49M   0% /run/user/1000

I had 17G and so could easily give the server 1g swap.

3 – Use fallocate to reserve space on the file system. Fallocate is used because it doesn’t bother to write anything to the file system. It just allocates the space on the disk.

sudo fallocate -l 4G /swapfile

4 – Set file permissions

sudo chmod 600 /swapfile

5 – Convert it to a swap file

sudo mkswap /swapfile

6 – Add the swap

sudo swapon /swapfile

7 – Check that the swap is there

sudo swapon -s

8 – Add it to the fstab
so that it is mounted when the server reboots

sudo nano /etc/fstab

and put this line at the bottom:

/swapfile   none    swap    sw    0   0

Finally, you can check it has all worked…

sudo swapon -s
Filename				Type		Size	Used	Priority
/swapfile                              	file    	1194300	103160	-1

3 thoughts to “WordPress / MySQL keeps crashing on Ubuntu / Digital Ocean”

  1. Really great article James, appreciate you spending the time to share this. I’m having the same issue with my VPS on Digital Ocean. I’m using htop to monitor my memory usage and I can see i’m using about half of it up while no ones accessing it!

    Need to research more as to what a “swap” is before deciding to do this. Ideally i’d like DO to try and resolve why mySQL memory overflow as i’ve spoken to other people that use other VPS providers and they don’t seem to have an issue.

    Keepup the good work bro!

    1. You shouldn’t worry about linux / mysql using lots of memory when the server isn’t under load. See this article. It’s only an issue if mysql doesn’t have enough memory under load and you don’t want to pay for more memory.

Leave a Reply to james Cancel reply