Mosquitto增加最大连接数

时间:2016-10-23 03:14:34

标签: debian mqtt mosquitto pam ulimit

我安装了mosquitto,并在

中更改了很多设置
/etc/security/limits.conf
/etc/sysctl.cnof
/etc/pam.d/common-session
/etc/pam.d/common-session-noninteractive

细节已更改:

$ sudo vim /etc/sysctl.conf

fs.file-max=2097152
fs.nr_open=2097152
net.core.somaxconn=32768
net.ipv4.tcp_max_syn_backlog=16384
net.core.netdev_max_backlog=16384
net.ipv4.ip_local_port_range='1025 65535'
net.core.rmem_default=262144
net.core.wmem_default=262144
net.core.rmem_max=16777216
net.core.wmem_max=16777216
net.core.optmem_max=16777216
net.ipv4.tcp_rmem='1024 4096 16777216'
net.ipv4.tcp_wmem='1024 4096 16777216'
net.ipv4.tcp_max_tw_buckets=1048576
net.ipv4.tcp_fin_timeout=15

$ sudo vim /etc/security/limits.conf

*       hard    nofile  1048576
*       soft    nofile  1048576
*       hard    nproc   1048576
*       soft    nproc   1048676

$ sudo vim /etc/pam.d/common-session
$ sudo vim /etc/pam.d/common-session-noninteractive

session required pam_limits.so

但在完成此操作后,mosquitto仍然可以支持1024个连接。

所以我使用cat /etc/{pid}/limits来检查蚊子的极限

然后我发现mosquitto的限制仍然是默认值

Max processes             7968                 7968      processes
Max open files            1024                 4096      files

我发现mosquitto是nologin用户,并且无法触发PAM限制配置。如果我使用prlimit命令增加软打开文件限制,mosquitto确实可以增加最大连接数,但受硬限制(4096)的限制。

似乎debian仍然无法执行此操作(Reference),因此可能的解决方案是使用/etc/init/mosquitto.conf

我确实更改了/etc/init/mosquitto.conf,但似乎无效

$ sudo vim /etc/init/mosquitto.conf    
limit nofile 1048576 1048576
limit nproc  1048576 1048576
ulimit -n 1048576 # I do both command, but neither do they works
ulimit -u 1048576

有人有任何想法让蚊子支持大量的连接吗?

由于

PS。测试工具:emqtt benchmark

1 个答案:

答案 0 :(得分:0)

经过大量的研究,我终于得到了一个解决方案:更改mosquitto的init脚本,并将mosquitto用户更改为root

$ sudo vim /etc/mosquitto/mosquitto.conf

# change user to root
user root



$ sudo vim /etc/init.d/mosquitto

#! /bin/bash
# Change /bin/sh to /bin/bash for fixing "ulimit -u illegal option" error

set_ulimit () {
    ulimit -f unlimited
    ulimit -t unlimited
    ulimit -v unlimited
    ulimit -n 1048576
    ulimit -m unlimited
    ulimit -u 1048576
}

start)
    ...
    # Update ulimit config in start command
    set_ulimit
    ...
    ;;
  stop)

更改配置后,重新加载systemctl config并重启mosquitto

sudo systemctl daemon-reload
sudo service mosquitto stop
sudo service mosquitto start

然后限制改变了!