wiki:MQTT

MQTT

Create 3 folders and 3 config files as following

[krit@mini MQTT]$ tree .
.
└── mosquitto
    ├── config
    │   ├── acls.conf
    │   ├── mosquitto.conf
    │   └── passwd
    ├── data
    └── log

4 directories, 3 files
[krit@mini MQTT]$ 

In mosquitto.conf, we put following config

[krit@mini MQTT]$ cat mosquitto/config/mosquitto.conf 
# following two lines required for > v2.0
#allow_anonymous true
listener 1883
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log

allow_anonymous false
password_file /mosquitto/config/passwd
acl_file /mosquitto/config/acls.conf

We create user 'alice' for Mqtt broker with command

[krit@mini MQTT]$ mosquitto_passwd ./mosquitto/config/passwd alice

With ACL (Access Control List), we allow user to pub/sub to specific topic by

[krit@mini MQTT]$ cat mosquitto/config/acls.conf 

user admin
topic #

user alice
topic readwrite cat/#

user bob
topic read  gw/+/status/#
topic write gw/+/cmd/#

Then, we can run mosquitto with following

[krit@mini MQTT]$ docker run -it --name mosquitto -p 1883:1883 -v $(pwd)/mosquitto:/mosquitto/ eclipse-mosquitto

If we need to add/del/modify any config files in passwd or acls.conf, we just restart the docker with

[krit@mini MQTT]$ docker restart mosquitto

Docker compose

The owner and permission of each file. These file acls.conf and mosquitto.conf

[root@Mosh Mosquitto]# tree .
.
├── docker-compose.yml
└── mosquitto
    ├── config
    │   ├── acls.conf
    │   ├── mosquitto.conf
    │   └── passwd
    ├── data
    │   └── mosquitto.db
    └── log
        └── mosquitto.log

4 directories, 6 files
[root@Mosh Mosquitto]# ls -l mosquitto/log/
total 8
-rwxr-xr-x 1 krit krit 5963 Aug 29 11:38 mosquitto.log
[root@Mosh Mosquitto]# ls -l mosquitto/data/
total 4
-rw------- 1 krit krit 47 Aug 29 11:36 mosquitto.db
[root@Mosh Mosquitto]# 

docker-compose.yml file should be following. Pls note user: "1000:1000" the number 1000 from command to check your id -u and id -g.

mosquitto:
    image: eclipse-mosquitto
    hostname: mosquitto
    container_name: mosquitto
    user: "1000:1000"
    ports:
      - 1883:1883
      - 9001:9001
    volumes:
      - ./mosquitto/config:/mosquitto/config
      - ./mosquitto/log:/mosquitto/log
      - ./mosquitto/data:/mosquitto/data

To start run

[krit@Mosh Mosquitto]$ docker-compose up -d

mosquitto_pub to reset null for retain any message on specific topic. -n=Send a null (zero length) message, -r=retain, -d=debug

mosquitto_pub -h hostname -t the/topic -u username -P password -n -r -d
Last modified 2 years ago Last modified on 07/31/22 16:29:50

Attachments (2)

Download all attachments as: .zip