1. 程式人生 > 其它 >/etc/security/limits.conf 和/etc/systemd/system.conf 及/etc/systemd/user.conf的區別

/etc/security/limits.conf 和/etc/systemd/system.conf 及/etc/systemd/user.conf的區別

mysql max_connection

通常我們在設定某個程序可開啟最大檔案控制代碼數的時候都會去找/etc/security/limits.conf

這個檔案,在底部新增類似如下的設定

 *           soft   nofile       65535
 *           hard   nofile       65535
 *           soft   nproc        65535
 *           hard   nproc        65535

但是有時候你會發現設定並沒有生效,比如mysql服務,明明配置檔案設定了max_connections=5000或者其他數值,但是

進入mysql後,通過show variables like '%connections%';你會發現這裡的最大可開啟連線數還是受限

root@server 11:54:  [(none)]> 
root@server 11:54:  [(none)]> 
root@server 11:54:  [(none)]> show variables like '%connections%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| max_connections        | 214 |
| max_user_connections   | 0
| | mysqlx_max_connections | 100 | +------------------------+-------+ 3 rows in set (0.02 sec) root@server 11:54: [(none)]> exit Bye

同時你通過cat /proc/mysql程序id/limits會發現這裡的限制也依然是

[root@local-huajing /root]# cat /proc/1823/limits 
Limit                     Soft Limit           Hard Limit           Units     
Max cpu 
time unlimited unlimited seconds Max file size unlimited unlimited bytes Max data size unlimited unlimited bytes Max stack size 8388608 unlimited bytes Max core file size 0 unlimited bytes Max resident set unlimited unlimited bytes Max processes 65535 65535 processes Max open files 1024 4096 files Max locked memory 65536 65536 bytes Max address space unlimited unlimited bytes Max file locks unlimited unlimited locks Max pending signals 15505 15505 signals Max msgqueue size 819200 819200 bytes Max nice priority 0 0 Max realtime priority 0 0 Max realtime timeout unlimited unlimited us

你會發現mysql程序這裡分別是1024和4096,也並未被/etc/security/limits.conf 的設定影響。

仔細看/etc/security/limits.conf 檔案頂部的說明,你會發現有了相關說明,

[root@local-huajing /root]# cat /etc/security/limits.conf 
# /etc/security/limits.conf
#
#This file sets the resource limits for the users logged in via PAM.
#It does not affect resource limits of the system services.
#
#Also note that configuration files in /etc/security/limits.d directory,
#which are read in alphabetical order, override the settings in this
#file in case the domain is the same or more specific.
#That means for example that setting a limit for wildcard domain here
#can be overriden with a wildcard setting in a config file in the
#subdirectory, but a user specific setting here can be overriden only
#with a user specific setting in the subdirectory.
#
#Each line describes a limit for a user in the form:
#
#<domain>        <type>  <item>  <value>

可以發現第二行紅字說明,此設定對system services不生效,只對通過PAM登入的使用者生效,也就是說我們使用systemd管理的

服務程序是不受這裡影響的,那麼受什麼影響呢,就是/etc/systemd/system.conf 及/etc/systemd/user.conf檔案,有什麼區別呢

取決你systemd服務本身執行在什麼狀態,是系統例項還是使用者例項,通常情況下是執行在系統例項。如圖:就代表執行以系統例項狀態

在執行,那麼怎麼以使用者例項執行呢,需要把--system 換成--user即可。不過通常情況下都是系統例項狀態。

那麼回到剛才那2個配置檔案,以什麼狀態執行,你就去哪裡修改配置檔案內容即可,繼續回到字上面的mysql的問題,我的mysqld服務就是用systemd管理的,並在執行為系統例項

那麼我就需要修改etc/systemd/system.conf 裡面的最大可開啟檔案控制代碼數即可。如下圖:

取消前面的註釋,並設定為65535,並重啟服務。

重啟服務之後,你再去看mysql程序的可開啟最大檔案,你會發現均變為了65535,同時登入mysql檢視max_connection由原來的214也變為5000了。

當然網上也有直接在mysqld的service檔案裡在service段直接新增LimitNOFILE=65535配置解決,原理都是一樣的。二者皆可。

PS:如果你的mysql程序是通過普通的命令列啟動的,而不是systemctl,那麼是可以讀取/etc/security/limits.conf裡面的配置的。

可隨意轉載,歡迎署名!