Fix MySQL stops unexpectedly with EC2 Micro Instance 解决MySQL在EC2无法启动的问题

Reference:

If you had ever encountered the error as follows (tail /var/log/mysqld.log):
如果你的MySQL服务器忽然中止而且无法再启动,或者启动后很快又中止,日志如下:
Screen Shot 2013-11-19 at 9.49.25 AM

120423 09:13:38 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
120423 09:14:27 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
120423  9:14:27 [Note] Plugin 'FEDERATED' is disabled.
120423  9:14:27 InnoDB: The InnoDB memory heap is disabled
120423  9:14:27 InnoDB: Mutexes and rw_locks use GCC atomic builtins
120423  9:14:27 InnoDB: Compressed tables use zlib 1.2.3
120423  9:14:27 InnoDB: Using Linux native AIO
120423  9:14:27 InnoDB: Initializing buffer pool, size = 512.0M
InnoDB: mmap(549453824 bytes) failed; errno 12
120423  9:14:27 InnoDB: Completed initialization of buffer pool
120423  9:14:27 InnoDB: Fatal error: cannot allocate memory for the buffer pool
120423  9:14:27 [ERROR] Plugin 'InnoDB' init function returned error.
120423  9:14:27 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
120423  9:14:27 [ERROR] Unknown/unsupported storage engine: InnoDB
120423  9:14:27 [ERROR] Aborting

That’s because the micro instance doesn’t bring with Swap page.
这是因为EC2微型实例没有Swap页面而导致的MySQL致命错误。

Solution is here. Run the command:
解决方案如下,请在终端里运行:

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

 

Finally, add this line /swapfile swap swap defaults 0 0 to /etc/fstab
最后,在/etc/fstab 这个文件添加此行:

/swapfile swap swap defaults 0 0

 

Now you can restart mysqld by running: sudo service mysqld restart
现在你可以重启mysqld了: sudo service mysqld restart