Failed to connect zum Broker localhost

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?

Start Node-RED

Once Node-RED has started, point a browser at
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

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

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?

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

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.

# 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/

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

[{"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?

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:

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)
    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.

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.

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

No answer so far

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

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

listener 1883

No doesnt work -> under the flow: connecting

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

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

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.

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

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.