MySQL Error--打开过多文件导致数据库无法连接

【此文抄自同事的邮件,当作笔记学习】

环境描述
Mysql 5.5.21
OS centos 5.8
zabbix agent 2.4.3


情况描述
现象数据库处于运行状态,但是无法创建新的连接,监控报警数据库无法连接,连接不上MySQL,一直处于等待状态。

Mysql的error日志报错:

[ERROR] /usr/sbin/mysqld: Can't find file: './wukong_customs/wukong_task_info.frm' (errno: 23)
[ERROR] Error in accept: Too many open files in system

Mysqld进程打开的文件句柄数:
lsof(list open files)是一个列出当前系统打开文件的工具。

[root@db11149 ~]# lsof -p 24504 | wc -l 
4805

没有超过MySQL设置的限值。

查看操作系统的日志:

localhost kernel: VFS: file-max limit 65536 reached
localhostnrpe[6665]: Network server accept failure (23: Too many open files in system)

日志显示操作系统的连接数已经达到了最大值65535了,但是服务器上运行的业务应用只有数据库服务,而mysql打开的文件句柄数不到5000个。因此推断是有哪个正在运行的服务打开的句柄数过多导致的。

显示shell中的资源限制:
ulimit命令修改的数值只对当前登录用户的目前使用环境有效(只对当前会话有效),系统重启或者用户退出后就会失效.

[root@db11149 ~]# ulimit -a
core file size (blocks, -c) 0
dataseg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 268288
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 65536
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 268288
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

对所有会话永久生效,修改limits.conf:

[root@db11149 ~]# cat /etc/security/limits.conf | tail -3
# End of file
* soft nofile 65536
* hard nofile 65536

这个当中的硬限制是实际的限制,而软限制,是warnning限制,只会做出warning.其实ulimit命令本身就有分软硬设置,加-H就是硬,加-S就是软
默认显示的是软限制,如果运行ulimit命令修改的时候没有加上的话,就是两个参数一起改变.

查看目前运行的所有进程打开的句柄数:

# fori in `ps -ef| egrep -v 'UID PID PPID' | awk '{print $2}'` ; do echo "pid=$i, open_files=`lsof -p $i | wc -l`" >>1 ;done
pid=27644, open_files=35
pid=27646, open_files=35
pid=27647, open_files=7614
pid=27648, open_files=7614
pid=27649, open_files=7666
pid=27650, open_files=7379
pid=27651, open_files=7479
pid=27652, open_files=7214
pid=27653, open_files=7916
pid=27654, open_files=7804

#for i in `ps -ef| egrep -v 'UID PID PPID' | awk '{print $2}'` ; do echo "open_files=`lsof -p $i | wc -l` &&& whole=`ps -ef | awk '{if($2=="'"$i"'"){print $0}}'`" ;done

查看打开句柄数最多的进程:

确定是zabbix进程打开的

[root@db11149 ~]# ps -ef | grep zabbix
root 27644 1 0 Jul27 ? 00:00:00 zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
root 27646 27644 0 Jul27 ? 00:00:00 zabbix_agentd: collector [idle 1 sec] 
root 27647 27644 0 Jul27 ? 00:02:06 zabbix_agentd: listener #1 [waiting for connection]
root 27648 27644 0 Jul27 ? 00:02:06 zabbix_agentd: listener #2 [waiting for connection]
root 27649 27644 0 Jul27 ? 00:02:07 zabbix_agentd: listener #3 [waiting for connection]
root 27650 27644 0 Jul27 ? 00:02:04 zabbix_agentd: listener #4 [waiting for connection]
root 27651 27644 0 Jul27 ? 00:02:06 zabbix_agentd: listener #5 [waiting for connection]
root 27652 27644 0 Jul27 ? 00:01:59 zabbix_agentd: listener #6 [waiting for connection]
root 27653 27644 0 Jul27 ? 00:02:09 zabbix_agentd: listener #7 [waiting for connection]
root 27654 27644 0 Jul27 ? 00:02:07 zabbix_agentd: listener #8 [waiting for connection]


产生原因

重启zabbix agent后,打开的文件被释放。

https://support.zabbix.com/browse/ZBX-9251

由此确定是由于zabbix agent的异常导致的。

如何处理
查找zabbix的资料后确定是zabbix agent的BUG,目前在zabbix agent 2.4.3和2.4.4版本中发现了这个BUG,在2.2和2.5版本中已经修复了。
然后更新zabbix agent的版本。

原文地址:https://www.cnblogs.com/gaogao67/p/10726404.html