MQTT配置文件详解
以下是两者的详细配置说明。
一、Mosquitto 配置文件详解
Mosquitto 的配置文件通常位于 /etc/mosquitto/mosquitto.conf 。格式很简单:# 开头是注释,配置项以 变量名 值 的形式书写 。
1. 基础网络配置
这部分控制服务如何监听和接受连接。
| 配置项 | 说明 | 示例 |
|---|---|---|
port |
监听端口,默认 1883 | port 1883 |
bind_address |
绑定的 IP 地址,不设置则监听所有网卡 | bind_address 192.168.1.100 |
max_connections |
最大客户端连接数,-1 表示无限制 | max_connections 1024 |
protocol |
监听协议,可选 mqtt 或 websockets |
protocol mqtt |
listener |
定义多个监听器,格式:listener port bind_ip |
listener 1885 |
配置示例:
# 默认监听器
port 1883
bind_address 0.0.0.0
max_connections 10000
# 额外监听一个 WebSocket 端口
listener 8083 protocol websockets
2. 安全认证配置
| 配置项 | 说明 | 示例 |
|---|---|---|
allow_anonymous |
是否允许匿名连接,建议生产环境设为 false |
allow_anonymous false |
password_file |
用户名密码文件路径,需用 mosquitto_passwd 命令生成 |
password_file /etc/mosquitto/pwfile |
acl_file |
访问控制列表文件,控制订阅/发布权限 | acl_file /etc/mosquitto/aclfile |
allow_zero_length_clientid |
是否允许空客户端 ID,默认 true | allow_zero_length_clientid false |
密码文件生成命令:
# 创建密码文件并添加用户
mosquitto_passwd -c /etc/mosquitto/pwfile admin
# 追加更多用户
mosquitto_passwd /etc/mosquitto/pwfile user2
3. ACL 访问控制文件格式
acl_file 文件用于细粒度控制每个用户的主题权限 。
# 控制匿名用户权限(如果允许匿名)
topic read sensors/#
# 控制特定用户权限
user admin
topic readwrite #
user sensor_device
topic write sensors/+/data
# 使用 %u(用户名)和 %c(客户端ID)做动态匹配
pattern write sensors/%u/status
权限类型:
read:只允许订阅
write:只允许发布
readwrite:允许订阅和发布
deny:明确拒绝(优先级最高)
4. 持久化配置
用于将内存中的消息保存到磁盘,防止重启后丢失 。
| 配置项 | 说明 | 示例 |
|---|---|---|
persistence |
是否开启持久化,默认 false | persistence true |
persistence_location |
持久化文件存储目录 | persistence_location /var/lib/mosquitto/ |
autosave_interval |
自动保存间隔(秒),0 表示仅在退出时保存 | autosave_interval 1800 |
persistent_client_expiration |
持久会话客户端的过期时间 | persistent_client_expiration 7d |
5. 性能和限制配置
| 配置项 | 说明 | 示例 |
|---|---|---|
max_inflight_messages |
每个客户端同时进行中的 QoS 1/2 消息数 | max_inflight_messages 20 |
max_queued_messages |
每个客户端队列最大消息数 | max_queued_messages 1000 |
message_size_limit |
消息最大字节数,0 表示无限制 | message_size_limit 65536 |
retry_interval |
重发 QoS 消息的等待时间(秒) | retry_interval 20 |
6. 日志配置
| 配置项 | 说明 | 示例 |
|---|---|---|
log_dest |
日志输出目标:stderr、syslog、file |
log_dest file /var/log/mosquitto.log |
log_type |
日志类型:error、warning、debug 等 |
log_type error log_type websockets |
connection_messages |
是否记录连接/断开事件 | connection_messages true |
7. 完整配置示例
# ========== 基础配置 ==========
pid_file /var/run/mosquitto.pid
user mosquitto
# ========== 监听器 ==========
port 1883
bind_address 0.0.0.0
max_connections 10000
# MQTT over TLS
listener 8883
cafile /etc/mosquitto/certs/ca.crt
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key
require_certificate false
# ========== 安全认证 ==========
allow_anonymous false
password_file /etc/mosquitto/pwfile
acl_file /etc/mosquitto/aclfile
# ========== 持久化 ==========
persistence true
persistence_location /var/lib/mosquitto/
autosave_interval 1800
# ========== 性能限制 ==========
max_inflight_messages 50
max_queued_messages 1000
message_size_limit 65536
# ========== 日志 ==========
log_dest file /var/log/mosquitto.log
log_type error
log_type warning
connection_messages true
二、EMQX 配置文件详解
EMQX 从 5.0 版本开始采用 HOCON 格式(类似 JSON,但更简洁)。配置文件位于 etc/emqx.conf,也支持通过 Dashboard Web 界面在线修改热配置 。
1. 节点配置
node {
name = "emqx@127.0.0.1" # 节点名称
cookie = "emqxsecretcookie" # 集群通信 cookie
data_dir = "data" # 数据存储目录
}
2. 监听器配置
EMQX 支持多种协议,通过 listeners 配置块定义 。
listeners {
# TCP 监听器(MQTT)
tcp {
default {
bind = "0.0.0.0:1883"
max_connections = 1024000
max_conn_rate = 1000 # 每秒最大连接数
}
}
# SSL/TLS 监听器
ssl {
default {
bind = "0.0.0.0:8883"
ssl_options {
keyfile = "etc/certs/key.pem"
certfile = "etc/certs/cert.pem"
cacertfile = "etc/certs/ca.pem"
verify = verify_peer
}
}
}
# WebSocket 监听器
ws {
default {
bind = "0.0.0.0:8083"
}
}
}
3. MQTT 协议配置
mqtt {
max_packet_size = 1MB # 最大报文大小
max_clientid_len = 65535 # Client ID 最大长度
max_topic_levels = 128 # 主题最大层级数
max_qos_allowed = 2 # 允许的最大 QoS 等级
session_expiry_interval = 2h # 会话过期间隔
max_subscriptions = infinity # 最大订阅数
retain_available = true # 是否启用保留消息
max_retained_messages = infinity
}
4. 认证配置
# 匿名访问开关
allow_anonymous = false
# 配置认证器(通过 Dashboard 或 API)
authentication = [
{
mechanism = "password_based"
backend = "built_in_database"
user_id_type = "username"
}
]
支持的认证方式:
-
内置数据库:用户名/密码存储在 EMQX 内
-
MySQL/PostgreSQL:从关系数据库读取认证信息
-
Redis/MongoDB:从 NoSQL 数据库读取
-
HTTP:调用外部 HTTP 服务认证
-
JWT:JSON Web Token 认证
5. Dashboard 配置
dashboard {
listeners.http {
bind = 18083 # Web 管理端口
}
default_username = "admin"
default_password = "public" # 首次登录后请修改
}
6. 集群配置
cluster {
name = "emqx_cluster"
discovery_strategy = "manual" # 集群发现方式:manual/static/mcast/dns
}
配置集群(CLI 方式):
# 在节点2上执行,加入节点1的集群
./bin/emqx_ctl cluster join emqx1@192.168.0.10
# 查看集群状态
./bin/emqx_ctl cluster status
7. EMQX 常用端口一览
| 端口 | 协议 | 用途 |
|---|---|---|
| 1883 | TCP | MQTT 默认端口 |
| 8883 | TCP/SSL | MQTT over TLS |
| 8083 | WebSocket | MQTT over WebSocket |
| 8084 | WebSocket/SSL | MQTT over WSS |
| 8080 | HTTP | 管理 API 接口 |
| 18083 | HTTP | Dashboard 管理控制台 |
三、Mosquitto vs EMQX 配置对比
| 对比项 | Mosquitto | EMQX |
|---|---|---|
| 配置文件格式 | 简单 key-value | HOCON(层级结构) |
| Web 管理界面 | 无 | 有(端口 18083) |
| 热加载配置 | SIGHUP 信号重载部分配置 | Dashboard 在线修改,无需重启 |
| 多监听器支持 | 支持,通过 listener 指令 |
支持,通过 listeners 块 |
| 集群能力 | 需通过 Bridge 桥接 | 原生支持分布式集群 |
| 插件扩展 | 有限(需自己编译) | 丰富(认证桥接、规则引擎等) |
四、配置文件生效方法
Mosquitto
# 使用指定配置文件启动
mosquitto -c /etc/mosquitto/mosquitto.conf
# 作为服务启动
sudo systemctl start mosquitto
# 修改配置后重载(部分配置需重启)
sudo systemctl reload mosquitto # 发送 SIGHUP 信号
EMQX
# 启动 EMQX
./bin/emqx start
# 查看状态
./bin/emqx_ctl status
# 修改配置文件后重启
./bin/emqx restart
# 或通过 Dashboard 在线修改(无需重启)
# 访问 http://服务器IP:18083
五、快速配置建议
测试环境(Mosquitto):
port 1883
allow_anonymous true
log_type error
生产环境(EMQX):
allow_anonymous false
authentication = [ { mechanism = "password_based", backend = "built_in_database" } ]
listeners.tcp.default.max_connections = 10000

浙公网安备 33010602011771号