Adding Swap Space to Amazon EC2 Micro Instances

Most of us use EC2 micro instances for prototyping and running small servers. If you use MySQL on an EC2 machine, you may experience the MySQL server automatically shut down with the /var/log/mysql/error.log file reading as below:

140516 10:54:24 InnoDB: Initializing buffer pool, size = 128.0M
InnoDB: mmap(137363456 bytes) failed; errno 12
140516 10:54:24 InnoDB: Completed initialization of buffer pool
140516 10:54:24 InnoDB: Fatal error: cannot allocate memory for the buffer pool
140516 10:54:24 [ERROR] Plugin 'InnoDB' init function returned error.
140516 10:54:24 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
140516 10:54:24 [ERROR] Unknown/unsupported storage engine: InnoDB
140516 10:54:24 [ERROR] Aborting

The cause of the frequent server shutdowns is the lack of available memory for allocation to InnoDB. EC2 micro instances have only 613MB of memory and no swap space. The easiest method to recover from the above error is to create a swap space for your machine.

To create your swap space, execute the following code as root user:

dd if=/dev/zero of=/swapfile bs=1M count=1024
mkswap /swapfile
swapon /swapfile

To create the swap spaces on server start, add the following line to /etc/fstab

/swapfile swap swap defaults 0 0

After creating the swap space, you should be able to start your MySQL service again.

1 thought on “Adding Swap Space to Amazon EC2 Micro Instances

  • Thanks, dude… But, can I run this commands (dd if=/dev/zero of=/swapfile bs=1M count=1024) at a already installed and operational server? Could I loose my partitions and everything?!

Leave a Reply

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