Mysql 运行一段时间后自动停止

最近Mysql可以正常启动,但运行一段时间后会自动停止,停止后又可以手动正常启动。反反复复这样子,然后刚学会查log的博主就决定查查怎么回事。经过查询,log如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
150226 11:39:13 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be removed in a future release. Please use the full name instead.
150226 11:39:13 [Note] Plugin 'FEDERATED' is disabled.
150226 11:39:13 InnoDB: The InnoDB memory heap is disabled
150226 11:39:13 InnoDB: Mutexes and rw_locks use GCC atomic builtins
150226 11:39:13 InnoDB: Compressed tables use zlib 1.2.8
150226 11:39:13 InnoDB: Using Linux native AIO
150226 11:39:13 InnoDB: Initializing buffer pool, size = 128.0M
InnoDB: mmap(137363456 bytes) failed; errno 12
150226 11:39:13 InnoDB: Completed initialization of buffer pool
150226 11:39:13 InnoDB: Fatal error: cannot allocate memory for the buffer pool
150226 11:39:13 [ERROR] Plugin 'InnoDB' init function returned error.
150226 11:39:13 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
150226 11:39:13 [ERROR] Unknown/unsupported storage engine: InnoDB
150226 11:39:13 [ERROR] Aborting
150226 11:39:13 [Note] /usr/sbin/mysqld: Shutdown complete

需要解决的有一个Warning和一个ERROR。

1
[Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be removed in a future release. Please use the full name instead.

这个是因为Mysql配置文件/etc/mysql/my.cnf中使用了简称,需要这两语句

1
2
3
4
5
[mysqld]
...
key_buffer = 16M
...
myisam-recover = BACKUP

改为

1
2
3
4
5
[mysqld]
...
key_buffer_size = 16M
...
myisam-recover-options = BACKUP

注意:只修改[mysqld]部分,其余部分不改变。
Fatal error: cannot allocate memory for the buffer pool
这是因为VPS内存不足,但也不需要升级,只需要把Initializing buffer pool size调小。

在Mysql配置文件/etc/mysql/my.cnf文件[mysqld]部分开头增加一句

1
2
3
[mysqld]
innodb_buffer_pool_size=64M
...

然后重启Mysql即可

事实证明我还是太天真,问题重现。这次我选择了添加一个512M的swap分区,毕竟SSD速度也棒棒哒。

在/mnt/创建一个512M的512Mb.swap文件

1
sudo dd if=/dev/zero of=/mnt/512Mb.swap bs=1M count=512

注:bs = block size 区块大小; count = 区块个数;

格式化文件,使之成为swap(交换设备)

1
sudo mkswap /mnt/512Mb.swap

将swap加入到现有系统中

1
sudo swapon /mnt/512Mb.swap

编辑 /etc/fstab 文件,做永久性变更

1
sudo nano /etc/fstab

在文件末尾增加一句

1
/mnt/512Mb.swap  none  swap  sw  0 0

保存退出。这样重启后Swap分区也依然有效。

添加swap后运行了一天暂没问题,应该OK了,如果还有问题持续更新。