/etc/security/limits.conf 和/etc/systemd/system.conf 及/etc/systemd/user.conf的区别

通常我们在设置某个进程可打开最大文件句柄数的时候都会去找/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里面的配置的。

 

posted @ 2021-10-09 14:36  忙碌在路上  阅读(1720)  评论(0编辑  收藏  举报