目录

0 MQTT协议

1 编译和安装

1.1 Download source code

1.2 安装

1.2.1 源码安装

1.2.2 二进制安装

1.3 启动运行

Systemd 启动

手动启动

2 配置

2.1安全配置

3 测试工具


Mosquitto是一个开源的C实现的MQTT服务器和客户端。本文介绍Mosquitto在ubuntu18.04系统上的编译,安装,配置和使用。

0 MQTT协议

MQTT协议已经到了5.0版本,每个版本的协议内容可以如下获取:

MQTT 3.1.1

中文版 http://mqtt.p2hp.com/mqtt311

英文版 http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html

MQTT 5.0

中文版 http://mqtt.p2hp.com/mqtt-5-0

英文版https://docs.oasis-open.org/mqtt/mqtt/v5.0/mqtt-v5.0.html

v3.1.1和v5.0之间的差异

https://github.com/Youjingyu/mqtt.github.io/wiki/v3.1.1%E5%92%8Cv5.0%E4%B9%8B%E9%97%B4%E7%9A%84%E5%B7%AE%E5%BC%82

1 编译和安装

1.1 Download source code

http://mosquitto.org/download/

https://github.com/eclipse/mosquitto

libwebsockets: git clone https://libwebsockets.org/repo/libwebsockets

or git clone https://github.com/warmcat/libwebsockets.git

1.2 安装

1.2.1 源码安装

下载http://mosquitto.org/download/

下载解压:tar -xzvf mosquitto-1.6.9.tar.gz

直接在目录下运行:  cd  mosquitto-1.6.9; make即可

# Comment out to remove publishing of the $SYS topic hierarchy containing# information about the broker state.WITH_SYS_TREE:=yes# Build with systemd support. If enabled, mosquitto will notify systemd after# initialization. See README in service/systemd/ for more information.WITH_SYSTEMD:=no# Build with SRV lookup support.WITH_SRV:=no# Build with websockets support on the broker.WITH_WEBSOCKETS:=no# Use elliptic keys in brokerWITH_EC:=yes# Build man page documentation by default.WITH_DOCS:=yes

如果要修改编译选项, 例如要mosquitto支持websocket

可以修改config.mk,再make

要支持websocket,  编译时会报错:

mosquitto.c:49:12: fatal error: libwebsockets.h: No such file or directory

#  include <libwebsockets.h>

这时因为缺少libwebsockets包,需要先安装libwebsockets:

git clone https://libwebsockets.org/repo/libwebsockets

1.2.2 二进制安装

源码安装太繁琐,可以直接二进制安装。

以Ubuntu18.04为例:

sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa

sudo apt-get update

sudo apt-get install mosquitto

sudo apt-get install mosquitto-clients

1.3 启动运行

Binary:/usr/sbin/mosquito

Configuration file:/etc/mosquitto/mosquitto.conf

Systemd file: /lib/systemd/system/mosquitto.service

Systemd 启动

安装完成后,运行命令systemctl status mosquitto,如果有以下输出,说明安装成功。

> systemctl status mosquitto

mosquitto.service - Mosquitto MQTT v3.1/v3.1.1 Broker

   Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor preset: enabled)

   Active: active (running) since Mon 2020-04-20 13:25:31 CST; 4min 24s ago

     Docs: man:mosquitto.conf(5)

           man:mosquitto(8)

 Main PID: 4487 (mosquitto)

    Tasks: 1 (limit: 4915)

   CGroup: /system.slice/mosquitto.service

           └─4487 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

4 20 13:25:31 robin systemd[1]: Starting Mosquitto MQTT v3.1/v3.1.1 Broker...

4 20 13:25:31 robin systemd[1]: Started Mosquitto MQTT v3.1/v3.1.1 Broker

手动启动

mosquitto -c /etc/mosquitto/mosquitto.conf -d

-c:指定配置文件
-d:后台运行

2 配置

