For a small project (a microcontroller with a temperature sensor) I needed a mqtt broker to process the sensors data to display it in Grafana. There’s already a TIG stack running on my server, so it was obvious to me that I will go with Mosquitto.
For some reason Mosquitto never returned any data scraped by Telegraf. At this point I was really puzzled, as all tutorials I have found online had the same configuration and a similar setup as my stack. It took me quite some time to discover that Mosquitto introduced a breaking change in version 2, as it now connects by default only to the loopback interface, which explains the issue I saw: no connections from outside of the container are successful. All tutorials I have seen on the internet are from before version 2, so there was no mention of this newly introduced behaviour.
To change this behavior a single line needs to be added to the mosquitto.conf
: listener 1883
(or whatever port you’re using). After this change a connection from Telegraf is successful.
My entire mosquitto.conf looks like this after this change:
persistence true persistence_location /mosquitto/data/ log_dest file /mosquitto/log/mosquitto.log listener 1883 allow_anonymous true
This configuration file is mounted into the container by using a volume mount in a docker-compose file:
mosquitto: image: eclipse-mosquitto:2.0.7 container_name: mosquitto ports: - 9001:9001 - 1883:1883 volumes: - /media/enc/docker-volumes/mosquitto/conf/mosquitto.conf:/mosquitto/config/mosquitto.conf - /media/enc/docker-volumes/mosquitto/data/:/mosquitto/data/ - /media/enc/docker-volumes/mosquitto/log/:/mosquitto/log/
Telegraf is configured to subscribe to the mqtt topics by using the mqtt_consumer of inputs and outputting the data to influxdb:
[[outputs.influxdb]] urls = ["http://influxdb:8086"] database = "sensors" skip_database_creation = true [[inputs.mqtt_consumer]] servers = ["tcp://mosquitto:1883"] topics = [ "bedroom/#" ]
After this simple change Mosquitto accepts external (outside of the container) connections and the Telegraf is able to subscribe to the data and send it to InfluxDb.
Paul
Hell i thank you from the desperated bottom of my hearth!
I’m completely new in this thematic and i was struggling for a couple of days with this issue.
You saved my ass! Many thanks.
Tobey
I’m glad this helped you! 🙂
Frank
Is the docker compose file still part of the config file?
Tobey
I’m not sure I understand your question. The docker-compose file is a specific file in YAML for Docker to declare what to start. This file is not part of the mosquitto configuration, but references the mosquitto config file to mount it inside a container. You may leave the docker-compose part out and start your container with a regular Docker run command.