WordPress出现“建立数据库连接时出错”怎样解决

网站建设
浏览器访问网站突然出现“建立数据库连接时出错”,什么情况?于是进入服务器管理页,重启了服务器,网站正常了。
是什么原因因此“建立数据库连接时出错”?查看一下var/log/里面的各种日志,虽然不太会看。
mysqld.log日志文件
2019-01-09t13:24:04.615230z 0 [warning] timestamp with implicit default value is deprecated. please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2019-01-09t13:24:04.617096z 0 [note] /usr/sbin/mysqld (mysqld 5.7.**) starting as process 23937 ... 2019-01-09t13:24:04.620930z 0 [note] innodb: punch hole support available 2019-01-09t13:24:04.620954z 0 [note] innodb: mutexes and rw_locks use gcc atomic builtins 2019-01-09t13:24:04.620960z 0 [note] innodb: uses event mutexes 2019-01-09t13:24:04.620964z 0 [note] innodb: gcc builtin __atomic_thread_fence() is used for memory barrier 2019-01-09t13:24:04.620968z 0 [note] innodb: compressed tables use zlib 1.2.11 2019-01-09t13:24:04.620971z 0 [note] innodb: using linux native aio 2019-01-09t13:24:04.621268z 0 [note] innodb: number of pools: 1 2019-01-09t13:24:04.621377z 0 [note] innodb: using cpu crc32 instructions 2019-01-09t13:24:04.622890z 0 [note] innodb: initializing buffer pool, total size = 128m, instances = 1, chunk size = 128m 2019-01-09t13:24:04.622937z 0 [error] innodb: mmap(137428992 bytes) failed; errno 12 2019-01-09t13:24:04.622945z 0 [error] innodb: cannot allocate memory for the buffer pool 2019-01-09t13:24:04.622952z 0 [error] innodb: plugin initialization aborted with error generic error 2019-01-09t13:24:04.622959z 0 [error] plugin 'innodb' init function returned error. 2019-01-09t13:24:04.622963z 0 [error] plugin 'innodb' registration as a storage engine failed. 2019-01-09t13:24:04.622967z 0 [error] failed to initialize builtin plugins. 2019-01-09t13:24:04.622969z 0 [error] aborting 2019-01-09t13:24:04.622988z 0 [note] binlog end 2019-01-09t13:24:04.623035z 0 [note] shutting down plugin 'csv' 2019-01-09t13:24:04.623243z 0 [note] /usr/sbin/mysqld: shutdown complete上述只截取了mysqld.log部分error报错,上述日志重复出现了5次,mysql无法启动。网上搜索关于此现象,大概是内存不足引起mysql无法启动。
怎么解决此问题?环境为:centos7.3
配置为:1核1g
参考各种资料,总结两种方法
a、创建swap交换分区因为轻量应用服务器1核1g,搞不定mysql5.7这个耗资源大户。只能创建一个类似windows系统的虚拟内存,来解决内存不足引起的各种问题。
b、配置 php-fpm.conf 文件,控制php-fpm的子进程数可能是由于php-fpm子进程过多耗光了内存,造成mysql的进程被干掉了。
现在我只是实行了a计划,创建了swap交换分区,暂时还没再次遇到mysql被干掉。这需要服务器连续工作10天左右,才能验证问题是否得到解决。如果问题没有得到很好的解决,再来实行b计划。
a、创建swap交换分区首先查看当前内存用量?free -m?
[root@iz2zecnv***sp3z ~]# free -m total used free shared buff/cache available mem: 992 517 59 48 415 262 swap: 0 0 0可以看到swap没有容量,因此我们需要创建一个swap交换分区。
### 建立交换分区文件swapfile ### [root@iz2zecnv***sp3z ~]# dd if=/dev/zero of=/swapfile bs=1024 count=2048k ### bs=每块字节数1024k count=2048块 ### 2097152+0 records in 2097152+0 records out 2147483648 bytes (2.1 gb) copied, 15.3131 s, 140 mb/s将swapfile文件指定为交换文件
[root@iz2zecnv***sp3z ~]# mkswap /swapfile setting up swapspace version 1, size = 2097148 kib no label, uuid=0fa99a4c-05e8-4662-9011-286012868e08将swapfile映射为swap分区
[root@iz2zecnv***sp3z ~]# swapon /swapfile swapon: /swapfile: insecure permissions 0644, 0600 suggested. [root@iz2zecnv***sp3z ~]# chmod 600 /swapfile查看交换文件设置信息
[root@iz2zecnv***sp3z ~]# swapon -s filename type size used priority /swapfile file 2097148 0 -1再次查看当前内存用量?free -m
[root@iz2zecnv***sp3z ~]# free -m total used free shared buff/cache available mem: 992 498 103 48 390 265 swap: 2047 0 2047 创建swap分区成功后,设置swap分区开机自动加载。需要修改系统的fstab文件,vi /etc/fstab
# # /etc/fstab # created by anaconda on fri aug 18 03:51:14 2017 # # accessible filesystems, by reference, are maintained under '/dev/disk' # see man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # uuid=59d9ca7b-4f39-4c0c-9334-c56c182076b5 /