最近服务器并发很高,昨天晚上突然好几个服务同时挂了,查找日志发现报错:“too many open files”,在网上也找了些资料,最后通过修改句柄数,解决了问题。
大家知道在linux服务器大并发调优时,往往需要预先调优linux参数,其中修改linux最大文件句柄数是最常修改的参数之一。
在linux中执行ulimit -a 即可查询linux相关的参数,如下所示:
[[email protected] ~]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 256324
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
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) 256324
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
默认情况下,linux最大文件句柄数为1024个。当你的服务器在大并发达到极限时,就会报出“too many open files”。
那么如何修改linux最大文件句柄数呢?其实很简单:
1、ulimit -n 2048
这命令就可以修改linux最大文件句柄数,修改以后使用ulimit -a 查看修改的状态,如:
[[email protected] ~]# ulimit -n 2048
[[email protected] ~]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 256324
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 2048
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) 256324
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
但是,这种方法只针对当前进程有效。重新打开一个shell或者开启一个进程,你就会发现参数还是ulimit -n xx修改之前的数字。那么有没有一劳永逸的方法呢?
当然有!那就是修改系统参数。
2、修改linux系统参数。vi /etc/security/limits.conf 添加
* soft nofile 65535
* hard nofile 65535
修改以后保存,注销当前用户,重新登录,执行ulimit -a ,ok ,参数生效了:
[[email protected] ~]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 256600
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 65535
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) 256600
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
原文地址:http://blog.51cto.com/kouhao/2087604