Installing Mosquitto MQTT Server
Sunday, April 13th, 2025
Recently came across a customer problem which needed access to a MQTT server. Here is how it went.
Requirement
The aim is to provide a cross platform way of providing a MQTT server for testing with the following characteristics:
It should be stressed that this is a disposable test environment running on a local network. The system will not be exposed to the Internet and so security and robustness are not going to be an issue.
- Cross platform, running on Mac and Raspberry Pi
- Persistence of data is not necessary as the system will be started and stopped as needed
- Running on a local network with no access to the Internet (reduced need for security)
- Simple to configure across platforms
Looking at the above it is apparent that a full installation should not be required. In fact it may be overkill. as would a dedicated server. In fact this use case points in the direction of a docker container with some shared configuration.
Installation and Configuration
Mosquitto is a lightweight MQTT server available as a native application for the target platforms and also as a docker image. Using docker will ensure a consistent deployment across multiple platforms and the Eclipse image will be the one used there.
The docker image can be installed with the command:
docker pull eclipse-mosquitto
The download only takes a few seconds with a moderate speed connection.
The Mosquitto client tools are also required for testing. These are installed with the following commands, for Raspberry Pi:
sudo apt update && sudo apt upgrade sudo apt install mosquitto-clients
and for Mac (assumes Homebrew is already installed, if not, Homebrew installation instructions can be found here):
brew install mosquitto
Note that on a Mac this installs both the client and the server components although we will only need the client applications for testing the system setup.
The Eclipse image page for the docker container describes a simple directory structure for configuration:
/mosquitto/config /mosquitto/data /mosquitto/log
This can be replicated by creating a local directory structure and then mapping this when we start the docker container. The local structure will look like this (note the missing / at the start of the directory names):
mosquitto/config mosquitto/data mosquitto/log
Time to run and test the server.
Testing
The client tools will allow the installation to be tested. The server will be installed on a Raspberry Pi with the name testserver500.local. The server is started by logging on to the Raspberry Pi and executing the following command:
docker run -it -p 1883:1883 -v "$PWD/mosquitto/config:/mosquitto/config" -v "$PWD/mosquitto/data:/mosquitto/data" -v "$PWD/mosquitto/log:/mosquitto/log" eclipse-mosquitto
This will start the downloaded image and register the config, data and log directories with the image.
First problem, we also need a configuration file (mosquitto.conf) in the mosquitto/config. A quick scan suggests that the following is all that is required:
persistence true persistence_location /mosquitto/data/ log_dest file /mosquitto/log/mosquitto.log
Two open shells are required for testing, one for receiving MQTT messages (subscription) and one for sending MQTT messages (publishing). In the first shell we subscribe to notifications with the command:
mosquitto_sub -h testserver500.local -t test/debug
Second problem, we get an error message Error: Connection refused. Some googling suggests that anonymous login is also required. The configuration file needs a couple more options adding to the file:
persistence true persistence_location /mosquitto/data/ log_dest file /mosquitto/log/mosquitto.log allow_anonymous true listener 1883 0.0.0.0
Stopping the docker container and then restarting it applies the changes. Subscribing again is successful.
Time to try and publish some data. In a third terminal enter the command:
mosquitto_pub -h testserver500.local -t test/debug -m "Hello, world" -d
The subscription terminal should now display the message just sent:
clusteruser@TestServer500:~ $ mosquitto_sub -h testserver500.local -t test/debug Hello, world
Success!
One final test, publish / subscribe from a remote machine.
Conclusion
Using a docker image makes it possible to use a standardised setup on any platform, including Windows (although this is not in scope here). The only additional set up required is to supply any specialised configuration and possibly data and log directories should data retention be required.
It should also be remembered that the configuration here is really only suitable for a low risk network (i.e. isolated testing systems) and should not be used in production.