第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服务。

相关推荐
FIT2CLOUD飞致云28 分钟前
支持Hermes Agent与MongoDB管理,1Panel v2.1.9版本发布
ai·开源·1panel
人工智能AI技术31 分钟前
Python 断言 assert 基础用法
人工智能
我是发哥哈33 分钟前
横向评测:五款主流AI培训课程效果与选型分析
人工智能
GetcharZp1 小时前
告别昂贵显卡!llama.cpp 终极指南:在你的电脑上满速运行大模型!
人工智能
Cyeam1 小时前
Roadbook CSV:一行 CSV 秒变高德地图路书
后端·开源·aigc
AI木马人1 小时前
3.【Prompt工程实战】如何设计一个可复用的Prompt系统?(避免每次手写提示词)
linux·服务器·人工智能·深度学习·prompt
ch3nyuyu1 小时前
Ubuntu(乌班图)基础指令
linux·运维·网络
Agent产品评测局1 小时前
临床前同源性反应种属筛选:利用AI Agent加速筛选的实操方案 —— 2026企业级智能体选型与技术落地指南
人工智能·ai·chatgpt
ting94520002 小时前
HunyuanOCR 全方位深度解析
人工智能·架构
woai33642 小时前
AI通识-大模型的原理&应用
人工智能