一台小服务器,前几天出现持续地连接不上数据库的情况。
于是进到服务器,重启mysql, 过一会发现,依然连不上,mysql又崩了,查看mysql日志
[Note] InnoDB: PUNCH HOLE support available
[Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
[Note] InnoDB: Uses event mutexes
[Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
[Note] InnoDB: Compressed tables use zlib 1.2.11
[Note] InnoDB: Using Linux native AIO
[Note] InnoDB: Number of pools: 1
[Note] InnoDB: Using CPU crc32 instructions
[Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
[ERROR] InnoDB: mmap(137428992 bytes) failed; errno 12
[ERROR] InnoDB: Cannot allocate memory for the buffer pool
[ERROR] InnoDB: Plugin initialization aborted with error Generic error
[ERROR] Plugin ‘InnoDB’ init function returned error.
[ERROR] Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed.
[ERROR] Failed to initialize builtin plugins.
[ERROR] Aborting
[Note] Binlog end
[Note] Shutting down plugin ‘CSV’
[Note] /usr/sbin/mysqld: Shutdown complete
有问题找度娘,结果人家说是 mysql 没有使用swap
$ free
total used free shared buff/cache available
Mem: 1009152 243408 561672 24052 204072 599100
Swap: 0 0 0
好像还真是
于是给系统加上swap
$ sudo fallocate -l 1G /swapfile
$ ls -lh /swapfile
-rw-r–r– 1 root root 1.0G Aug 6 00:34 /swapfile
$ sudo chmod 600 /swapfile
$ ls -lh /swapfile
-rw——- 1 root root 1.0G Aug 6 00:34 /swapfile
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=acf7347a-7a96-443b-ae73-cf6ea2d6e7f2
$ sudo swapon /swapfile
$ sudo swapon –show
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 0B -2
$ free -h
total used free shared buff/cache available
Mem: 985M 240M 541M 23M 203M 582M
Swap: 1.0G 0B 1.0G
重启mysql,貌似有点作用,不过发现还是好慢好慢,访问在等待十几分钟后可以显示出网页,
top – 02:09:49 up 1:11, 2 users, load average: 0.23, 0.10, 0.02
Tasks: 117 total, 2 running, 80 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 1.0 sy, 0.0 ni, 98.7 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st
KiB Mem : 1009280 total, 301684 free, 399268 used, 308328 buff/cache
KiB Swap: 2097148 total, 1963260 free, 133888 used. 447072 avail Mem
$ free
total used free shared buff/cache available
Mem: 1009280 399296 301480 19832 308504 447044
Swap: 2097148 133888 1963260
看看 apache的配置,稍微调整一下,还是不起作用
<IfModule mpm_prefork_module>
StartServers 3
MinSpareServers 2
MaxSpareServers 5
#MaxRequestWorkers 150
MaxRequestWorkers 20
#MaxConnectionsPerChild 5000
MaxConnectionsPerChild 30
</IfModule>
应该是apache吃了太多的内存,导致mysql吃不到,然后mysql直接掀桌子不玩。
短时间内没辙了,还是云服务器加内存吧
原来服务器是活动时买的,1cpu 1G,想要升到2G内存,因为不加钱,居然不给升,发企鹅工单后,才知道只有产生了费用的才给升,于是升到4G。
升好内存后,发现世界原来如此美好,一切又美出天了
这世界归根结底还是人民币玩家厉害。
空下来研究一下apache的内存调优,或者切换到nginx上
参考:
ubuntu 18.04 设置swap 交换分区文件:https://blog.csdn.net/lhs960124/article/details/80446433