给安装源增加存储库
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
# 更新安装源
sudo apt update 使用apt安装mosquiito
sudo apt-get install mosquitto启动重启和停止服务
sudo /etc/init.d/mosquitto restart/stop/start
#修改配置文件
sudo vim /etc/mosquitto/mosquitto.conf使用apt安装命令行客户端
sudo apt-get install mosquitto-clientsmosquitto_pub是推送消息,参数说明
使用示例
mosquitto_sub -h localhost -t "topic/#" -i "client1" -u bruce -P lan1532mosquitto_sub订阅主题,参数说明
使用示例
mosquitto_pub -h 172.18.205.245 -t "topic/test" -i "client3" -m "How are you?" -u bruce -P lan1532QOS消息服务质量,默认是0
拉取mosquitto的镜像,最新的版本是2.0.14,官方镜像
docker pull eclipse-mosquitto创建挂载目录及配置文件
bash mkdir -p /mosquitto/config touch /mosquitto/config/mosquitto.conf mkdir -p /mosquitto/data mkdir -p /mosquitto/log touch /mosquitto/pwdfile touch /mosquitto/aclfile
docker启动容器
docker run -itd --name mosquitto -p 1883:1883 -p 9001:9001 -v /mosquitto:/mosquitto eclipse-mosquittopid_file /var/run/mosquitto.pid
# 消息持久存储
persistence true
persistence_location /var/lib/mosquitto/
# 日志文件
log_dest file /var/log/mosquitto/mosquitto.log
#不记录
#log_type none
#########下面的debug、error、warning.....等等可以组合使用。
#记录网络通信包,通信包大小(含心跳包),但不显示内容
log_type debug
#错误信息(没见过)
log_type error
#警告信息(没见过)
log_type warning
#设备的订阅信息、发布信息及下线信息(端口、设备名、用户、不包发布内容)
log_type notice
#服务启动关闭信息、版本号、端口号、配置文件信息
log_type information
#所有设备订阅主题提醒
log_type subscribe
#这个没有试出来干啥用的(没见过)
#log_type unsubscribe
# 其他配置 这个docker配置中可以不要
include_dir /etc/mosquitto/conf.d
# 禁止匿名访问
allow_anonymous false
# 认证配置
password_file /mosquitto/pwdfile
# 权限配置
acl_file /mosquitto/aclfile
#如果需要外网可以访问,就必须指定mqtt协议
#MQTT协议
port 1883
protocol mqtt
# 设置最大连接数
max_connections -1
#websockets协议
listener 8000
protocol websockets
#如果需要查看websockets日志还可以加入以下面
log_type websockets
websockets_log_level 0
# 设置前缀
clientid_prefixes guduyl认证配置文件
#创建用户和密码
mosquitto_passwd -b /mosquitto/pwdfile username #然后输入及确认密码mosquitto_passwd参数说明:
权限配置文件
# 打开配置
vim /mosquitto/aclfile
# 李雷只能发布以test为前缀的主题,订阅以$SYS开头的主题即系统主题
user lilei
topic write test/#
topic read $SYS/#
# 韩梅梅只能订阅以test为前缀的主题
user hanmeimei
topic read test/#