# Failed to connect zum Broker localhost

**URL:** https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628
**Category:** General
**Created:** [14 May 2022 19:16 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628 "2022-05-14T19:16:08Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![Rzut](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@Rzut](https://discourse.nodered.org/u/Rzut)
#### Post date: [14 May 2022 19:16 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628/1 "2022-05-14T19:16:08Z")

</div>

Hey, im new to raspberry Pi and i created my first mqtt flow but somehow its not working. i already searched the internet and this forum found users with the same problem but its still not woking. Can somebody help me?

```auto
Start Node-RED

Once Node-RED has started, point a browser at http://192.168.178.71:1880
On Pi Node-RED works better with the Firefox or Chrome browser

Use node-red-stop to stop Node-RED
Use node-red-start to start Node-RED again
Use node-red-log to view the recent log output
Use sudo systemctl enable nodered.service to autostart Node-RED at every boot
Use sudo systemctl disable nodered.service to disable autostart on boot

To find more nodes and example flows - go to http://flows.nodered.org

Starting as a systemd service.
14 May 21:12:55 - [info] [mqtt-broker:localhost] Verbindung zum Broker mqtt://localhost:1883 konnte nicht hergestellt werden

```

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [14 May 2022 20:28 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628/2 "2022-05-14T20:28:08Z")

</div>

What mqtt broker have you installed? - I'm guessing Mosquitto.  
Is it running - **systemctl status mosquitto**?  
How have you configured the broker - does /etc/mosquitto/mosquitto.conf contain "allow anonymous false"?  
How have you configured the MQTT nodes in Node-red?

---

<div class="post-metadata">

### Author: ![Rzut](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@Rzut](https://discourse.nodered.org/u/Rzut)
#### Post date: [14 May 2022 20:34 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628/3 "2022-05-14T20:34:57Z")

</div>

systemctl status mosquitto -\> is running  
How have you configured the broker - does /etc/mosquitto/mosquitto.conf contain "allow anonymous false"? -\> was on False i put it on true  
How have you configured the MQTT nodes in Node-red? -\> I dont know what you mean

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [14 May 2022 22:11 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628/4 "2022-05-14T22:11:37Z")

</div>

This is my /etc/mosquitto/mosquitto.conf. I don't have anything in the directory /etc/mosquitto/conf.d  
NB I generally use username/password for mosquitto, but just for this I changed to allow\_anonymous true.  
I had to **sudo systemctl restart mosquitto** and reload the Node-red browser page to get it connected.

```auto
# 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/syslog # enable logging stops mosquitto starting log file permissions

include_dir /etc/mosquitto/conf.d

allow_anonymous true

```

And here is a simple flow to show how the mqtt-in and out nodes are set up and test it.  
 ![Untitled 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/c/bca077351a3f14ea4855c8aa9d8731f52c247217.jpeg)

```auto
[{"id":"7ea899624d0e5932","type":"tab","label":"Flow 4","disabled":false,"info":"","env":[]},{"id":"74e2f2c11b1ec85d","type":"inject","z":"7ea899624d0e5932","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test","payload":"Testing","payloadType":"str","x":230,"y":120,"wires":[["a850a31137dc5273"]]},{"id":"a850a31137dc5273","type":"mqtt out","z":"7ea899624d0e5932","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"754230cc.99c44","x":450,"y":120,"wires":[]},{"id":"b7b07b01fa59328a","type":"mqtt in","z":"7ea899624d0e5932","name":"","topic":"test","qos":"2","datatype":"auto","broker":"754230cc.99c44","nl":false,"rap":true,"rh":0,"inputs":0,"x":250,"y":180,"wires":[["0518746599af4baf"]]},{"id":"0518746599af4baf","type":"debug","z":"7ea899624d0e5932","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":180,"wires":[]},{"id":"754230cc.99c44","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

```

Does this flow work for you?

---

<div class="post-metadata">

### Author: ![Rzut](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@Rzut](https://discourse.nodered.org/u/Rzut)
#### Post date: [16 May 2022 18:18 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628/5 "2022-05-16T18:18:13Z")

</div>

If i run **sudo systemctl restart mosquitto** i get an error: Job for mosquitto.service failed because the control process exited with error code.  
See "systemctl status mosquitto.service" and "journalctl -xe" for details.

Then i run this line: systemctl status mosquitto.service  
and get:

```auto
systemctl status mosquitto.service
● mosquitto.service - Mosquitto MQTT Broker
     Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Mon 2022-05-16 20:16:52 CEST; 22s ago
       Docs: man:mosquitto.conf(5)
             man:mosquitto(8)
    Process: 9070 ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto (code=exited, status=0/SUCCESS)
    Process: 9071 ExecStartPre=/bin/chown mosquitto /var/log/mosquitto (code=exited, status=0/SUCCESS)
    Process: 9072 ExecStartPre=/bin/mkdir -m 740 -p /run/mosquitto (code=exited, status=0/SUCCESS)
    Process: 9073 ExecStartPre=/bin/chown mosquitto /run/mosquitto (code=exited, status=0/SUCCESS)
    Process: 9074 ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf (code=exited, status=1/FAILURE)
   Main PID: 9074 (code=exited, status=1/FAILURE)
        CPU: 90ms

May 16 20:16:52 raspberrypi systemd[1]: mosquitto.service: Scheduled restart job, restart counter is at 5.
May 16 20:16:52 raspberrypi systemd[1]: Stopped Mosquitto MQTT Broker.
May 16 20:16:52 raspberrypi systemd[1]: mosquitto.service: Start request repeated too quickly.
May 16 20:16:52 raspberrypi systemd[1]: mosquitto.service: Failed with result 'exit-code'.
May 16 20:16:52 raspberrypi systemd[1]: Failed to start Mosquitto MQTT Broker.

```

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [16 May 2022 18:25 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628/6 "2022-05-16T18:25:57Z")

</div>

> [@Rzut](#):
>
> `ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf (code=exited, status=1/FAILURE)`

So it seems that you have a problem with your config file. May be you check with @jbudds content of the config file. If you want to customize mosquitto you should create an extra file and place it in the /etc/mosquitto/conf.d directory.

---

<div class="post-metadata">

### Author: ![Rzut](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@Rzut](https://discourse.nodered.org/u/Rzut)
#### Post date: [18 May 2022 18:43 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628/7 "2022-05-18T18:43:40Z")

</div>

I have the exact same conf as @jbudd but its still not working.  
This command has no error massage anymore: **sudo systemctl restart mosquitto**  
But i still get Connection failed to broker: mqtt://localhost:1883

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [18 May 2022 20:02 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628/8 "2022-05-18T20:02:43Z")

</div>

> [@jbudd](#):
>
> ```auto
> [{"id":"7ea899624d0e5932","type":"tab","label":"Flow 4","disabled":false,"info":"","env":[]},{"id":"74e2f2c11b1ec85d","type":"inject","z":"7ea899624d0e5932","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test","payload":"Testing","payloadType":"str","x":230,"y":120,"wires":[["a850a31137dc5273"]]},{"id":"a850a31137dc5273","type":"mqtt out","z":"7ea899624d0e5932","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"754230cc.99c44","x":450,"y":120,"wires":[]},{"id":"b7b07b01fa59328a","type":"mqtt in","z":"7ea899624d0e5932","name":"","topic":"test","qos":"2","datatype":"auto","broker":"754230cc.99c44","nl":false,"rap":true,"rh":0,"inputs":0,"x":250,"y":180,"wires":[["0518746599af4baf"]]},{"id":"0518746599af4baf","type":"debug","z":"7ea899624d0e5932","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":180,"wires":[]},{"id":"754230cc.99c44","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]
> 
> ```
> 
> Does this flow work for you?

No answer so far

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [18 May 2022 20:09 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628/9 "2022-05-18T20:09:02Z")

</div>

Then you should have a look if you have an additional conf file under conf.d of your /etc/mosquitto/directory or something else is running on port 1883 and you may be you need a config file.

In addition to the mosquitto.conf - i have a separate conf file (i.e. my.conf) in /etc/mosquitto/conf.d with the following content

```auto
allow_anonymous false
password_file /etc/mosquitto/pwfile
# persistance true
# persistance_file mosquitto.db
# persistence_location /data/mosquitto

listener 1883

```

---

<div class="post-metadata">

### Author: ![Rzut](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@Rzut](https://discourse.nodered.org/u/Rzut)
#### Post date: [19 May 2022 18:39 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628/10 "2022-05-19T18:39:24Z")

</div>

No doesnt work -\> under the flow: connecting

---

<div class="post-metadata">

### Author: ![Rzut](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@Rzut](https://discourse.nodered.org/u/Rzut)
#### Post date: [19 May 2022 18:42 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628/11 "2022-05-19T18:42:19Z")

</div>

hmmn didnt find a second conf file under /etc/mosquitto/ or /etc/mosquitto/conf.d

is it possible to delete node red completely and start all over again? or is there an easier way

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [19 May 2022 19:01 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628/12 "2022-05-19T19:01:10Z")

</div>

Please show us how your MQTT broker is configured in Node-red, the "Connection" and "Security" tabs.

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [19 May 2022 19:07 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628/13 "2022-05-19T19:07:30Z")

</div>

You should one create - with the content I posted. In my opinion Node Red is here not in the game when the mosquitto service wont start.

---

<div class="post-metadata">

### Author: ![Rzut](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@Rzut](https://discourse.nodered.org/u/Rzut)
#### Post date: [20 May 2022 19:51 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628/14 "2022-05-20T19:51:29Z")

</div>

I got it i guess: The problem was that in the Security there was an user and password  
So in node red it says connected and is green but the console still says  
20 May 21:50:14 - [info] [mqtt-broker:localhost] Connection failed to broker: mqtt://localhost:1883

and if i start with a new flow its start with  
20 May 21:46:13 - [info] [mqtt-broker:localhost] Connected to broker: mqtt://localhost:1883  
and then  
20 May 21:46:14 - [info] [mqtt-broker:localhost] Connection failed to broker: mqtt://localhost:1883

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [20 May 2022 21:53 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628/15 "2022-05-20T21:53:20Z")

</div>

I suspect you now have two mqtt configuration nodes. One of them connects ok, the other does not.

Delete the test flow I posted above, deploy and then search for unused configuration nodes.

If one shows up as unused, delete it and deploy again.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [19 July 2022 21:53 UTC](https://discourse.nodered.org/t/failed-to-connect-zum-broker-localhost/62628/16 "2022-07-19T21:53:27Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
