How to fix HomeAssistant [homeassistant.components.mqtt] Unable to connect to the MQTT broker: Connection Refused: not authorised.

Problem:

When starting up HomeAssistant, e.g. using docker-compose up, you see this error message:

homeassistant    | 2021-12-27 19:55:46 WARNING (Recorder) [homeassistant.components.recorder.util] The system could not validate that the sqlite3 database at //config/home-assistant_v2.db was shutdown cleanly                                                                                  
homeassistant    | 2021-12-27 19:55:47 ERROR (Thread-3) [homeassistant.components.mqtt] Unable to connect to the MQTT broker: Connection Refused: not authorised.                                                                                                                                 
homeassistant    | 2021-12-27 19:55:47 WARNING (Thread-3) [homeassistant.components.mqtt] Disconnected from MQTT server 127.0.0.1:1883 (5)
homeassistant    | 2021-12-27 19:55:48 ERROR (Thread-3) [homeassistant.components.mqtt] Unable to connect to the MQTT broker: Connection Refused: not authorised.                                                                                                                                 
h

Solution

Your configuration.yml does not have the correct username and/or password for your MQTT server.

This is an example section that works if the MQTT server has the correct user:

mqtt:
  broker: "127.0.0.1"
  username: "homeassistant"
  password: "ep2ooy8di3avohn1Ahm6eegheiResh"

Also check if the MQTT server such as Mosquitto has the correct user with the correct password.