配置文件放在/etc/mosquitto/目录下。

可以运行: man 5 mosquitto.conf 查看完整的配置说明

配置参数有很多,本人已经翻译成中文, 可以从以下获取完整的中文翻译版本:

https://blog.csdn.net/lclfans1983/article/details/105670039

https://blog.csdn.net/lclfans1983/article/details/105670288

https://blog.csdn.net/lclfans1983/article/details/105680528

2.1安全配置

/etc/mosquitto/conf.d$ 创建 robot.conf

# 禁止匿名访问allow_anonymous false# 认证配置password_file /etc/mosquitto/pwfile# 权限配置acl_file /etc/mosquitto/aclfile

创建密码文件

$ sudo touch /etc/mosquitto/pwfile

向密码文件添加用户

$ sudo mosquitto_passwd /etc/mosquitto/pwfile inspectRobot1

会让你输入两遍密码,  本例输入: foa123

$ sudo mosquitto_passwd -b /etc/mosquitto/pwfile usr1 usr1

$ sudo mosquitto_passwd -b /etc/mosquitto/pwfile usr2 usr2

创建权限配置文件

sudo touch /etc/mosquitto/aclfile

添加以下内容

# usr1只能发布以test为前缀的主题,订阅以$SYS开头的主题即系统主题user usr1topic write test/#topic read $SYS/## usr2只能订阅以test为前缀的主题user usr2topic read test/#

这时运行$ mosquitto_sub -h localhost -t "test/#" -i "client1" 会报以下错误

Connection error: Connection Refused: not authorised.

mosquitto_sub -h localhost -t "test/#" -u inspectRobot1 -P foa123 -i "client1"

mosquitto_pub -h localhost -t "test/abc" -u usr1 -P usr1 -i "client2" -m "How are you?"

3 测试工具

mosquitto_pub: 发布消息

mosquitto_sub:订阅消息

常用参数介绍:

参数

描述

-h

服务器主机,默认localhost

-t

指定主题

-u

用户名

-P

密码

-i

客户端id,唯一

-m

发布的消息内容

mosquitto_sub -h localhost -t "test/#" -u usr1 -P usr1 -i "client1"

mosquitto_sub -h localhost -t "test/#" -i "client1"

mosquitto_pub -h localhost -t "test/abc" -u usr2 -P usr12 -i "client3" -m "How are you?"

mosquitto_pub -h localhost -t "test/abc" -i "client3" -m "How are you?"

发布:

mosquitto_pub -h localhost -t "test/abc" -i "client3" -m "{\"header\":123, \"data\":{\"co\":0.002, \"o2\":23.0}}"

订阅结果为:

{"header":123, "data":{"co":0.002, "o2":23.0}}

Reference

https://www.jianshu.com/p/9e3cb7042a2e

