第5章 Mosquitto配置文件完全指南

第5章 配置文件完全指南

5.1 配置文件结构

mosquitto.conf
全局配置
监听器配置
认证配置
日志配置
存储配置
最大连接数
协议版本
持久化
端口设置
绑定地址
WebSocket
允许匿名
密码文件
ACL文件
日志类型
日志级别
日志位置
内存持久化
磁盘持久化
自动保存

5.2 基础配置项

最小配置示例

bash 复制代码
# /etc/mosquitto/mosquitto.conf

# 监听端口
listener 1883

# 允许匿名访问
allow_anonymous true

# 持久化
persistence true
persistence_location /var/lib/mosquitto/

# 日志
log_dest file /var/log/mosquitto/mosquitto.log
log_dest stdout

配置文件语法

配置行
参数 值
参数 值1 值2

注释

listener 1883
allow_anonymous true

这是注释

5.3 监听器配置

单监听器

bash 复制代码
# 标准MQTT端口
listener 1883
protocol mqtt
allow_anonymous true

多监听器

bash 复制代码
# MQTT端口
listener 1883
protocol mqtt
allow_anonymous false
password_file /etc/mosquitto/passwd

# MQTT over TLS
listener 8883
protocol mqtt
cafile /etc/mosquitto/ca.crt
certfile /etc/mosquitto/server.crt
keyfile /etc/mosquitto/server.key

# WebSocket
listener 9001
protocol websockets
allow_anonymous true

监听器配置流程

监听器9001 监听器8883 监听器1883 客户端 监听器9001 监听器8883 监听器1883 客户端 TCP连接 MQTT协议 MQTT+TLS WebSocket 连接1883 连接8883 连接9001

5.4 认证配置

匿名访问控制

bash 复制代码
# 允许匿名
allow_anonymous true

# 拒绝匿名
allow_anonymous false
password_file /etc/mosquitto/passwd

密码文件配置

bash 复制代码
# 创建密码文件
mosquitto_passwd -c /etc/mosquitto/passwd admin
mosquitto_passwd -b /etc/mosquitto/passwd user1 password123

# 配置文件
password_file /etc/mosquitto/passwd
allow_anonymous false

ACL访问控制

bash 复制代码
# ACL文件示例
# /etc/mosquitto/acl

# 用户admin可以读写所有主题
user admin
topic readwrite #

# 用户sensor只能读写传感器数据
user sensor
topic write sensor/+/data
topic read sensor/+/config

# 只读用户
user readonly
topic read #

# 模式匹配
pattern readwrite home/%u/#

5.5 日志配置

日志类型

日志类型
stdout
file
syslog
topic
控制台输出
文件输出
系统日志
主题输出

日志配置示例

bash 复制代码
# 多目标日志
log_dest stdout
log_dest file /var/log/mosquitto/mosquitto.log
log_dest syslog

# 日志级别
log_type error
log_type warning
log_type notice
log_type information
log_type debug

# 日志时间戳
log_timestamp true
log_timestamp_format %Y-%m-%dT%H:%M:%S

5.6 持久化配置

持久化选项
内存模式
磁盘模式
persistence false
重启丢失
persistence true
persistence_location
autosave_interval
var/lib/mosquitto
自动保存间隔

持久化配置

bash 复制代码
# 启用持久化
persistence true
persistence_location /var/lib/mosquitto/

# 自动保存间隔(秒)
autosave_interval 1800

# 持久化客户端
persistent_client_expiration 1d

5.7 资源限制

bash 复制代码
# 最大连接数
max_connections -1  # -1表示无限制
max_connections 1000

# 消息队列限制
max_queued_messages 1000

# 主题深度限制
max_inflight_messages 20

# 连接超时
# (默认5秒)
# keepalive超时检测倍数
max_keepalive 65535

5.8 完整配置示例

开发环境配置

bash 复制代码
# dev.conf
per_listener_settings true

# MQTT监听器
listener 1883
allow_anonymous true
log_dest stdout

# WebSocket监听器
listener 9001
protocol websockets
allow_anonymous true

生产环境配置

bash 复制代码
# production.conf

# 全局设置
per_listener_settings true
max_connections 10000

# MQTT监听器
listener 1883
allow_anonymous false
password_file /etc/mosquitto/passwd
acl_file /etc/mosquitto/acl
max_queued_messages 1000

# TLS监听器
listener 8883
protocol mqtt
cafile /etc/mosquitto/ca.crt
certfile /etc/mosquitto/server.crt
keyfile /etc/mosquitto/server.key
allow_anonymous false
password_file /etc/mosquitto/passwd
acl_file /etc/mosquitto/acl

# 持久化
persistence true
persistence_location /var/lib/mosquitto/
autosave_interval 3600
persistent_client_expiration 7d

# 日志
log_dest file /var/log/mosquitto/mosquitto.log
log_type error
log_type warning
log_type notice
log_timestamp true

5.9 配置验证

bash 复制代码
# 测试配置文件
mosquitto -c /etc/mosquitto/mosquitto.conf -t

# 输出:
# Configuration OK.

5.10 本章小结

掌握了mosquitto配置文件的各项参数,能够根据不同场景配置合适的Mosquitto服务。

相关推荐
罗西的思考8 分钟前
【轻量化复现大模型 / KIMI K3】训练模块梳理 & Muon P2P 优化器
人工智能·深度学习·机器学习
lilihuigz16 分钟前
权限配置:WordPress 7.2 Abilities API 权限、输入验证与 JSON 输出指南
人工智能
杨凯凡22 分钟前
IT日报|2026-11-20:欧盟 AI 网络攻击、Valkey 9.2、GitHub 企业迁移
人工智能·github
LabVIEW开发27 分钟前
LabVIEW 用 25 ns 精度捕捉赛车发动机共振
网络·labview·labview知识·labview功能·labview程序
鲁邦通物联网1 小时前
机器人梯控到站状态机设计:楼层匹配、事件时序与任务判断
人工智能·机器人·机器人梯控·agv梯控·非侵入式采集·机器人乘梯·机器人自主乘梯
静水深码1 小时前
文生图提示词的工程师写法:把玄学话术拆成结构化模板
人工智能·后端·代码规范
Seiran1 小时前
Claude Fable 5.1写技术方案,ZEEKEAI提效实录
人工智能
JonaHu1 小时前
GPT Image 2.6 多语言海报改版:保留商品细节,分离文案与图像验收
图像处理·人工智能·aigc
4cos902 小时前
pi学习笔记-BranchSummary
人工智能
wflynn2 小时前
GitHub 日榜趋势速报 | 2026-11-20
开源·github