修改linux最大文件句柄数
大家知道在linux服务器大并发调优时,往往需要预先调优linux参数,其中修改linux最大文件句柄数是最常修改的参数之一。
在linux中执行ulimit -a 即可查询linux相关的参数,如下所示:
[root@mongodb11 ~]# 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 查看修改的状态,如:
[root@mongodb11 ~]# ulimit -n 2048
[root@mongodb11 ~]# 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 65536
* hard nofile 65536
修改以后保存,注销当前用户,重新登录,执行ulimit -a ,ok ,参数生效了:
[root@localhost ~]# 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) 65536
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
-----------------------------------------------------------------------
* soft nofile 45535
* hard nofile 45535
* soft nproc 45535
* hard nproc 45535
root soft nofile 65535
root hard nofile 65535
root soft nproc 65535
root hard nproc 65535