0
0
Drone-programmingHow-ToBeginner · 4 min read

How to Set Up Mosquitto MQTT Broker Quickly and Easily

To set up a Mosquitto MQTT broker, install the Mosquitto package on your server, start the broker service, and configure it via the mosquitto.conf file if needed. Use sudo systemctl start mosquitto to run the broker and mosquitto_sub and mosquitto_pub commands to test it.
📐

Syntax

The basic commands to manage Mosquitto MQTT broker are:

  • sudo apt install mosquitto - installs the broker on Debian-based systems.
  • sudo systemctl start mosquitto - starts the Mosquitto service.
  • sudo systemctl enable mosquitto - enables the broker to start on boot.
  • mosquitto.conf - configuration file to customize broker settings.
  • mosquitto_sub and mosquitto_pub - command line clients to subscribe and publish messages.
bash
sudo apt install mosquitto
sudo systemctl start mosquitto
sudo systemctl enable mosquitto
# Configuration file location: /etc/mosquitto/mosquitto.conf
mosquitto_sub -t 'test/topic'
mosquitto_pub -t 'test/topic' -m 'Hello MQTT'
💻

Example

This example shows how to install Mosquitto, start the broker, and test message publishing and subscribing on the same machine.

bash
sudo apt update
sudo apt install -y mosquitto mosquitto-clients
sudo systemctl start mosquitto
sudo systemctl enable mosquitto

# Open a terminal and subscribe to a topic:
mosquitto_sub -t 'home/temperature' &

# In another terminal, publish a message:
mosquitto_pub -t 'home/temperature' -m '22.5 C'

# The subscriber terminal will show:
# 22.5 C
Output
22.5 C
⚠️

Common Pitfalls

Common mistakes when setting up Mosquitto include:

  • Not starting the Mosquitto service after installation.
  • Firewall blocking MQTT default port 1883.
  • Incorrect permissions on mosquitto.conf causing the broker to fail.
  • Trying to publish or subscribe without the broker running.

Always check the service status with sudo systemctl status mosquitto and ensure port 1883 is open.

bash
## Wrong: Trying to publish without broker running
mosquitto_pub -t 'test' -m 'Hello'
# Error: Connection refused

## Right: Start broker first
sudo systemctl start mosquitto
mosquitto_pub -t 'test' -m 'Hello'
# Message sent successfully
Output
Error: Connection refused Message sent successfully
📊

Quick Reference

CommandDescription
sudo apt install mosquittoInstall Mosquitto broker
sudo systemctl start mosquittoStart Mosquitto service
sudo systemctl enable mosquittoEnable Mosquitto to start on boot
mosquitto_sub -t Subscribe to MQTT topic
mosquitto_pub -t -m Publish message to topic
sudo systemctl status mosquittoCheck Mosquitto service status

Key Takeaways

Install Mosquitto using your system package manager before starting the broker.
Always start and enable the Mosquitto service to keep the broker running.
Use mosquitto_sub and mosquitto_pub commands to test MQTT messaging.
Check firewall settings to allow traffic on port 1883 for MQTT communication.
Edit /etc/mosquitto/mosquitto.conf carefully to customize broker behavior.