Centos7 MySQL 错误 InnoDB: Cannot allocate memory for the buffer pool

最近跑博客的小机总出现 Mysql 挂掉的情况

日志如下:

2020-03-29T12:47:50.856945Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2020-03-29T12:47:50.942730Z 0 [ERROR] InnoDB: mmap(139722752 bytes) failed; errno 12
2020-03-29T12:47:50.942773Z 0 [ERROR] InnoDB: Cannot allocate memory for the buffer pool
2020-03-29T12:47:50.942783Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2020-03-29T12:47:50.942797Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2020-03-29T12:47:50.942803Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2020-03-29T12:47:50.942811Z 0 [ERROR] Failed to initialize builtin plugins.
2020-03-29T12:47:50.942816Z 0 [ERROR] Aborting

查看内存,发现内存2G 应该是足够用的。再看 Swap,`total`为0

[root@4wei_cn log]# free -m
             total       used       free     shared    buffers     cached
Mem:          2005       1937         68        131          0        138
-/+ buffers/cache:       1799        206
Swap:            0          0          0

解决的办法是给小机加点内存,如果或者加点 Swap。加大内存可以在配置文件中上调innodb buffer.

[mysqld]
pid-file=/var/run/mysqld/mysqld.pid
log-error=/var/log/mysqld.log
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
thread_stack = 512k

character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
skip-character-set-client-handshake = true

innodb_buffer_pool_size=512M
max_connections=100
max_allowed_packet=10M

[client]
default-character-set=utf8mb4

[mysql]
default-character-set = utf8mb4

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

如果内存扩容比较复杂,或者考虑使用 Swap 也可以。

swapon -s
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
swapon -s
free -m

最后呢,添加开机启动挂载 swap,修改 `/etc/fstab` 添加行

/swapfile   swap   swap  defaults  0 0

发表评论