Mosquitto 部署配置

  • ~6.50K 字
  1. 1. 一、安装
  2. 2. 二、创建目录/文件与权限
  3. 3. 三、创建密码文件(账号)
  4. 4. 四、创建 ACL(访问控制列表)
  5. 5. 五、Mosquitto 配置文件
  6. 6. 六、重启 Mosquitto
  7. 7. 七、测试访问
  8. 8. 七、(可选)启用 TLS 安全通信
    1. 8.1. 常见坑位提醒

一、安装

Ubuntu / Debian

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

CentOS / RedHat

sudo yum install epel-release -y
sudo yum install mosquitto mosquitto-clients -y
sudo systemctl enable mosquitto

安装完毕后,Mosquitto 默认运行在端口 1883

二、创建目录/文件与权限

  1. 目录与权限
sudo mkdir -p /data/mosquitto/data /data/mosquitto/log
sudo chown -R mosquitto:mosquitto /data/mosquitto
sudo chmod 700 /data/mosquitto/data
sudo chmod 750 /data/mosquitto/log
  1. 认证与 ACL 文件
sudo touch /etc/mosquitto/passwd /etc/mosquitto/acl
sudo chown mosquitto:mosquitto /etc/mosquitto/passwd /etc/mosquitto/acl
sudo chmod 640 /etc/mosquitto/passwd /etc/mosquitto/acl

三、创建密码文件(账号)

我们先创建一个账号文件来保存用户名和密码:

sudo mosquitto_passwd -c /etc/mosquitto/passwd user1
sudo mosquitto_passwd /etc/mosquitto/passwd user2
sudo mosquitto_passwd /etc/mosquitto/passwd user3

系统会提示你为每个用户输入密码。
(参数 -c 只在第一次创建时用,以后添加用户不要加 -c,否则会覆盖)

四、创建 ACL(访问控制列表)

创建 ACL 文件:

sudo nano /etc/mosquitto/acl

写入以下内容:

# 允许 user1 发布和订阅 topic/user 下的所有主题
user user1
topic readwrite topic/user/#

# 只允许 user2 订阅 topic/user 下的所有主题
user user2
topic read topic/user2/#

# 只允许 user3 发布 topic/user 下的所有主题
user user3
topic rwrite topic/user2/#

在 Mosquitto 的 ACL 文件里,readwritereadwrite 这三个关键字用于定义用户在主题上的访问方向

关键字 含义 示例 能否发布 能否订阅
read 只允许订阅(SUBSCRIBE) topic read topic/user/#
write 只允许发布(PUBLISH) topic write topic/user/#
readwrite 允许发布与订阅 topic readwrite topic/user/#

保存退出。

五、Mosquitto 配置文件

编辑主配置文件:

sudo nano /etc/mosquitto/mosquitto.conf

在文件末尾添加:

############################
# 基础与目录
############################
# 持久化(保存会话与消息队列)
persistence true
# 数据存储目录
persistence_location /data/mosquitto/data
# 持久化文件名(可区分环境)
persistence_file mosquitto.db

# 持久化保存策略
# 每 60s 自动保存持久化数据
autosave_interval 60
# 有变化即保存
autosave_on_changes true
# 离线会话保留时长(示例 2 周)
persistent_client_expiration 2w

############################
# 多监听器配置
############################
# 允许每个 listener 拥有独立配置(非常关键)
per_listener_settings true

############################################################
# Listener 1 - MQTT TCP 1883
############################################################
listener 1883 0.0.0.0

# 鉴权与访问控制(该端口生效)
# 禁止匿名连接
allow_anonymous false
# 用户名密码文件(用 mosquitto_passwd 维护)
password_file /etc/mosquitto/passwd
# ACL 访问控制规则文件
acl_file /etc/mosquitto/acl

# 网络优化
# 降低延迟(小包多时有利)
set_tcp_nodelay true

############################################################
# Listener 2 - WebSocket 8083
############################################################
listener 8083 0.0.0.0
# WebSocket 模式(用于浏览器客户端)
protocol websockets
# 同样启用鉴权
allow_anonymous false
password_file /etc/mosquitto/passwd
acl_file /etc/mosquitto/acl

# 若后续需要加密,可使用 8084 + TLS
#listener 8084
#protocol websockets
#allow_anonymous false
#password_file /etc/mosquitto/passwd
#acl_file /etc/mosquitto/acl
#cafile /etc/mosquitto/certs/ca.crt
#certfile /etc/mosquitto/certs/server.crt
#keyfile /etc/mosquitto/certs/server.key
#tls_version tlsv1.2

############################
# 性能与队列
############################
# 单客户端 QoS1/2 飞行窗口(默认20)
max_inflight_messages 50
# 最大消息体 2MB
max_packet_size 2097152
# 离线消息最大数量
max_queued_messages 1000
# 按需可限制字节(0 表示不限制)
max_queued_bytes 0
# 离线时不缓存 QoS0
queue_qos0_messages false

