mosquitto

是啥

mosquitto是基于MQTT协议的开源消息服务端,同时也提供简易使用的客户端以及客户端库,使用C/C++编写(主要部分使用C编写,提供C++封装的客户端库)。mosquitto作为一个MQTT服务端(文档中称为broker,翻译为经理人、代理人,我习惯称为服务端),实现了MQTT3.1、MQTT3.1.1、MQTT5版本。截止目前为止最新版本是2.0.11,相较于mosquitto1.6.X,mosquitto2.X性能有较大提升。

安装

编译安装

到github上下载需要的版本,然后编译安装。

编译安装2.0.11:

# apt install -y wget tar gcc g++ cmake libssl-dev
# wget https://github.com/eclipse/mosquitto/archive/refs/tags/v2.0.11.tar.gz
# tar -zxvf v2.0.11.tar.gz
# cd mosquitto-2.0.11/
# mkdir ____build
# cd ____build/
# vi ../CMakeLists.txt// 修改这行,不编译文档
option(DOCUMENTATION "Build documentation?" OFF)// 先安装到当前目录的__install_x86目录下,如果需要安装到系统,用 cmake .. 即可
# cmake -DCMAKE_INSTALL_PREFIX=${PWD}/__install_x86 ..
# make -j8
# make install
# cd __install_x86
# vi etc/mosquitto/mosquitto.conf// 监听端口
listener 1883 0.0.0.0
// 允许匿名登录
allow_anonymous true# ./sbin/mosquitto -c etc/mosquitto/mosquitto.conf

编译安装1.5.3:

// 安装必要的依赖
# apt install -y wget tar gcc g++ cmake libssl-dev
# wget https://mosquitto.org/files/source/mosquitto-1.5.3.tar.gz
# tar -zxvf mosquitto-1.5.3.tar.gz
# cd mosquitto-1.5.3
# mkdir build
# cd build
# cmake ..
# make && make install

如果编译遇到问题可以参考最后的问题列表。

编译完目录结构大致如下:

├── bin
│   ├── mosquitto_passwd    // 生成、加密密码文件命令
│   ├── mosquitto_pub       // mqtt发布客户端
│   ├── mosquitto_rr        // ?
│   └── mosquitto_sub       // mqtt订阅客户端
├── etc
│   └── mosquitto   // 配置文件
│       ├── aclfile.example // 控制访问示例列表
│       ├── mosquitto.conf  // mosquitto配置文件
│       ├── pskfile.example // psk示例文件
│       ├── pwfile.conf     // 密码示例文件
│       └── pwfile.example  // 密码示例文件
├── include         // 头文件
│   ├── mosquitto_broker.h
│   ├── mosquitto.h
│   ├── mosquitto_plugin.h
│   └── mosquittopp.h
├── lib             // 依赖库,我把依赖了openssl库也放在这里了
│   ├── libcrypto.so.1.0.0
│   ├── libmosquitto.so -> libmosquitto.so.1
│   ├── libmosquitto.so.1 -> libmosquitto.so.1.6.3
│   ├── libmosquitto.so.1.6.3
│   └── libssl.so.1.0.0
├── sbin
│   ├── mosquitto // mosquitto服务端

命令安装

因为我使用的是ubuntu系统,为了简单,也可以直接使用命令安装,这里根据ubuntu系统版本会安装对应版本的mosquitto,并且不用考虑依赖问题。不过,命令行安装我看只有1.4.X版本。

# apt install mosquitto

启动服务端

启动命令

mosquitto [-c config file] [ -d | --daemon ] [-p port number] [-v]-c 配置文件路径
-p 指定端口,默认1883,监听端口最好配置在配置文件中
-d 后台启动
-v 打印全部日志,等于配置文件中的 log_type=all

启动服务端(关于启动的配置文件参数,参考另一篇关于配置文件mosquitto.conf的文档。)

# mosquitto -c /etc/mosquitto/mosquitto.conf

测试

订阅

订阅主题为mqtt的消息

# mosquitto_sub -t "mqtt"

发布

(再起一个终端)发布内容为 "hello world"到主题mqtt,切到监听端的终端就能看到收到消息了

# mosquitto_pub -t "mqtt" -m "hello world"

连接test.mosquitto.org

前面我们使用mosquitto来创建服务,如果有公用的服务器(如test.mosquitto.org),那么只需要使用mosquitto_sub和mosquitto_pub来发布订阅即可。

订阅端

// 恰逢IG夺冠,来,订阅一波IG
# root@ubuntu:~# mosquitto_sub -h test.mosquitto.org -t "IG"
b
c

发布端

