nginx报错 too many open files in system
系统进不去了,用ssh连接服务器也非常慢,负载均衡显示后端连接异常,重启mysql数据库,发现经常重启,或者直接关机,访问页面也访问不到。
http://www.51testing.com/html/56/13956-209988.html
查看mysql日志文件没发现问题,
查看nginx.error的日志发现报错
018/04/23 16:44:42 [crit] 11182#0: accept4() failed (23: Too many open files in system)
2018/04/23 16:44:42 [crit] 11182#0: accept4() failed (23: Too many open files in system)
2018/04/23 16:44:47 [crit] 11185#0: accept4() failed (23: Too many open files in system)
2018/04/23 16:44:47 [crit] 11185#0: accept4() failed (23: Too many open files in system)
当系统访问高峰的时间用root执行脚本出现如下结果:
lsof -n | awk '{print $2}' | sort | uniq -c | sort -nr | more
365
365 4500
365 4423
365 2057
365 2056
365 2055
365 2054
365 2053
365 2052
365 2051
365 2050
365 2049
365 2048
365 2047
365 2046
365 2045
365 2044
365 2043
365 2042
365 2041
365 2040
365 2039
第一行是打开的文件的句柄数量,第二行为进程号.得到进程号后,可以通过ps命令得到进程的详细内容。
ps -ef | grep 7220
mysql 7220 423 465 15:09 ? /usr/sbin/mysqld
原来是mysql进程打开最多文件句柄数量。但是他目前只打开了131个文件句柄数量,远远底于系统默认值1024
但是如果系统并发特别大,尤其是某个进程,很有可能会超过1024。这时候就必须要调整系统参数,以适应应用变化
关闭某个进程。
综上所述:
是因为系统对打开的文件做了限制。
ulimit -n 查看当前用户的文件描述的数目
该数值是显示的是1024,意思是显示文件打开的数目限制为1024,可以让数字更大些,让网站的访问并发更高些。
修改ulimit限制数的方法:
.首先你得修改nginx.conf配置文件,在定义error.log日志路径的位置添加一行
vi nginx.conf
worker_rlimit_nofile 655350;
2.在/etc/bashrc文件最后面添加下面内容
ulimit -n 655350
3.在/etc/security/limits.conf文件最后面添加下面内容
* sofe nofile 655350
* sofe nofile 655350
*代表所有用户,如果想代表某个用户的话,则user sofe/hard nofile 65535
sofe代表软连接,hard代表硬限制
4.要使 limits.conf 文件配置生效,必须要确保 pam_limits.so 文件被加入到启动文件中
在/etc/bashrc后面加上ulimit -n 655350
或者 先查找下pam_limits.so的位置。
find / -name pam_limits.so
/lib64/security/pam_limits.so
vi /etc/pam.d/login
session required /lib64/security/pam_limits.so
若查看mysql文件报错:
101029 16:09:24 [ERROR] Error in accept: Too many open files
101029 16:17:20 [ERROR] Error in accept: Too many open files
101029 16:21:37 [ERROR] Error in accept: Too many open files
101029 16:25:53 [ERROR] Error in accept: Too many open files
101029 16:30:09 [ERROR] Error in accept: Too many open files
/proc/sys/fs/file-max
这是系统资源分配的最高档案数,设定值与内存大小有关,早期 ram 很贵的时代,这个值通常不会太大,所以 mysql 开的档案数如果太多,确实可能被这个值限制住,但是现在动辄数 G 的 memory,这个值在我的 linux 系统上都内定开到 20 万以上,因此问题不在这个值。 (若真想修改这个值,可以用 sysctl -w fs.file-max=##### 来修改,但系统重开后自动改回,可写入/etc/rc.local 开机执行)
cat /proc/sys/fs/file-max
sysctl -w fs.file-max=406914
fs.file-max=406914
cat /proc/sys/fs/file-max
vi /etcc/rc.local
sysctl -w fs.file-max=
开机重启时候加上该文件保证其生效
cat /etc/security/limits.conf |grep mysql
mysql soft nofile 24000
mysql hard nofile 32000
ulimit -n
ulimit 可以查看每个 shell 的使用资源大小,-n 参数在 man page 中是写 The maximum number of open file descriptors,换句话说,mysql 所处的 shell,真的能开的档案只是ulimit -n 的值,在我的系统上 ulimit -n 的值仅有 1024,所以就算 fs.file-max 有几十万,mysql shell 可以开的就是 1024 而已,这才是关键所在。
my.cnf 中的 table_open_cache,max_connections, open_files_limit 三个参数
table_open_cache,指 mysql 开启 table 的 cache file 数,一般 mysql 开一个 table就会开启 *.MYI 和 *.MYD 两个档,比方说我们用 phpMyAdmin 开一个有 100 个 tables 的 DB,mysql 会 cache 住 200 个files。 (default: 64)
open_files_limit: mysqld 开启的最高档案数。 (default: 0)
max_connections: 最高联机数。 (default: 100)
mysqld 在 open file 后会 cache 住,那它要开到多少个档案之后,才会去释放掉 cache 的档案?
那就得看 my.cnf 里面,table_cache, max_connections, open_files_limit 的值,
如果:open_files_limits 的值为 0,就看 table_cache 和max_connections 透过某个函数计算出来的值;
table_cache * 2 + max_connections
如果 open_files_limits 的值不为 0,那应该是要看这个值的大小设定。
不管我们要将我们的 mysql 设置 open_files_limit 或是使用 table_cache * 2 + max_connections,都应该要注意ulimit -n 的值才是正解,跟 fs.file-max关联反而较小了。
要查看 mysql 开启的 files 数,可先用 ps aux| grep mysql 看 mysql PID,再利用 lsof -p PID# | wc -l 来统计。
nginx报错 too many open files in system的更多相关文章
- Nginx报错: "Too many open files accept" 和 "could not build the server_names_hash"
一.访问Nginx时,报错:"accept() failed (24: Too many open files)"原因时:nginx的连接数超过了系统设定的最大值造成的. 处理办法 ...
- nginx 报错 upstream timed out (110: Connection timed out)解决方案【转】
转自 nginx 报错 upstream timed out (110: Connection timed out)解决方案 - 为程序员服务http://outofmemory.cn/code-sn ...
- nginx报错:./configure: error: C compiler cc is not found, gcc 是已经安装了的
源码安装nginx报错,找不到gcc,但是实际上gcc是存在的,如下: # ./configure checking for OS + Linux -.el7.x86_64 x86_64 checki ...
- nginx报错zero size shared memory zone one
为了限速,在虚拟主机中加上了一个参数:limit_conn one 1:结果导致重启nginx报错: zero size shared memory zone "one"解决办法是 ...
- nginx报错:403 Forbidden 并且访问首页index.php是下载文件的状态
nginx报错:403 Forbidden 并且访问首页index.php是下载文件的状态,不能正常解析php 系统有其他两个站访问是正常的 看日志没有看到明显的错误 搜索了下: 答案如下: php的 ...
- Centos下yum安装Nginx报错 No package nginx available.
在Centos6下使用yum安装Nginx报错 解决方案: yum install epel-release
- 遇到问题----mongodb-----mongorestore报错too many open files甚至mongo服务崩溃
之前运行mongorestore还原mongodb数据库一直都没问题,今天还原的时候 报错too many open files.而且mongo服务经常崩溃需要重启. 问题有两方面: 原因一 一个原因 ...
- 安装Nginx报错“Cannot retrieve metalink for repository: epel. Please verify its path and try again”
CentOS 6.5中通过yum安装nginx报错. 搜了一下,很多都是修改某个配置文件的.但是在StackOverFlow的某个问题下,有人回答说修改配置文件并不是一个好的方法,虽然我采用了这个人的 ...
- nginx报错:nginx: [emerg] unknown directive in /etc/nginx/conf.d/test.conf:4
nginx报错:nginx: [emerg] unknown directive in /etc/nginx/conf.d/test.conf:4 解决: 第四行出现了 tab 空格 , 换成正常的 ...
随机推荐
- scrapy安装遇到的Twisted问题
贴上大佬的博客地址:https://blog.csdn.net/a19990412/article/details/78849881 电脑一直在爆下面这一堆的信息 Command”c:\users\l ...
- 洛谷 P1902 刺杀大使
刺杀大使 一道并不难的二分题,竟让我交了上20次,诶,果然还是我太弱了. 看完题目就基本想到要怎么做了: 只需要对最小伤害代价进行二分即可,check()函数里用搜索判断是否可以到达最后一行,这里的c ...
- Android中的Selector的使用总结
Android中的Selector主要是用来改变ListView和Button控件等其他空的默认背景,其使用方法可以如下所示: 1.首先在res目录下drawable文件夹,新建一个comm_butt ...
- Yii2中多表关联查询(with、join、joinwith)
表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer (id customer_name) 订单表Order (id order_name custome ...
- 使用python模拟登陆百度
#!/usr/bin/python # -*- coding: utf- -*- """ Function: Used to demostrate how to use ...
- Fedora19 有关输入法的无法切换问题 和 终端的快捷设置问题
Fedora19 有关输入法的无法切换问题 和 终端的快捷设置问题 1.首先,要单击右上角的设置输入法的"区域与语言设置",要设置为“为每个窗口设置不同的输入源”. 还有,刚使用的 ...
- 关于用终端运行php来测试推送的问题
照网上的方法,合并好了证书的pem,密码也是对的,然后也写好了推送用的php文件,在终端里php这个文件,报错报错内容是:Warning: stream_socket_client(): SSL op ...
- numpy.random.randint
low.high.size三个参数.默认high是None,如果只有low,那范围就是[0,low).如果有high,范围就是[low,high). >>> np.random.ra ...
- 基于docker搭建wordpress博客网站平台
WordPress是使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站.也可以把 WordPress当作一个内容管理系统(CMS)来使用. WordPre ...
- js常用技巧汇总
将彻底屏蔽鼠标右键 oncontextmenu="window.event.returnvalue=false" <table border oncontextmenu=re ...