视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
MySQL报错:twomanyconnections/twomanyopenfiles的解决方法
2020-11-09 11:31:12 责编:小采
文档


mysql报错:[ERROR] Error in accept: Two many open files。而且这位朋友还说他并没有同时打开这么多文件,那么这是什么原因呢?

最近在论坛里面看到有几个朋友都发帖求助,原因是mysql报错:[ERROR] Error in accept: Two many open files。而且这位朋友还说他并没有同时打开这么多文件,那么这是什么原因呢?

首先,我们需要了解的是,在linux里面打开文件是以文件描述符(FD,file descripter)的形式打开的,每打开一次文件,,那么os就分配给你一个文件描述符,对于同一个文件如果有多个进程打开,那么就可以分配多个文件描述符。好了,现在我们回到这个问题上,这位朋友说他并没有一次性打开这么多的文件,怎么会出现这个问题呢?刚才说了,每个进程打开同一个文件都可能分配一个的FD,而mysql正是这么做的,每个session开打的数据文件的描述符都是的,而对于索引文件则是所有的session共享,
我先将手册里面的原话贴出来:
MySQL is multi-threaded, so there may be many clients issuing queries for a given table simultaneously. To minimize the problem with multiple client sessions having different states on the same table, the table is opened independently by each concurrent session. This uses additional memory but normally increases performance. WithMyISAMtables, one extra file descriptor is required for the data file for each client that has the table open. (By contrast, the index file descriptor is shared
between all sessions.)
好了,那你现在应该明白了,为什么并没有同时打开那么文件,但是却有那么多的文件描述符。弄清楚问题,那么现在就来解决这个问题。首先是增大table_open_cache,具体用法参考手册上说明,然后根本解决方法是在os上增大mysqld这个线程的最大开打文件数,在linux里面可以通过编辑文件 /etc/security/limits.conf然后里面的信息如下:
# /etc/security/limits.conf
#
#Each line describes a limit for a user in the form:
#
#
#
#Where:
# can be:
# - an user name
# - a group name, with @group syntax
# - the wildcard *, for default entry
# - the wildcard %, can be also used with %group syntax,
# for maxlogin limit
#
# can have the two values:
# - "soft" for enforcing the soft limits
# - "hard" for enforcing hard limits
#
# can be one of the following:
# - core - limits the core file size (KB)
# - data - max data size (KB)
# - fsize - maximum filesize (KB)
# - memlock - max locked-in-memory address space (KB)
# - nofile - max number of open files
# - rss - max resident set size (KB)
# - stack - max stack size (KB)
# - cpu - max CPU time (MIN)
# - nproc - max number of processes
# - as - address space limit
# - maxlogins - max number of logins for this user
# - maxsyslogins - max number of logins on the system
# - priority - the priority to run user process with
# - locks - max number of file locks the user can hold
# - sigpending - max number of pending signals
# - msgqueue - max memory used by POSIX message queues (bytes)
# - nice - max nice priority allowed to raise to
# - rtprio - max realtime priority
#
#
#

这个里面可以对进程需要的很多资源进行配置,比如说我们现在需要就是onfile这个选项open file而且我们有两种配置方式,第一是针对用户组的形式,第二具体用户的形式

比如有mysql用户

mysql soft nofile 131072

上面就了mysql用户最大的打开文件数。同时可以通过ulimit -a命令来查看这些信息。

同时需要注意的是,上面配置的用户级别打开FD的个数,系统级别的FD最大打开数可以通过 cat /proc/sys/fs/file-max查看,编辑有两种方式:

1. echo n > /proc/sys/fs/file-max

2. vi /etc/sysctl.conf 然后添加一行 fs.file-max=n 然后使用sysctl -p使其生效

但有时候,我们遇到的不是two many open files 而是 two many connections,这个就是并发的连接数,通过修改参数mysql的max_connections来解决,但是需要注意的是当这个参数增大的时候,同时可能会引发two many open files的问题,原因上面已经解释过了。自己并没有遇到过这个问题,只是简单模拟了一下,如果有错误,欢迎指正。

下载本文
显示全文
专题