MySQL shutting down unexpectedly on Amazon EC2: the swap solution
Scenario
While testing sample applications and trying to achieve some POC (proof of concept) of some idea, one may have taken the Amazon EC2 deploy path with MySQL as the running database. After sometime leaving the application running, one may have problems with the application stability and may notice that MySQL is being shut down .
The possible problem
While checking the /var/log/syslog or any other log file (and there shall be some of them), it is possible to find the following line:
InnoDB: Fatal error: cannot allocate memory for the buffer pool
If it’s there -somewhere-, then the cause may be the lack of RAM. When the RAM is touched to its limits, MySQL won’t work correctly and will be shut down immediately.
This discovering leads us to find that a swap partition/file was not created on the EC2 machine operating system and it would be of great use.
The possible solution – the swap solution
To work around this problem, one shall create a swap file a mount it into the system, so MySQL can use the swap file and won’t be shut down.
Here, we create a 1GB swap file and mount it to the EC2 machine.
1 2 3 | sudo dd if=/dev/zero of=/mnt/1GB.swap bs=1024 count=1048576 sudo mkswap /mnt/1GB.swap sudo swapon /mnt/1GB.swap |
After these commands, we can edit /etc/fstab and add the follwing line:
1 | /mnt/1GB.swap none swap sw 0 0 |
One can check the swap file that was created typing the following comand:
1 | sudo swapon -s |
After that, the system may behave better.
Conclusion
To prevent MySQL to be shut down unexpectedly on Amazon EC2, one shall create a swap file to allow memory dumping.