LAMP and memory issues in digitalocean 5$ droplets

I recently move cloud hosting of my domain and some other personal domains from ec2 to digitalocean.
installed softwares, did hardening on security and installed LAMP configure my domain and other some other domains.

I noticed that after 20-30 minutes MySQL flat out died, resulting to all domains using mysql to issue an error regarding database connectivity.

MySQL died due to oom killer killing it , having garnered the top score prior to the kill.
This lead me to adjust oom_score_adjust to a very low value to prevent oom killer from killing MySQL.   Below is my script to adjust mysqld oom_score


OOM_SCORE=`cat /proc/$(pidof mysqld)/oom_score`
if [ "$OOM_SCORE" -gt 20 ]
 echo "adjust oom_score_adj by $OOM_ADJUST"
 echo "-$OOM_ADJUST" > /proc/$(pidof mysqld)/oom_score_adj

It basically did what I want and now MySQL works as expected.  But that’s not the root of the issue.  Basically if oom killer kicks in, it means you ran out of memory.

By default the cheapest plan for digitalocean does not have a configured swap space.  In a nutshell, Swap space are used when the OS ran out of memory.

Ideally,  you do not want mysql to swapped out of memory because it lowers performance especially on high traffic websites.  But for my needs its ok.

Installing a swap space.

There are two methods of having a swap space.  The most popular is installing a swap partition and another method is installing a swap file.

unlike amazon ebs back t1.micro instances 8GB disks, digitaloceans cheapest droplents have 20GB.  Its big enough that I can use some as a swap file.

1) Create a 1GB file called swapfile

#dd if=/dev/zero of=/swapfile bs=1024 count=1048576

2) Prepare the file to be used as a swap file

#mkswap /swapfile

3) Activate the swap file

#swapon /swapfile

4) Check the newly activated swap

# free -m

5) Make changes permanent activate swap during the boot process

#echo "/swapfile swap swap defaults 0 0" >> /etc/fstab

Leave a Reply

Your email address will not be published. Required fields are marked *