如果我们使用的vps虚拟机内存偏低时,例如512M或1G时候,mariadb或mysql运行时候出现db崩溃,而且无法启动,启动错误常见如下:
Mar 28 15:25:46 mariadbd: 2022-03-28 15:25:46 0 [ERROR] mariadbd: Out of memory (Needed
Mar 28 15:25:46 mariadbd: 2022-03-28 15:25:46 0 [ERROR] mariadbd: Out of memory (Needed 128180224 bytes)
Mar 28 15:25:46 mariadbd: 2022-03-28 15:25:46 0 [ERROR] mariadbd: Out of memory (Needed 96116736 bytes)
Mar 28 15:25:46 mariadbd: 2022-03-28 15:25:46 0 [ERROR] mariadbd: Out of memory (Needed 72081408 bytes)
Mar 28 15:25:46 mariadbd: 2022-03-28 15:25:46 0 [ERROR] mariadbd: Out of memory(Needed 54042624 bytes)
Mar 28 15:25:46 mariadbd: 2022-03-28 15:25:46 0 [ERROR] InnoDB: Cannot allocate memory for the buffer pool
Mar 28 15:25:46 mariadbd: 2022-03-28 15:25:46 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
Mar 28 15:25:46 mariadbd: 2022-03-28 15:25:46 0 [Note] InnoDB: Starting shutdown…
Mar 28 15:25:46 mariadbd: 2022-03-28 15:25:46 0 [ERROR] Plugin ‘InnoDB’ init function returned error.
Mar 28 15:25:46 mariadbd: 2022-03-28 15:25:46 0 [ERROR] Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed.
Mar 28 15:25:46 mariadbd: 2022-03-28 15:25:46 0 [Note] Plugin ‘FEEDBACK’ is disabled.
Mar 28 15:25:46 mariadbd: 2022-03-28 15:25:46 0 [ERROR] Unknown/unsupported storage engine: InnoDB
Mar 28 15:25:46 mariadbd: 2022-03-28 15:25:46 0 [ERROR] Aborting
一般常用的解决方式有两种:
一、在my.cnf文件或者/etc/my.cnf.d/server.cnf中[mysqld]段内加入performance_schema = off,并重启mariadb或mysql可以解决,但是我的vps是512M内存,经过实际测试不行。
二、扩充swap分区
1、使用free -m命令查看内存及swap分区使用情况(如下),会发现swap已经耗尽为0,预计是准备扩充swap分区
free -m total used free shared buff/cache available Mem: 503 398 14 31 91 61 Swap: 131 131 0
2、输入如下命令增加swap分区(1G)
dd if=/dev/zero of=/var/swap bs=1024 count=1048576 mkswap /var/swap
3、激活swap分区,然后用free -m命令查看,或者用cat /proc/swaps查看
swapon /var/swap
4、加入开机启动挂在swap分区,修改/etc/fstab文件加入:
/var/swap swap swap defaults 0 0
5、重启mariadb或mysql即可正常启动
systemctl start mariadb
systemctl status mariadb