前段时间真的是因为这个问题把我搞的头大了,大概每2-3天都会提示一次错误,全站无法打开,就只能看到一行提示:建立数据库连接时出错。每次都是重启服务器后又一切正常,刚开始还怀疑遭到了攻击,也没太在意服务器的配置问题。同时也一直没找到根本的解决办法,刚好,今天凌晨又发生了故障,遂下决心通宵达旦都要处理掉这个问题。
诊断过程
1,打开wordpress的debug模式,查看无法访问时的页面提示:无法正确的链接到数据库。而我的数据库配置参数一直是正确的,所以第一时间就去检查服务器的数据库服务,我用的是mysql,发现状态是inactive……
2,查看数据库错误的log文件/var/log/maridb/maridb.log
。根据故障发生时间找到了这样一段代码:
161003 3:45:07 [Warning] IP address '121.28.142.136' could not be resolved: Name or service not known 161003 3:45:07 [Warning] IP address '121.28.142.138' could not be resolved: Name or service not known 161003 3:45:08 [Warning] IP address '121.28.142.133' could not be resolved: Name or service not known 161003 7:05:41 [Warning] IP address '69.165.72.94' could not be resolved: Name or service not known 161003 11:37:43 [Warning] IP address '123.249.3.155' could not be resolved: Name or service not known 161003 12:28:12 [Warning] IP address '121.199.70.200' could not be resolved: Name or service not known 161004 0:10:16 [Warning] IP address '139.224.53.6' could not be resolved: Name or service not known 161004 0:19:28 [Warning] IP address '124.173.113.45' could not be resolved: Name or service not known 161004 00:38:31 mysqld_safe Number of processes running now: 0 161004 00:38:31 mysqld_safe mysqld restarted 161004 0:38:31 [Note] /usr/libexec/mysqld (mysqld 5.5.50-MariaDB) starting as process 27071 ... 161004 0:38:31 [Warning] Changed limits: max_open_files: 1024 max_connections: 214 table_cache: 400 161004 0:38:31 InnoDB: The InnoDB memory heap is disabled 161004 0:38:31 InnoDB: Mutexes and rw_locks use GCC atomic builtins 161004 0:38:31 InnoDB: Compressed tables use zlib 1.2.7 161004 0:38:31 InnoDB: Using Linux native AIO 161004 0:38:31 InnoDB: Initializing buffer pool, size = 128.0M InnoDB: mmap(137756672 bytes) failed; errno 12 161004 0:38:31 InnoDB: Completed initialization of buffer pool 161004 0:38:31 InnoDB: Fatal error: cannot allocate memory for the buffer pool 161004 0:38:31 [ERROR] Plugin 'InnoDB' init function returned error. 161004 0:38:31 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed. 161004 0:38:31 [ERROR] mysqld: Out of memory (Needed 128917504 bytes) 161004 0:38:31 [ERROR] mysqld: Out of memory (Needed 96681984 bytes) 161004 0:38:31 [ERROR] mysqld: Out of memory (Needed 72499200 bytes) 161004 0:38:33 [Note] Plugin 'FEEDBACK' is disabled. 161004 0:38:33 [ERROR] Unknown/unsupported storage engine: InnoDB 161004 0:38:33 [ERROR] Aborting 161004 0:38:33 [Note] /usr/libexec/mysqld: Shutdown complete 161004 00:38:33 mysqld_safe mysqld from pid file /var/run/mariadb/mariadb.pid ended
检查下来初步判断为内存溢出而导致数据库服务被回收掉,我使用的配置是2GB的ram,为什么还能内存溢出啊,mysql也吃不掉这么多内存吧。
3,检查内存使用状况free
,发现我的swap
分区是0,这下难怪了,原来是我没有做交换分区,导致内存过多没地方暂存才会进行回收。大致问题已经找到,那么可以开始处理了。
处理过程
想过如何解决这个内存的问题,无非两种,要么无脑的提升硬件配置,要么就是启用swap分区。当然最终我选择了性价比高的后者。
创建swap分区dd if=/dev/zero of=/swap.dat bs=1024 count=524288
,这里524288代表使用512MB的大小作为swap分区。
定义并使用swap分区,分别输入mkswap /swap.dat
,swapon /swap.dat
。
使用free -m
命令查看使用生效,如果swap右边又数字而不是0的话,说明已经生效了。
最后编辑/etc/fstab这个文件,在最后一行加入/swap.dat swap swap 0 0这样一行代码,使swap在开机的时候自动挂载。
至此,问题处理完毕了。再也不会有烦人的建立数据库连接时出错的错误了!