Search code examples
debianmqttmosquittopamulimit

Mosquitto increase max connections


I install mosquitto, and have changed a lot of setting in

/etc/security/limits.conf
/etc/sysctl.cnof
/etc/pam.d/common-session
/etc/pam.d/common-session-noninteractive

Detail changed:

$ 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

But after done this, mosquitto can still support 1024 connections.

So I use cat /etc/{pid}/limits to check the mosquitto's limits

Then I found that mosquitto's limit is still the default

Max processes             7968                 7968      processes
Max open files            1024                 4096      files

I figured out the mosquitto is nologin user, and cannot trigger the PAM limit configuration. And if I use prlimit command to increase the soft openfile limit, the mosquitto can indeed increase the max connections, but is limited by hard limit (4096).

Seems the debian still cannot do this (Reference), so a possible solution is to use /etc/init/mosquitto.conf

I do changed the /etc/init/mosquitto.conf but it seems not working

$ 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

Is anyone have any idea to make mosquitto support large amount of connections?

Thanks

PS. test tool: emqtt benchmark


Solution

  • After lots of study, I finally got an solution: change the init script of mosquitto, and change mosquitto user to 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)
    

    After changed the config, reload systemctl config and restart mosquitto

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

    Then the limits is changed!