root@ubuntu:~# mosquitto_pub -h test.mosquitto.org -t "ig" -m "a"
root@ubuntu:~# mosquitto_pub -h test.mosquitto.org -t "IG" -m "b"
root@ubuntu:~# mosquitto_pub -h test.mosquitto.org -t "IG" -m "c"
root@ubuntu:~#

可以发现,发布端使用 -t “ig” -m “a” 并没有被收到,-t “IG” -m "b"才能收到, 说明 -t(topic话题)大小写敏感

问题列表

编译安装可能遇到的问题

使用make && make install 失败,出现了下面几个问题

make[1]: cc command not found

安装gcc

# apt install gcc

编译找不到openssl/ssl.h

使用apt-get install openssl-xxxx

# apt install libssl-dev

缺少g++

# apt-get install g++

需要uuid。 uuid/uuid.h: No such file or directory错误

wget http://downloads.sourceforge.net/e2fsprogs/e2fsprogs-1.41.14.tar.gz
tar xvzf e2fsprogs-1.41.14.tar.gz
// 进入e2fsprogs-1.41.14目录后执行
cd e2fsprogs-1.41.14
./configure --enable-elf-shlibs
make && make install
cp -r lib/uuid/    /usr/include/
cp -rf lib/libuuid.so* /usr/lib

libwebsockets.h missing from the src directory

这个问题是因为我要使用到websocket功能,按照下面命令照撸就行了

git clone -b v2.4.1 https://github.com/warmcat/libwebsockets.git
cd libwebsockets
mkdir build
cd build
cmake .. -DLIB_SUFFIX=64
make
make install

如果没有安装cmake

# apt-install install cmake

我就出现了这几个错误,然后使用make && make isntall,可以了。

启动问题

libwebsockets.so.12: cannot open shared object file: No such file or directory

mosquitto -c /etc/mosquitto/mosquitto.conf
mosquitto: error while loading shared libraries: libwebsockets.so.12: cannot open shared object file: No such file or directory

为什么是lib64,因为我们前面cmake的时候安装cmake .. -DLIB_SUFFIX=64

[root@localhost lib]# cd /usr/local/lib64/
[root@localhost lib64]# ll
总用量 516
drwxr-xr-x. 3 root root     27 12月 20 09:49 cmake
-rw-r--r--. 1 root root 311854 12月 20 09:49 libwebsockets.a
lrwxrwxrwx. 1 root root     19 12月 20 09:49 libwebsockets.so -> libwebsockets.so.12
-rwxr-xr-x. 1 root root 211208 12月 20 09:49 libwebsockets.so.12
drwxr-xr-x. 2 root root     61 12月 20 09:49 pkgconfig
[root@localhost lib64]# ln -s /usr/local/lib64/libwebsockets.so.12 /usr/lib/libwebsockets.so.12
[root@localhost lib64]# ldconfig

再次启动就可以了

发布问题

发布内容为 "hello world"到主题mqtt,

# mosquitto_pub -t "mqtt" -m "hello world"

发布时报错 mosquitto_pub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory

mosquitto_pub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory

运行下面的命令即可

# sudo /sbin/ldconfig

再次运行

# mosquitto_pub -t "mqtt" -m "hello world"

又报错 Error: The connection was lost.

Error: The connection was lost.

注意此时需要修改配置文件中的一些配置项,就是下面这一个

# vi mosquitto.conf
allow_anonymous true

重启mosquitto,注意使用如下方式重启,有一些配置项是无法重新加载的

# kill -s SIGHUP [mosquitto的pid]

用这种方式重启:先停止mosquitto服务,然后再重新启动

# serivce mosquitto restart或者
# service mosquitto stop
# mosquitto -c /etc/mosquitto/mosquitto.conf或者
# ps -ef|grep mosquitto
# kill -9 [pid]
# mosquitto -c /etc/mosquitto/mosquitto.conf

再次运行

# mosquitto_pub -t "mqtt" -m "hello world"

OK, 这次成功了,切到监听端的终端就能看到收到消息了

参考

  • 编译中webosocket问题解决参考
  • libwebsockets.so.12: cannot open shared object file: No such file or directory
  • mosquitto官网
  • mosquitto源码
  • MQTT协议组织官网

