Setup MQTT Add-On within Hassio Docker

I know there are plenty of threads relating to MQTT setup but most are outdated or doesn’t apply to my case. I use Hassio Docker Container with Mosquitto MQTT Broker add-on installed within the Home Assistant Interface.

My default MQTT configuration with nothing’s changed:

logins: []
anonymous: false
customize:
  active: false
  folder: mosquitto
certfile: fullchain.pem
keyfile: privkey.pem
require_certificate: false

My configuration.yaml under /usr/share/hassio/homeassistant/:

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

homeassistant:
  customize: !include customize.yaml

mqtt:
  broker: 192.168.10.10
  username: !secret mqtt_user
  password: !secret mqtt_password

I get the following error under Mosquitto logs:

1589686818: Socket error on client <unknown>, disconnecting.
1589686829: New connection from <ip_address> on port 1883.
1589686829: Socket error on client <unknown>, disconnecting.
1589686840: New connection from <ip_address> on port 1883.
1589686840: Socket error on client <unknown>, disconnecting.

However, if I add the username and password Mosquitto configuration UI, it started working.

logins:
  - username: mqtt_username
    password: mqtt_password
anonymous: false
customize:
  active: false
  folder: mosquitto
certfile: fullchain.pem
keyfile: privkey.pem
require_certificate: false

Why can’t I specified the mqtt username and password under configuration.yaml file? This used to work when I was running off the Raspberry Pi.

These are two separate things.
In configuration.yaml you specify the user homeassistant uses to connect to the broker.
In your add-on, you actually create the broker user.

You should not have anything in configuration.yaml for mqtt broker. It is not needed.
If you use a Home Assistant user in your MQTT devices you don’t need to configure a user in the broker either. I suggest creating a Home Assistant user using the mqtt_username and mqtt_password, removing mqtt from config yaml and restarting.

Thank you for the clarification on the difference between the two. I ended up doing what David suggested as it’s a single source to manage while leaving add-on as default.

What is the reason or benefit to create a specific User/Password for the Broker (and not use the one that we use to login on HA) ?

Beats me! As I said you don’t need a specific user. You can leave it blank and use a Home Assistant user in your broker setting on the device. That is what I do and recommend