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