2018-11-05-mqtt-mosquitto系列01之编译安装启动相关推荐

  1. 2018.11.05 NOIP模拟 规避(最短路计数)

    传送门 正难则反. 考虑计算两人相遇的方案数. 先正反跑一遍最短路计数. 然后对于一条在最短路上的边(u,v)(u,v)(u,v),如果(dis(s,u)*2<total&&di ...

  2. 3. CMake 系列 - 分模块编译安装项目

    目录 1. 项目目录结构 2. 相关代码 2.1 add 模块 2.2 sub 模块 2.3 测试模块 2.4 顶层 CMakeLists.txt 3. 编译 & 安装 4. 项目安装基本语法 ...

  3. IDEA 2018 激活 IDEA 2018.3激活教程 最新的(三种)—2018.11.26亲测

    https://blog.csdn.net/HALEN001/article/details/81137092 IntelliJ IDEA 2018.3(Ultimate Edition)激活方法 本 ...

  4. QIIME 2用户文档. 4人体各部位微生物组分析实战Moving Pictures(2018.11)

    文章目录 前情提要 QIIME 2用户文档. 4人体各部位微生物组 启动QIIME2运行环境 样本元数据 下载和导入数据 拆分样品 序列质控和生成特征表 方法1. DADA2 方法2. Deblur ...

  5. 在ubuntu上安装,使用MQTT Mosquitto

    这是一种相对简单的安装办法,不用去下载源码后再编译安装,省去了编译和运行过程中出现的各种错误和安装各种依赖库. 在ubuntu上安装,使用MQTT Mosquitto 以下描述了如何安装Mosquit ...

  6. 【MQTT从入门到提高系列 | 01】从0到1快速搭建MQTT测试环境

    这是机器未来的第24篇文章 原文首发地址:https://blog.csdn.net/RobotFutures/article/details/125532208 1. mosquitto概述 Ecl ...

  7. QIIME 2用户文档. 7差异丰度分析gneiss(2018.11)

    文章目录 前情提要 QIIME 2用户文档. 7差异丰度分析gneiss 创建`balances` 选项1:相关性聚类 选项2:梯度聚类 用平衡建立线性模型 Reference 译者简介 猜你喜欢 写 ...

  8. QIIME 2教程. 01简介和安装 Introduction Install(2020.11)

    文章目录 写在前面 QIIME 2的优势 QIIME 2用户文档(版本:2020.11) 视频:QIIME 2用户文档01.1 简介 入门指南 什么是QIIME 2? 核心概念 数据文件: QIIME ...

  9. Java第十二天~第十三天/11.04~11.05

    第十二天/11.04 一.选择排序 从0索引开始,用它对应的元素依次和后面索引对应的元素进行比较,小的往前放,第一次比较完毕后,最小值出现在最小索引处,依次比较,就可以得到一个排好序的数组. pack ...

最新文章

  1. wordpress短代码转php,WordPress中的shortcode短代码功能使用详解
  2. GSA+麦肯锡开年首场线上活动:汽车半导体要变天?
  3. GoogleNet - Going deeper with convolutions
  4. Ubuntu 16.04下Caffe-SSD的应用——常见训练时报错总结
  5. 如何搭建socks5和ss节点_以太坊区块链搭建与使用(三)-联盟链
  6. PHP 发邮件《转》
  7. Linux expect与Shell交互
  8. 【转】一步步构建大型网站架构
  9. 第一章 计算机网络 3 标准化工作和相关组织 [计算机网络笔记] -简单浏览了解即可
  10. mybatis 鉴别其_Mybatis学习笔记9 - 鉴别器discriminator
  11. ThinkPHP2.1 增加PHPCMS模板引擎,支持PC标签(get,json)
  12. TMS320F28035 中断中使用DINT,无法关闭中断的原因
  13. 年长车友的单车游记:骑单车游崇明岛(转)
  14. 【解决办法】ES文件浏览器无法播放该链接
  15. 苹果cms影视建站系统免费吗?
  16. 我国会计计算机的发展历程,会计的发展历程是什么
  17. 弹出框动态增加input输入框
  18. 技术解析 | 云游戏在未来如何实现?
  19. 安装win 7 + ubuntu 16.04 双系统安装
  20. 我的运动、通勤好搭档,南卡Runner Pro 4骨传导耳机深度测评

热门文章

  1. 一度智信|拼多多优惠券怎么取消
  2. python对文件的操作都有什么_python中文件操作的相关内容总结(附示例)
  3. foxmail邮件导入Outlook 2010
  4. Fiddler配置及使用教程
  5. 第四次网页前端学习笔记(css)
  6. 更改ip地址的软件多少钱一个月_武汉社保代缴多少钱一个月?武汉社保一个月交多少钱?...
  7. QQ群视频出现imsdk登录失败的解决方法
  8. OpenCV玩九宫格数独(零)——预告篇
  9. 深度学习之图像分类(六)--Inception进化史
  10. bzoj1754: [Usaco2005 qua]Bull Math