Mosquitto安装配置和使用指南相关推荐

  1. cmake安装配置及入门指南

    前言 今天,从github下载代码学习,让我用cmake编译,纳尼?make我知道,cmake是啥鬼?天啊,无知很可怕!赶紧mark一波,虽然很耽误学习进度,但感觉还是要get一波! 一.安装准备 感 ...

  2. 【转】Android Studio安装配置学习教程指南 Gradle基础--不错

    原文网址:http://www.linuxidc.com/Linux/2015-02/113890p4.htm 其实很早之前也写了一篇Gradle的基础博客,但是时间很久了,现在Gradle已经更新了 ...

  3. Win10 Terminal + WSL 2 安装配置指南,精致开发体验 - 知乎 (zhihu.com)

    Win10 Terminal + WSL 2 安装配置指南,精致开发体验 - 知乎 (zhihu.com) https://zhuanlan.zhihu.com/p/273237897

  4. 服务器证书安装配置指南(Nginx)-天威诚信

    服务器证书安装配置指南(Nginx) 一.生成证书请求 您需要使用CSR生成工具来创建证书请求.    1.下载AutoCSR:   http://www.itrus.cn/soft/autocsr. ...

  5. RabbitMQ快速安装配置指南

    RabbitMQ快速安装配置指南 官网的安装教程由于需要解释原理很多废话,这里总结一下在CentOS7环境下的安装配置过程.如需理解原理,请看官网原文的安装指南或翻译 1. 安装RabbitMQ se ...

  6. GitLab 安装配置指南

    为什么80%的码农都做不了架构师?>>>    GitLab 在 CentOS 7系统上的安装配置指南 1.简单介绍 GitLab 是利用 Ruby on Rails 开发的一个开源 ...

  7. Oracle data integrator 11g安装配置和一个实例应用指南pdf

    <Oracle data integrator 11g安装配置和一个实例应用指南pdf> 下载地址: 网盘下载 转载于:https://www.cnblogs.com/long12365/ ...

  8. 高可用,完全分布式Hadoop集群HDFS和MapReduce安装配置指南

    原文:http://my.oschina.net/wstone/blog/365010#OSC_h3_13 (WJW)高可用,完全分布式Hadoop集群HDFS和MapReduce安装配置指南 [X] ...

  9. vSphere Web Client使用指南之安装配置

    2019独角兽企业重金招聘Python工程师标准>>> vSphere Web Client使用指南之安装配置 vSphere Web Client是为忙碌的管理员提供的一款通用的. ...

  10. squid 3 反向代理 缓存服务器 安装配置精华指南 by lbj

    squid-3.0.STABLE21 安装配置 by lbj 作用:作为反向代理/缓存服务器 系统:redhat linux 企业版 5.4 linux 要有gcc编译器,使用squid-3.0.ST ...

最新文章

  1. 一行代码,解决空指针问题.
  2. 【Android UI】图片 + 文字展示by SpannableStringBuilder
  3. 基于自然语言识别下的流失用户预警
  4. python 直方图排序_利用直方图对lis进行排序
  5. JavaScript实现碰撞检测(分离轴定理)
  6. 吃鸡11月15服务器维护,绝地求生11月20日维护到几点 11.20吃鸡更新维护公告
  7. 一次mongoengine查询速度慢的优化
  8. 甲骨文Java Archive
  9. 画面每秒传输帧数是什么意思
  10. 基于Laravel+VueJS实战开发WebAPP
  11. PHM算法与智能分析技术
  12. 互联网搜索 解决问题的心法 找什么 哪里找 怎么找
  13. 光伏发电极其并网控制技术 最大功率点跟踪
  14. 菠萝V1mini是以太坊唯一的静音机器
  15. 国内大多数网站的密码在 post 传输过程中都是明文的,这正常吗?
  16. JavaScript删除DOM
  17. AlphaTest烘焙的阴影不正确
  18. 实证研究的步骤_实证研究方法究竟有多重要,被这5张图惊到了!
  19. RK3288开发板GPIO介绍
  20. 旧电脑又卡有慢,一招设置电脑最佳性能

热门文章

  1. 【Pygame实战】强烈推荐:教你用百行代码编写一款《小蜜蜂历险记》不信?来看看(玩儿嗨了~)
  2. c++win32项目 如何显示后再删除一个绘图_如何运用Excel,R等软件结合PPT做出你想要的矢量图...
  3. 如何在PowerPoint中显示,隐藏或调整幻灯片缩略图的大小
  4. 【21考研】计算机/软件等专业调剂信息集合!【完结版】
  5. HDU 2608 0 or 1 简单数论
  6. Ubuntu 16.04 (Ubuntu18.04 也可以用)远程桌面(使用win10远程桌面客户端链接)和 使用vncveiwer链接的配置
  7. 超级表格终于上线「文件转让」功能!文件调动容易解决!
  8. 京东商家下单未付款?订单催付教程
  9. dat image 微信_电脑微信image文件夹下的dat文件怎么打开?
  10. 第三方平台代微信公众号开发