zoukankan      html  css  js  c++  java
  • ubuntu下Mosquitto安装及配置

    ubuntu下Mosquitto安装及配置

    本文最近一次修改日期:2020-03-04 1:43 周三

    Eclipse Mosquitto是一个开源消息代理,实现了MQTT协议版本3.1和3.1.1.Mosquitto轻量,适用于低功耗单板计算机到完整服务器的所有设备.

    Mosquitto项目还提供了用于实现MQTT客户端的C库以及非常受欢迎的mosquitto_pub和mosquitto_sub命令行MQTT客户端.

    本文抄自:飞虎兄的文章Mosquitto 搭建及配置
    本文参考:清明-心若淡定的文章订阅mosquitto服务器状态各主题

    所超代码,均已在ubuntu server 18.04 LTS上测试,
    订阅客户端数量 $SYS/broker/clients/active (1.4版本已取消 //经测试我用的1.6.8的客户端,这个命令并没有取消
    修改为$SYS/broker/clients/expired (当前连接的客户端数量) //这个命令什么也没返回,这是为什么?
    

    其他服务器代理实现:https://github.com/mqtt/mqtt.github.io/wiki/servers
    各操作系统安装指引:https://mosquitto.org/download/

    Mosquitto安装

    • 添加存储库
    sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
    
    • 更新软件包
    sudo apt-get update
    
    • 安装
    sudo apt-get install mosquitto -y
    
    • 安装命令行客户端
    sudo apt-get install mosquitto-clients -y
    

    配置

    sudo nano /etc/mosquitto/mosquitto.conf

    pid_file /var/run/mosquitto.pid
    
    # 消息持久存储
    persistence true
    persistence_location /var/lib/mosquitto/
    
    # 日志文件
    log_dest file /var/log/mosquitto/mosquitto.log
    
    # 其他配置
    include_dir /etc/mosquitto/conf.d
    
    # 禁止匿名访问
    allow_anonymous false
    
    # 认证配置
    password_file /etc/mosquitto/pwfile
    
    # 权限配置
    acl_file /etc/mosquitto/aclfile
    
    • 认证配置pwfile

    创建文件

    sudo touch /etc/mosquitto/pwfile
    
    • 开启服务开启
    sudo mosquitto_passwd /etc/mosquitto/pwfile bootloader #bootloader是我当前用户名
    
    • 权限配置aclfile
    sudo nano /etc/mosquitto/aclfile
    
    # user1只能发布以test为前缀的主题,订阅以$SYS开头的主题即系统主题
    user user1
    topic write test/#
    topic read $SYS/#
    
    # user2只能订阅以test为前缀的主题
    user user2
    topic read test/#
    

    启动服务端

    #-c:指定特定配置文件启动
    #-d:后台运行
    mosquitto -c /etc/mosquitto/mosquitto.conf -d
    

    测试

    发布使用mosquitto_pub命令,订阅使用mosquitto_sub命令.常用参数介绍:

    参数 描述
    -h 服务器主机,默认localhost
    -t 指定主题
    -u 用户名
    -P 密码
    -i 客户端id,唯一
    -m 发布的消息内容
    • 订阅
    mosquitto_sub -h localhost -t "test/#" -u user2 -P 123456 -i "client1"
    
    • 订阅系统主题
    # 订阅客户端存活连接数
    mosquitto_sub -h localhost -u user1 -P 123456 -i "client2" -t '$SYS/broker/clients/active'  #发布者 订阅 者都算存活连接
    
    • 发布
    mosquitto_pub -h localhost -t "test/abc" -u user1 -P 123456 -i "client3" -m "How are you?"
    

    其他

  • 相关阅读:
    Zabbix 单位换算
    Confluence6.9配置邮件服务器
    Linux内核基础优化
    Nginx跨域问题
    ssh远程登录过程中卡住
    postfix无法启动问题
    mysql-配置文件详解
    Mongodb-副本集部署
    Mongodb-安全配置优化
    Mongodb-简单部署
  • 原文地址:https://www.cnblogs.com/guyk/p/12405938.html
Copyright © 2011-2022 走看看