mysql服务无法启动的一个坑

网站运行在阿里云的一个国外服务器,ubuntu系统,之前状态一直良好,但是最近老是莫名其妙出现连接mysql失败的错误导致网站无法访问,通过systemctl status mysql.service 和 journalctl -xe 查看,由于不具备相关经验,只是知道mysql启动失败了,然后查看 cat /var/log/mysql/error.log 发现了关键信息:

2017-08-10 18:11:03 9772 [Note] InnoDB: Initializing buffer pool, size = 128.0M
InnoDB: mmap(136019968 bytes) failed; errno 12
2017-08-10 18:11:03 9772 [ERROR] InnoDB: Cannot allocate memory for the buffer pool
2017-08-10 18:11:03 9772 [ERROR] Plugin 'InnoDB' init function returned error.
2017-08-10 18:11:03 9772 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2017-08-10 18:11:03 9772 [ERROR] Unknown/unsupported storage engine: InnoDB
2017-08-10 18:11:03 9772 [ERROR] Aborting

其中InnoDB: mmap(136019968 bytes) failed; errno 12是关键的错误信息。
从网上查资料,有人说修改innodb_buffer_pool_size,经过测试无效。
有人说是swap分区为0导致的此错误,使用free -m命令查看系统内存,发现swap确实为0。使用如下命令建立一个临时的swap分区:

1. dd if=/dev/zero of=/swap bs=1M count=512  //创建一个swap文件,大小为512M
2. mkswap /swap                              //将swap文件变为swap分区文件
3. swapon /swap                              //将其映射为swap分区

此时使用free -m命令即可看到swap分区已存在了,然后启动mysql服务即可:

/etc/init.d/mysql restart
service mysql restart

为了保证下次系统启动后,此swap分区被自动加载,需要修改系统的fstab文件,操作如下:

vi /etc/fstab
//在其中添加如下一行
/swap swap swap defaults 0 0

保存文件即可。

参考资料:
Mysql 5.5+ 错误InnoDB

你可能感兴趣的:(mysql服务无法启动的一个坑)