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 空格 , 换成正常的 ...
随机推荐
- CString的GetBuffer和ReleaseBuffer
GetBuffer()主要作用是将字符串的缓冲区长度锁定,releaseBuffer则是解除锁定,使得CString对象在以后的代码中继续可以实现长度自适应增长的功能. CString ::GetBu ...
- 2017“编程之美”终章:AI之战勇者为王
编者按:8月15日,第六届微软“编程之美”挑战赛在选手的火热比拼中圆满落下帷幕.“编程之美”挑战赛是由微软主办,面向高校学生开展的大型编程比赛.自2012年起,微软每年都在革新比赛命题.紧跟时代潮流, ...
- Android学习总结(十八) ———— SQLite数据库使用
一.基本概念 数据库最经典的四个操作 添加.删除.修改.查找,在处理大量数据的时候使用数据库可以帮我们迅速定位当前须要处理的数据,举个例子 好比现在要实现一个搜索功能 用数据库的话只须要其中一个搜索条 ...
- (转)MyBatis框架的学习(六)——MyBatis整合Spring
http://blog.csdn.net/yerenyuan_pku/article/details/71904315 本文将手把手教你如何使用MyBatis整合Spring,这儿,我本人使用的MyB ...
- Educational Codeforces Round 12补题 经典题 再次爆零
发生了好多事情 再加上昨晚教育场的爆零 ..真的烦 题目链接 A题经典题 这个题我一开始推公式wa 其实一看到数据范围 就算遍历也OK 存在的问题进制错误 .. 思路不清晰 两个线段有交叉 并不是端点 ...
- python自动化基础问题解析
(1)自动化代码中用到的设计模式: po模式(page object): 1.PO提供了一种业务流程与页面元素操作分离的模式,这使得测试代码变得更加清晰. 2.页面对象与用例分离,使得我们更好的复 ...
- nyoj-1103-区域赛系列一多边形划分
http://acm.nyist.net/JudgeOnline/problem.php?pid=1103 区域赛系列一多边形划分 时间限制:1000 ms | 内存限制:65535 KB 难度: ...
- js获得本季度的开始日期 结束日期
var now = new Date(); //当前日期var nowMonth = now.getMonth()+1; //当前月var nowYear = now.getFullYear(); / ...
- (转发)IOS高级开发~Runtime(一)
IOS高级开发-Runtime(一) IOS高级开发-Runtime(二) IOS高级开发-Runtime(三) IOS高级开发-Runtime(四) 一些公用类: @interface Custom ...
- html5/css3响应式页面开发总结
一,自适应和响应式的区别 自适应是一套模板适应所有终端,但每种设备上看到的版式是一样的,俗称宽度自适应. 响应式一套模板适应所有终端,但每种设备看到的版式可以是不一样的. 虽然响应式/自适应网页设计会 ...