############################
# MQTT v5 相关(按需开启)
############################
#receive_maximum 100
#max_inflight_bytes 0

############################
# 监控与可观测性
############################
# $SYS 主题发布间隔(秒)
sys_interval 10

############################
# 日志配置
############################
# 日志输出位置
log_dest file /data/mosquitto/log/mosquitto.log
# 可选:同时输出到控制台或系统日志
#log_dest stdout
#log_dest syslog
log_timestamp true
log_timestamp_format %Y-%m-%d %H:%M:%S
# 常用日志级别:information / notice / debug
log_type information
# 连接/断开消息写入日志
connection_messages true

############################
# 结构化配置(可选)
############################
#include_dir /etc/mosquitto/conf.d

保存并退出。

配置检查

mosquitto -c /etc/mosquitto/mosquitto.conf -v

六、重启 Mosquitto

sudo systemctl daemon-reload
sudo systemctl restart mosquitto
sudo systemctl status mosquitto

七、测试访问

user1 发布消息:

mosquitto_pub -h localhost -p 端口 -u user1 -P user1_password -t topic/user1/temp -m "26.5"

user1 订阅自己的主题:

mosquitto_sub -h localhost -p 端口 -u user1 -P user1_password -t topic/user1/#

七、(可选)启用 TLS 安全通信

若需公网部署或更高安全性,可配置证书:

  1. 最简 TLS(服务器证书,端口 8883)
保留原有 1883  配置
############################################################
# Listener - MQTT over TLS 8883(仅服务器证书)
############################################################
listener 8883 0.0.0.0
allow_anonymous false
password_file /etc/mosquitto/passwd
acl_file /etc/mosquitto/aclfile

# 服务器证书与私钥(PEM)
# certfile 建议包含服务器证书 + 中间证书链(fullchain)
certfile /etc/mosquitto/certs/server.crt
keyfile  /etc/mosquitto/certs/server.key

# TLS 版本(建议至少 1.2)
tls_version tlsv1.2

要点:

  • certfile:服务器证书(建议附带中间证书链,很多人直接放 fullchain.pem)。
  • keyfile:与该证书匹配的私钥(600 权限,仅 mosquitto 可读)。
  • 客户端需信任签发该证书的 CA(如公网域名用 Let’s Encrypt,客户端通常已信任)。
  1. 双向 mTLS(可选,要求客户端证书)

只有安装了客户端证书的设备才能连;用户名/密码可选。

############################################################
# Listener - MQTT over TLS 8883(双向 mTLS)
############################################################
listener 8883 0.0.0.0
allow_anonymous false
password_file /etc/mosquitto/passwd
acl_file /etc/mosquitto/aclfile

# 服务器证书与私钥
certfile /etc/mosquitto/certs/server.crt
keyfile  /etc/mosquitto/certs/server.key
tls_version tlsv1.2

# 验证客户端证书
# cafile:用于验证“客户端证书”的 CA(不是服务器自己的证书)
cafile /etc/mosquitto/certs/ca.crt
require_certificate true

# 可选:将客户端证书的 Subject DN 或 CN 作为用户名用来匹配 ACL
# 例如 CN=device001 则可直接用 %u = device001 做隔离
use_identity_as_username true

要点:

  • cafile给 broker 验证“客户端证书”用的 CA;请不要与服务器证书链混淆。
  • 开启 use_identity_as_username true 后,可在 ACL 用 %u 按证书身份精准授权。
  1. 纯 TLS(无客户端证书)
# 连接并发布
mosquitto_pub -h your.domain.com -p 8883 \
  --cafile /etc/ssl/certs/ca-certificates.crt \
  -u admin -P 'yourpass' -t 'iot/test' -m 'hello over tls'
  1. mTLS
mosquitto_pub -h your.domain.com -p 8883 \
  --cafile /etc/mosquitto/certs/ca.crt \
  --cert /path/to/client.crt --key /path/to/client.key \
  -t 'iot/test' -m 'hello mTLS'
  1. wss(浏览器)
  • URL:wss://your.domain.com:8084/mqtt
  • 用 MQTT.js 连接时无需特配证书(浏览器已校验证书链)。

常见坑位提醒

  • certfile 建议用完整链(Let’s Encrypt 的 fullchain.pem),避免“中间证书缺失”导致部分客户端连接失败。
  • cafile 仅在需要验证客户端证书时配置;别把它当“给客户端的链”用。
  • 证书私钥权限要严格(600/640),否则 Mosquitto 可能拒绝读取。
  • 域名证书用于公网,客户端连接地址必须与证书 CN/SAN 匹配(不要用 IP 连域名证书)。
  • 升级到 2.x:使用 max_packet_size,不要再用 message_size_limit不要再配置 retry_interval