mosquitto配置
mosquitto.conf
mosquitto.conf是mosquitto的配置文件,
# Config file for mosquitto
# =================================================================
# General configuration
# =================================================================
per_listener_settings false
allow_zero_length_clientid false
check_retain_source false
max_inflight_messages 20
max_keepalive 60
persistent_client_expiration 1w
retain_available true
sys_interval 3600
listener 1883 0.0.0.0
# bind_address 127.0.0.1
# =================================================================
# Persistence
# =================================================================
persistence false
# =================================================================
# Security
# =================================================================
allow_anonymous false
password_file /etc/mosquitto/pwfile.example
acl_file /etc/mosquitto/aclfile.example
# =================================================================
# Debug configuration
# =================================================================
# log_dest file /var/log/mosquitto.log
# log_dest none
# log_type error
# log_type warning
# log_type notice
# log_type information
# connection_messages true
# log_timestamp true
# log_timestamp_format %Y-%m-%dT%H:%M:%S
# max_keepalive 5
# sys_interval 10
pwfile.example
设置用户名和密码,若客户端登录时,提供的用户名和密码不匹配,将无法连接mosquitto broker。
安装mosquitto时,附带安装了mosquitto_passwd,用户创建用户的可执行文件。
创建两个用户,分别是:admin和mosquitto
创建第一个用户admin:sudo mosquitto_passwd -c /etc/mosquitto/pwfile.example admin
创建第二个用户mosquitto:sudo mosquitto_passwd /etc/mosquitto/pwfile.example mosquitto
(注意:创建第二个用户时,不需要参数-c。若添加了参数-c,那么第一个用户会被第二个用户覆盖)
aclfile.example
可以限制指定用户对主题的发布或订阅权限。如果需要的主题没有在 aclfile.example 中定义,那么尽管MQTT Broker对不属于aclfile 文件定义的主题都忽略掉,导致其他MQTT Client接收不到主题消息。
例如:用户admin可以对 device/1001
发布,对 room/#
订阅。用户mosquitto可以对 #
发布和订阅。
user admin
topic write device/1001
topic read room/#
user mosquitto
topic readwrite #
注意:尽管在 aclfile 中对用户的主题进行了限制,并不是意味着MQTT Client就不能发布或者订阅该主题了。这些规则是对于MQTT Broker的约束,而不是对于MQTT Client的约束。例如:对于admin,device/1001主题可以被MQTT Broker接收到,并通知出去,其他订阅该主题的客户端可以接收到该主题消息。那么对于hello主题,在aclfile中没有定义,admin用户也是可以发布hello主题成功的,只是该主题消息到达MQTT Broker就会被丢弃,以至于订阅hello的其他客户端也收不到hello主题消息。