Thursday, November 3, 2016

MySQL crashing on EC2 Micro Linux instance

Amazon Web Services (AWS) is great place to start playing / learning cloud technologies. AWS offers EC2 micro instances free of cost. This allows developers (like me) to try out various things, which in real corporate life very hard to do.

Last week I was playing with my EC2 micro instance. I installed Apache, MySQL and WordPress. I configured everything according to great tutorial published on AWS. Everything was great, working fine. But then I encountered an issue, my blog was failing more than couple of times a day. 

Problem:
On digging I found out that the MySQL daemon was failing due to insufficient memory. 

Details:
EC2 micro instance provides you with 1 vCPU and 1 GB of memory. Clearly this is not ideal for big application. But I am just creating a person blog site with only I am being the user :), so this should be sufficient. 
Problem lies with default setup of Linux, Apache and MySQL. Apache and MySQL tries to hog resources and with default Linux setup there is no swap memory allocated.

Solution:
After doing few hours of googling I arrived at following solutions (which worked for me)

1. Allocate swap 
  • Run dd if=/dev/zero of=/swapfile bs=1M count=1024
  • Run mkswap /swapfile
  • Run swapon /swapfile
  • Add this line /swapfile swap swap defaults 0 0 to /etc/fstab 
You might have to run above commands using sudo access. (Credit: http://www.prowebdev.us/2012/05/amazon-ec2-linux-micro-swap-space.html)

2. Reduce MySQL buffer pool size
Add following to /etc/my.cnf
[mysqld]

innodb_buffer_pool_size=64M

Then restart the MySQL and Apache. This should fix the issue of MySQL crashing on EC2 Micro Instance.

No comments:

Post a Comment