標籤:io os ar 使用 sp strong 檔案 on 問題
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 65536
* hard nofile 65536
修改以後儲存,登出目前使用者,重新登入,執行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) 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限制中所能設定的最大值。 soft限制指的是當前系統生效的設定值。 hard限制值可以被普通使用者降低。但是不能增加。 soft限制不能設定的比hard限制更高。 只有root使用者才能夠增加hard限制值。
可以最大設到:
655350
centos之Too many open files問題-修改linux最大檔案控制代碼數