三种方案解决Aliyun VPS MariaDB内存溢出导致服务自动停止

提供三种解决方案

方法一

近几天aliyun服务器异常,数据库无法连接,网站不能访问。

查看mariadb日志,打开: /var/log/mariadb/mariadb.log

160915 19:44:22 InnoDB: Fatal error: cannot allocate memory for the buffer pool
160915 19:44:22 [ERROR] Plugin 'InnoDB' init function returned error.
160915 19:44:22 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
160915 19:44:22 [ERROR] mysqld: Out of memory (Needed 128917504 bytes)
160915 19:44:22 [ERROR] mysqld: Out of memory (Needed 96681984 bytes)
160915 19:44:22 [ERROR] mysqld: Out of memory (Needed 72499200 bytes)

是内存不够,购买是阿里云主机1G内存,不够用了,可以创建swap分区来解决。

创建4g swap分区

1
2
dd if=/dev/zero of=/var/swap bs=1024 count=4194304 
mkswap /var/swap

激活swap分区

1
swapon /var/swap

设置自动挂载

1
2
vi /etc/fstab
/var/swap               swap                    swap    defaults        0 0

重启服务器

1
shutdown -r now

查看内存使用状态

1
free -m
1
2
3
              total        used        free      shared  buff/cache   available
Mem:            991         347         261          40         382         382
Swap:          4096           0        4096

创建swap分区成功,再没因内存不够而maridb自动关闭了。

方法二:只需要在my.cnf配置文件中的[mysqld]段内,加入performance_schema = off即可。

方法三:定时检测数据库状态,并重启

5 * * * * * systemctl status mariadb.service || systemctl restart mariadb.service