Install
sudo apt-get install mosquitto mosquitto-clients
sudo mosquitto_passwd -c /etc/mosquitto/passwd <mqtt_user> (set passwd here)
sudo vi /etc/mosquitto/conf.d/default.conf # new file with
allow_anonymous false # disable anonymous user
listener 1883 0.0.0.0 # open to all IP, open to localhost by default.
password_file /etc/mosquitto/passwd # make password work
sudo /etc/init.d/mosquitto restart
Test
mosquitto_sub -h localhost -u netro -P netrohome -t 'test'
mosquitto_pub -h localhost -u netro -P netrohome -t 'test' -m 'hello'
Increase max open file limit issue
The default max open file is 1024, which may not be enough for large MQTT service, increase it to 65535
# vi /etc/systemd/system.conf
DefaultLimitNOFILE=65536:524288
sudo systemctl daemon-reexec
sudo /etc/init.d/mosquitto restart
# cat /proc/<pid>/limits should be:
Max open files 65535 524288 files
# count the number of file opened by:
sudo ls /proc/<pid>/fd | wc -l