mosquitto Error: Address already in use

Yes, I’m just wondering what starts it. I cannot see a second service at least. Or some internal issue makes it spawn multiple instances?

Currently difficult to have a closer look here, as my phone cannot open .deb files.

If you stop the service, is there still a Mosquitto instance running? systemctl stop mosquitto
In case you could mask it: systemctl mask mosquitto
If no other instance is up then, we know that it internally somehow spawns multiple instances.

we can try as well an older version of mosquitto. It’s not that difficult to remove 2.0.9 and install 2.0.8 or 2.0.7

Do you know the version used before?

I did a test installation on RPi 32bit as well as 64bit. Both are working fine. Strange. Could it be some config files invoke which will create a 2nd instance?

32bit

root@DietPi3:~# journalctl -u mosquitto
-- Logs begin at Sat 2021-03-20 22:41:40 CET, end at Sat 2021-03-20 22:42:45 CET. --
Mar 20 22:41:58 DietPi3 systemd[1]: Starting Mosquitto MQTT Broker...
Mar 20 22:41:58 DietPi3 systemd[1]: Started Mosquitto MQTT Broker.
root@DietPi3:~# cat /var/log/mosquitto/mosquitto.log
1616276518: mosquitto version 2.0.9 starting
1616276518: Config loaded from /etc/mosquitto/mosquitto.conf.
1616276518: Starting in local only mode. Connections will only be possible from clients running on this machine.
1616276518: Create a configuration file which defines a listener to allow remote access.
1616276518: Opening ipv4 listen socket on port 1883.
1616276518: Opening ipv6 listen socket on port 1883.
1616276518: Error: Cannot assign requested address
1616276518: mosquitto version 2.0.9 running
root@DietPi3:~#

64bit

root@DietPi4:~# journalctl -u mosquitto
-- Logs begin at Thu 2019-02-14 11:11:58 CET, end at Sat 2021-03-20 22:36:27 CET. --
Mar 20 22:35:40 DietPi4 systemd[1]: Starting Mosquitto MQTT v3.1/v3.1.1 Broker...
Mar 20 22:35:40 DietPi4 systemd[1]: Started Mosquitto MQTT v3.1/v3.1.1 Broker.
root@DietPi4:~# cat /var/log/mosquitto/mosquitto.log
1616276140: mosquitto version 1.5.7 starting
root@DietPi4:~#

Fun fact, 32bit will install 2.0.9 while 64bit is still 1.5.7 :slight_smile:

After systemctl stop mosquitto and checking mosquitto

I’m now not getting an error but still only in local mode.

root@DietPi:~# mosquitto
1616324408: mosquitto version 2.0.9 starting
1616324408: Using default config.
1616324408: Starting in local only mode. Connections will only be possible from clients running on this machine.
1616324408: Create a configuration file which defines a listener to allow remote access.
1616324408: Opening ipv4 listen socket on port 1883.
1616324408: Opening ipv6 listen socket on port 1883.
1616324408: mosquitto version 2.0.9 running
1616324409: New connection from 127.0.0.1:53278 on port 1883.
1616324409: New client connected from 127.0.0.1:53278 as mqtt_154a2846.3b1378 (p1, c1, k60).
1616324415: New connection from 127.0.0.1:53280 on port 1883.
1616324415: New client connected from 127.0.0.1:53280 as mqtt_7623cef0.cf7 (p1, c1, k60).

The local connection seems to be a security feature since version 2. You would need to configure authentication on your Mosquitto instance

https://mosquitto.org/documentation/authentication-methods/

Yes I saw that, but for now it does seem to be another instance running somewhere as I did a reboot checked mosquitto and I’m back to square one with the error.

Again systemctl stop mosquitto and there’s no error.

Would like to get to the bottom of this.

question. How do you check mosquitto. do you simply type the command into the console? Because this will not check mosquitto. This command will start a 2nd instance of mosquitto, which of course is conflicting with the systemd service

Better to check mosquitto as follow after system was rebooted.

systemctl status mosquitto.service
cat /var/log/mosquitto/mosquitto.log
ss -tulpn | grep mosquitto

Oops yes I was, that was unknown to me sorry. I’ve been chasing my tail :roll_eyes:

So it was just the update to vers. 2.0.9 local only thing blocking incoming messages, how embarrassing :roll_eyes:

Thanks for your help, time and patience.

I’ll edited the config file later and let you know.

Regards
Ian

root@DietPi:~# systemctl status mosquitto.service
● mosquitto.service - Mosquitto MQTT Broker
   Loaded: loaded (/lib/systemd/system/mosquitto.service; disabled; vendor preset: enabled)
   Active: active (running) since Sun 2021-03-21 12:54:04 CET; 17s ago
     Docs: man:mosquitto.conf(5)
           man:mosquitto(8)
  Process: 813 ExecStartPre=/bin/chown mosquitto: /var/run/mosquitto (code=exited, status=0/SUCCES
S)
  Process: 811 ExecStartPre=/bin/mkdir -m 740 -p /var/run/mosquitto (code=exited, status=0/SUCCESS
)
  Process: 808 ExecStartPre=/bin/chown mosquitto: /var/log/mosquitto (code=exited, status=0/SUCCES
S)
  Process: 805 ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto (code=exited, status=0/SUCCESS
)
 Main PID: 818 (mosquitto)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/mosquitto.service
           └─818 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Mar 21 12:54:03 DietPi systemd[1]: Starting Mosquitto MQTT Broker...
