eclipse / mosquitto

Eclipse Mosquitto - An open source MQTT broker
https://mosquitto.org
Other
8.92k stars 2.37k forks source link

Failed to start Mosquitto MQTT Broker. #1951

Open ghost opened 3 years ago

ghost commented 3 years ago

Hi,

i have updated Mosquitto MQTT Broker on my Ubuntu 20.04 and now it doesn't work. In fact of that i removed it completely and install it again. It works, but only until next restart.

Than i've added a custom.conf to /etc/mosquitto/conf.d

allow_anonymous true listener 1883

It works well untill i restarted the system.

I try to find a solution and start the server with

sudo /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

I'll get the message that the pid_file is unable to create. I must add the missing folder to /var/run/ and change the owner to

sudo chown mosquitto:mosquitto /var/run/mosquitto

Now the error message doesn't shown but the pid_file is not written and mosquitto doesn't start.

Than i try to do that: sudo /usr/sbin/mosquitto

The Result is that Error:

1607991918: mosquitto version 2.0.2 starting 1607991918: Using default config. 1607991918: Starting in local only mode. Connections will only be possible from clients running on this machine. 1607991918: Create a configuration file which defines a listener to allow remote access. 1607991918: Opening ipv4 listen socket on port 1883. 1607991918: Error: Address already in use 1607991918: Opening ipv6 listen socket on port 1883. 1607991918: Error: Address already in use

Anyway it will not start, but if i delete it completely and install it again it will run properly until the next restart. Could anybody help me, because my smart home wont work.

Best regards Chris

ralight commented 3 years ago

Chris, if you reinstalled fresh from the PPA then it should be already running through systemd and you won't need to start it manually. If not, then we can try and debug the problem.

Could you try running: /usr/sbin/mosquitto /etc/mosquitto/mosquitto.conf - that is the same command that systemd will use, so if there is an error we will see it.

carmenmoncada commented 3 years ago

Hello, I have the same problem, since I updated Mosquitto v2.0.3 on Ubuntu 16.04 the users can't connect to the broker.

When I tried to run with/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf it threw the following message. 2020-12-22|12:37:02: Error: Unable to write pid file.

I saw some people had the same problem, so I added pid_file /var/run/mosquitto/mosquitto.pid in mosquitto config but the problem did no go away.

In /lib/systemd/system/mosquitto.service I have this:

[Unit]
Description=Mosquitto MQTT Broker
Documentation=man:mosquitto.conf(5) man:mosquitto(8)
After=network.target
Wants=network.target

[Service]
Type=notify
NotifyAccess=main
ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto
ExecStartPre=/bin/chown mosquitto: /var/log/mosquitto
ExecStartPre=/bin/mkdir -m 740 -p /var/run/mosquitto
ExecStartPre=/bin/chown mosquitto: /var/run/mosquitto

[Install]
WantedBy=multi-user.target

The mosquitto.conf file is configured as:

# 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/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

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

include_dir /etc/mosquitto/conf.d

listener 1883

log_type error
log_type notice
log_type information
log_type debug
log_type all
log_dest topic 
log_type warning

allow_anonymous false
connection_messages true
max_keepalive 10
log_timestamp true
log_timestamp_format %Y-%m-%d|%H:%M:%S

password_file /etc/mosquitto/pwfile

####################################################
# MOSQUITTO LISTENER WITH SLL
####################################################

listener 8883
protocol mqtt
allow_anonymous false

#PARA BROKER EN PC
cafile /etc/mosquitto/certs/ca.crt
keyfile /etc/mosquitto/certs/server.key
certfile /etc/mosquitto/certs/server.crt

#PARA BROKER EN RASPBERRY PI 3
#cafile /etc/mosquitto/certs/ca_pi.crt
#keyfile /etc/mosquitto/certs/server_pi.key
#certfile /etc/mosquitto/certs/server_pi.crt

tls_version tlsv1.2

When I ran systemctl status mosquitto from the terminal, it showed me the following message...

● mosquitto.service - Mosquitto MQTT Broker
   Loaded: loaded (/etc/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: exit-code) since mar 2020-12-22 13:06:13 -04; 5s ago
     Docs: man:mosquitto(8)
           man:mosquitto.conf(5)
  Process: 22889 ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf (code=exited, status=1/FAILURE)
 Main PID: 22889 (code=exited, status=1/FAILURE)

dic 22 13:06:13 carmenmoncada systemd[1]: 

I don't know what is happening, could anybody help me with this? Thanks in advance Regards.

carmenmoncada commented 3 years ago

Guys I already found how to solve the issue.

I've managed to get it working too. I've manually created a directory /var/run/mosquittowith ownership to user mosquitto and then the folder and file were created automatically.

sudo mkdir -m 777 mosquitto

In mosquitto.conf I set pid_file /var/run/mosquitto/mosquitto.pid

Durobot commented 3 years ago

I've been struggling with the same PID file problem, but in the end, it was my mistake that was causing it. But I don't understand why you had to resort to sudo mkdir -m 777 mosquitto, as you had these lines in your mosquitto.service:

ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto ExecStartPre=/bin/chown mosquitto: /var/log/mosquitto ExecStartPre=/bin/mkdir -m 740 -p /var/run/mosquitto ExecStartPre=/bin/chown mosquitto: /var/run/mosquitto

Having fixed my (unrelated) mistake, I ran sudo rmdir /var/run/mosquitto, but mosquitto could still run, as these ExecStartPre commands recreated the directory.

WombatHollow commented 3 years ago

It sounds like you might be trying to start Mosquitto twice, the first time is a leftover from earlier versions that need deletion. The words below are from a post in another thread.

There was a root CRON job that runs launcher.shwhich contained sudo mosquitto -d

How did I find it. Well once I found CRON has a @reboot option I looked further and found CRON is by user including root. So I winged it and tried sudo crontab -l hoping the use of sudo finds root cronjobs instead of pi user jobs and there as a another job.

After that it was a tidy up of all the other things I had tweaked and re-enable the systemd job on reboot. sudo systemctl enable mosquitto.service

So now have 2.0.11 working as I like. I suspect that CRON job was a left over from earlier versions of Mosquitto (I was on 1.5 or 1.6) but the update to 2.0.11 didn't delete it when it created the mosquitto.service job.