修改linux最大文件句柄數--ulimit -a
大家知道在linux服務器大並發調優時,往往需要預先調優linux參數,其中修改linux最大文件句柄數是最常修改的參數之一。
在linux中執行ulimit -a 即可查詢linux相關的參數,如下所示:
[root@mongodb1 ~]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
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
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 65535
* hard nofile 65535
修改以後保存,註銷當前用戶,重新登錄,執行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) 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
修改linux最大文件句柄數--ulimit -a