Mar 21 12:54:04 DietPi systemd[1]: Started Mosquitto MQTT Broker.
root@DietPi:~# cat /var/log/mosquitto/mosquitto.log
1616327644: mosquitto version 2.0.9 starting
1616327644: Config loaded from /etc/mosquitto/mosquitto.conf.
1616327644: Starting in local only mode. Connections will only be possible from clients running on this machine.
1616327644: Create a configuration file which defines a listener to allow remote access.
1616327644: Opening ipv4 listen socket on port 1883.
1616327644: Opening ipv6 listen socket on port 1883.
1616327644: mosquitto version 2.0.9 running
root@DietPi:~# ss -tulpn | grep mosquitto
tcp    LISTEN     0      100    127.0.0.1:1883                  *:*                   users:(("mosquitto",pid=818,fd=5))
tcp    LISTEN     0      100     ::1:1883                 :::*                   users:(("mosquitto",pid=818,fd=6))

I have it running again with the code below added to the mosquitto.conf file.

Sorry again for wasting your time, I’m still learning by doing.

listener 1883
allow_anonymous true

at least we found it :wink:

but…
https://dietpi.com/forum/t/7-0-2-upgrade-mosquitto-doesnt-appear-to-work/5122/2
still remains :frowning:

Well DietPi doesn’t do anything on that update. It is an apt package directly installed from mosquitto repository using apt package manager. Probably this is behavior of mosquitto v2 as security requirements changed. But better to check with mosquitto directly

after mosquitto upgrade:

DietPi-Services
─────────────────────────────────────────────────────
Mode: status mosquitto

[ INFO ] DietPi-Services | mosquitto inactive (dead)

with “dietpi-software reinstall 123” no luck also

pls share following

systemctl status mosquitto.service
cat /var/log/mosquitto/mosquitto.log
root@DietPi:/etc/mosquitto# systemctl status mosquitto.service
● mosquitto.service - Mosquitto MQTT Broker daemon
   Loaded: loaded (/etc/systemd/system/mosquitto.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

Mar 23 14:02:42 DietPi systemd[1]: /etc/systemd/system/mosquitto.service:11: PIDFile= referen
ces path below legacy directory /var/run/, updating /var/run/mosquitto.pid → /run/mosquitto.pid; please update the un
it file accordingly.
Mar 23 14:02:57 DietPi systemd[1]: /etc/systemd/system/mosquitto.service:11: PIDFile= referen
ces path below legacy directory /var/run/, updating /var/run/mosquitto.pid → /run/mosquitto.pid; please update the un
it file accordingly.
Mar 23 14:02:57 DietPi systemd[1]: Started Mosquitto MQTT Broker daemon.
Mar 23 14:02:57 DietPi systemd[1]: mosquitto.service: Succeeded.
Mar 23 14:03:38 DietPi systemd[1]: /etc/systemd/system/mosquitto.service:11: PIDFile= referen
ces path below legacy directory /var/run/, updating /var/run/mosquitto.pid → /run/mosquitto.pid; please update the un
it file accordingly.
Mar 23 14:06:03 DietPi systemd[1]: /etc/systemd/system/mosquitto.service:11: PIDFile= referen
ces path below legacy directory /var/run/, updating /var/run/mosquitto.pid → /run/mosquitto.pid; please update the un
it file accordingly.
Mar 23 14:06:14 DietPi systemd[1]: /etc/systemd/system/mosquitto.service:11: PIDFile= referen
ces path below legacy directory /var/run/, updating /var/run/mosquitto.pid → /run/mosquitto.pid; please update the un
it file accordingly.
Mar 23 14:06:14 DietPi systemd[1]: Started Mosquitto MQTT Broker daemon.
Mar 23 14:06:14 DietPi systemd[1]: mosquitto.service: Succeeded.
Mar 23 14:07:19 DietPi systemd[1]: /etc/systemd/system/mosquitto.service:11: PIDFile= referen
ces path below legacy directory /var/run/, updating /var/run/mosquitto.pid → /run/mosquitto.pid; please update the un
it file accordingly.

log file is empty :thinking:

can you share you config file pls

cat /etc/mosquitto/mosquitto.conf

Place your local configuration in /etc/mosquitto/conf.d/

A full description of the configuration file is at

/usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

allow_anonymous false
password_file /etc/mosquitto/users.passwd
listener 1883

pls can you able the line with pointing to the mosquitto.pid file. Should looks like this

#pid_file /var/run/mosquitto.pid
pid_file /run/mosquitto/mosquitto.pid

pls check if directory /run/mosquitto/ exist

and restart mosquitto afterwards.

that folder was not there, I created it, rebooted the system and…

[ INFO ] DietPi-Services | mosquitto inactive (dead) :face_with_raised_eyebrow: :cry:

That’s a tmpfs (RAMdisk), any files there are to live only for the lifetime the service runs. When the default configuration uses /run/mosquitto.pid that’s fine as no directory needs to be created. But the daemon does not have permissions to create a file there, which is probably the reason for the issue. Is the PID file really created by default there or did you add/set that setting manually? It would be basically a bug with their systemd unit as unprivileged users are never intended to create files there, so the systemd unit must handle this with root privileges. The PID file is actually not required with systemd, so the cleanest solution wound be to unset that setting.