解决Ubuntu下Mosquitto连接客户端数量限制的方法

本文解决了在Mosquitto客户端连接数超过1024后出现了Client connection *** denied access by tcpd. 的错误。

问题背景

在测试mosquitto 2.0.11过程中,发现当我们的客户端连接数超过1000之后,再创建连接Mosquitto的日志就会出现 Client connection *** denied access by tcpd. 这样的错误,给我第一感觉就是tcp连接数产生了限制。
在网络上搜集了这个问题的解决方案,mosquitto的邮件列表上对这个问题有响应的描述和解决方案,但是其中所说的对/etc/init/mosquitto.conf 的编辑并无效果。(可能针对邮件列表所说的1.4版本有效)

基本可以确定,Ubuntu系统对打开文件是有限制数量的,我们修改以下应该可以解决问题。


所以按照网上的方案进行了修改,方案如下:

在Linux系统上,无论编写应用程序还是测试,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
在一些开发或测试过程中经常需要涉及到socket最大连接数(也即文件最大打开数)的设置修改,网上能搜索到一些资料,不过很多都不是很管用,本文总结了一下自己修改过程中实测有效的方法,希望能帮到有需要的朋友。

基本命令了解:

使用命令ulimit -a查看,其中open files就是最大连接数,一般情况下web服务器最大连接数的设置不能超过它,linux一般默认是1024

root@ubuntu:~# 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) 31498
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) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 31498
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

open files 部分就是打开文件数1024个(Linux下一切都是文件),一般这个太小了。也可以用ulimit -n查看

查看Linux系统级的最大打开文件数限制,使用如下命令:
root@ubuntu:~#cat /proc/sys/fs/file-max
810905

如何设置呢,官方是这样的:

第一步:配置/etc/security/limits.conf

sudo vim /etc/security/limits.conf
文件尾追加下面两行,添加完后保存

* hard nofile 40960
* soft nofile 40960

40960可以根据自己业务需要自行设置,四列参数的设置见英文,简单讲一下:
第一列,可以是用户,也可以是组,要用@group这样的语法,也可以是通配符如*%

第二列,两个值:hard,硬限制,soft,软件限制,一般来说soft要比hard小,hard是底线,决对不能超过,超过soft报警,直到hard数

第三列,见列表,打开文件数是nofile

第四列,数量,这个也不能设置太大

#
#Each line describes a limit for a user in the form:
#
#<domain>        <type>  <item>  <value>
#
#Where:
#<domain> can be:
#        - an user name
#        - a group name, with @group syntax
#        - the wildcard *, for default entry
#        - the wildcard %, can be also used with %group syntax,
#                 for maxlogin limit
#        - NOTE: group and wildcard limits are not applied to root.
#          To apply a limit to the root user, <domain> must be
#          the literal username root.
#
#<type> can have the two values:
#        - "soft" for enforcing the soft limits
#        - "hard" for enforcing hard limits
#
#<item> can be one of the following:
#        - core - limits the core file size (KB)
#        - data - max data size (KB)
#        - fsize - maximum filesize (KB)
#        - memlock - max locked-in-memory address space (KB)
#        - nofile - max number of open files
#        - rss - max resident set size (KB)
#        - stack - max stack size (KB)
#        - cpu - max CPU time (MIN)
#        - nproc - max number of processes
#        - as - address space limit (KB)
#        - maxlogins - max number of logins for this user
#        - maxsyslogins - max number of logins on the system
#        - priority - the priority to run user process with
#        - locks - max number of file locks the user can hold
#        - sigpending - max number of pending signals
#        - msgqueue - max memory used by POSIX message queues (bytes)
#        - nice - max nice priority allowed to raise to values: [-20, 19]
#        - rtprio - max realtime priority
#        - chroot - change root to directory (Debian-specific)
#
#<domain>      <type>  <item>         <value>
#


#*               soft    core            0
#root            hard    core            100000

第二步:/etc/pam.d/su(官方)或/etc/pam.d/common-session(网络)

sudo vim /etc/pam.d/su
将 pam_limits.so 这一行注释去掉 ,保存修改
重起系统
sudo vim /etc/pam.d/common-session
加上以下一行并保存
session required pam_limits.so
打开/etc/pam.d/su,发现是包含/etc/pam.d/common-session这个文件的,所以修改哪个文件都应该是可以的

这个觉得修改su这个文件比较好,取消注释就OK了,不容易出错,vim打开,定位,x一下即可

官方只介绍到第二步,就重启系统了,没有第三步,我这里实测好象并没有改变,感觉是不是全是第三步的作用?!

第三步:配置/etc/profile

最后一行加上

ulimit -SHn 40960

重启,ulimit -n 验证,期待显示结果40960就没问题了

然而,显示总是那么残酷,重启之后发现,设置仍然没生效。谷歌半天,特别提醒 ubuntu的pam_limits.so有个bug,就是不支持通配符,没办法,把前面的通配符*换成具体的用户名吧,root,test,nginx,web等等 。

root soft nofile 40960
root hard nofile 40960
root soft nproc 40960
root hard nproc 40960

这里特别提醒一下吧,这个坑真的很坑,花了近半天时间解决这个问题,希望这个经验可以给其他人带来帮助。

其他相关命令

查看系统所有ipv4的tcp连接

root@ubuntu:~# ss -at4
a:显示所有
-t:tcp
-4:ipv4

显示socket摘要
root@ubuntu:~# ss -s

查看当前有多少个TCP连接到当前服务器命令:

netstat -antp |grep -i est |wc -l
查看 TCP 8080 端口连接数

netstat -nat|grep -i "8080"|wc -l

关于netstat请参考博客
[https://blog.csdn.net/wade3015/article/details/90779669]

以上部分摘自不屈真实

补充手段

经过上述方法,如果使用service mosquitto start去启动仍然会限制1024个连接,但是我们可以手动启动

sudo mosquitto -c /etc/mosquitto/mosquitto.conf -d

参数意义:

(base) beiyang@dell-workstation:~$ mosquitto -h
mosquitto version 2.0.11

mosquitto is an MQTT v5.0/v3.1.1/v3.1 broker.

Usage: mosquitto [-c config_file] [-d] [-h] [-p port]

 -c : specify the broker config file.
 -d : put the broker into the background after starting.
 -h : display this help.
 -p : start the broker listening on the specified port.
      Not recommended in conjunction with the -c option.
 -v : verbose mode - enable all logging types. This overrides
      any logging options given in the config file.

See https://mosquitto.org/ for more information.

手动启动后,测试发现系统解除了对连接数的限制。

但是仍然不能确定为什么service mosquitto start无法解除限制,而手动加载配置文件是可以的。

posted @ 2021-07-08 10:44  牵着毛驴唱着歌  阅读(1842)  评论(1)    收藏  举报