Setup mqtt broker Mosquitto

Written: 16 Mar 2025 | 10:43 PM

Table of Contents

Open Table of Contents

Docker

Set up

Mosquitto MQTT broker will be hosted on docker container.

First prepare compose file:

services:
    mosquitto-broker:
        image: eclipse-mosquitto:2.0.21
        ports:
            - "1883:1883"
        volumes:
            - ./mosquitto.conf:/mosquitto/config/mosquitto.conf
            - /etc/mosquitto/pwfile:/mosquitto/config/pwfile

On the host machine, create password file:

touch /etc/mosquitto/pwfile
chmod 604 /etc/mosquitto/pwfile

Also create config file mosquitto.conf:

allow_anonymous false
password_file /mosquitto/config/pwfile
listener 1883 0.0.0.0

Although allow anonymous is false, this still authorize any connections to broker.

Now start service docker compose up -d.

Goes into container and create a user:

docker exec -it <container_id> sh
mosquitto_passwd -c /mosquitto/config/pwfile <username_1>

mosquitto_passwd /mosquitto/config/pwfile <username_2>

This will prompt for password.

The -c option is for create new file and override any file on that location.

Test

On the client machine, test connection:

mosquitto_pub -h <broker-address> -u <username> -P <password> -t <topic> -m "Hi"

And voila!

Windows

Setup

  1. Download on offical website, the download includes mosquitto broker daemon, mosquitto pub/sub client binary, mosquitto password utility to manage credentials.

  2. Open mosquitto.conf, add following configs

allow_anonymous false
listener 1883 localhost
password_file C:\mosquitto\pwfile
  1. On Powershell/CMD, run
mosquitto_passwd -c C:\mosquitto\pwfile <username>

This will prompt for password, then, the file C:\mosquitto\pwfile will be created if not exist. It will have this format: username:password_hash.

  1. Restart daemon
net stop mosquitto
net start mosquitto
stop-service mosquitto
start-service mosquitto
get-service mosquitto

Then, we can use with localhost.