大家知道在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