背景
有一个服务使用到了使用Java实现对线上数据库进行备份,是一个定时任务,一切准备就绪,等第二天起来看的时候,发现服务挂了。。。
日志提示信息
2021-09-13 03:00:00.002-[scheduling-1] ERROR com.personloger.utils.BackUpDataBaseManager [BackUpDataBaseManager.java:91] - An I/O exception occurs when the database is backed up | exception Cannot run program "mysqldump": error=24, Too many open files 2021-09-13 10:33:04.124-[http-nio-80-Acceptor] ERROR org.apache.tomcat.util.net.Acceptor [DirectJDKLog.java:175] - Socket accept failed java.io.IOException: Too many open files at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method) at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:422) at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:250) at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:574) at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:80) at org.apache.tomcat.util.net.Acceptor.run(Acceptor.java:106) at java.lang.Thread.run(Thread.java:748) 2021-09-13 10:33:04.174-[http-nio-80-Acceptor] ERROR org.apache.tomcat.util.net.Acceptor [DirectJDKLog.java:175] - Socket accept failed java.io.IOException: Too many open files at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method) at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:422) at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:250) at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:574) at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:80) at org.apache.tomcat.util.net.Acceptor.run(Acceptor.java:106) at java.lang.Thread.run(Thread.java:748) 2021-09-13 10:33:04.275-[http-nio-80-Acceptor] ERROR org.apache.tomcat.util.net.Acceptor [DirectJDKLog.java:175] - Socket accept failed java.io.IOException: Too many open files at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method) at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:422) at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:250) at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:574) at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:80) at org.apache.tomcat.util.net.Acceptor.run(Acceptor.java:106) at java.lang.Thread.run(Thread.java:748)
当我看见这个Too many open files就基本知道是哪里出现问题。
查看数据库的open-limit信息
SHOW VARIABLES LIKE '%open_files_limit%';
查看服务器open limit
cat /proc/sys/fs/file-max
开始配置
配置原则: 所有进程打开的文件描述符数量不能超过/proc/sys/fs/file-max。单个进程打开的文件描述符数不能超过user limit中nofile的soft limit。nofile的soft limit不能超过其hard limit。
首先修改 file-max
# 永久性设置 [root@localhost ~]# echo "fs.file-max = 数字" >> /etc/sysctl.conf
永久性设置shell中打开文件的数量
[root@localhost ~]# echo "ulimit -n 数字 " >> ~/.bashrc