环境信息:

1. Linux Centos7.2 环境,CPU 2核,内存8G.

2. mosquitto版本:mosquitto-1.5.4

官网:http://mosquitto.org/download/
libwebsockets官网: https://libwebsockets.org/

本环境搭建,主要用到了mosquitto,么有涉及websocket的内容,所以,这里不做安装介绍。

搭建过程:

1. 安装mosquitto-1.5.4

下载解压缩mosquitto-1.5.4.tar.gz。然后执行make

[root@ws2 mosquitto-1.5.]# make
set -e; for d in lib client src; do make -C ${d}; done
make[]: Entering directory `/opt/mosquitto-1.5./lib'
cc -Wall -ggdb -O2 -I. -I.. -I../lib -fPIC -DWITH_TLS -DWITH_TLS_PSK -DWITH_THREADING -DWITH_SOCKS -c mosquitto.c -o mosquitto.o
In file included from mosquitto.c:::
mosquitto_internal.h::: fatal error: openssl/ssl.h: No such file or directory
# include <openssl/ssl.h>
^
compilation terminated.
make[]: *** [mosquitto.o] Error
make[]: Leaving directory `/opt/mosquitto-1.5./lib'
make: *** [mosquitto] Error

上述错误是因为没有安装openssl-devel。安装上openssl-devel即可解决: yum install openssl-devel

make -C cpp
make[]: Entering directory `/opt/mosquitto-1.5./lib/cpp'
g++ -Wall -ggdb -O2 -I. -I.. -I../lib -fPIC -c mosquittopp.cpp -o mosquittopp.o
make[]: g++: Command not found
make[]: *** [mosquittopp.o] Error
make[]: Leaving directory `/opt/mosquitto-1.5./lib/cpp'
make[]: *** [all] Error
make[]: Leaving directory `/opt/mosquitto-1.5./lib'

上述错误是因为没有安装gcc-c++. 安装上gcc-c++即可解决: yum install gcc-c++

cc -I.. -Wall -ggdb -O2  -c mosquitto_passwd.c -o mosquitto_passwd.o
cc mosquitto_passwd.o -o mosquitto_passwd -lcrypto
make[]: Leaving directory `/opt/mosquitto-1.5./src'
set -e; for d in man; do make -C ${d}; done
make[]: Entering directory `/opt/mosquitto-1.5./man'
make[]: Nothing to be done for `all'.
make[]: Leaving directory `/opt/mosquitto-1.5./man'

上述错误是因为没有安装c-ares-devel. 安装上c-ares-devel即可解决: yum install c-ares-devel

解决上面的问题,可以一次安装下面的所有的依赖:yum -y install gcc gcc-c++ openssl-devel c-ares-devel libuuid-devel wget cmake

[root@ws2 mosquitto-1.5.]# yum -y install gcc gcc-c++ openssl-devel c-ares-devel libuuid-devel wget cmake
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package gcc-4.8.-.el7.x86_64 already installed and latest version
Package gcc-c++-4.8.-.el7.x86_64 already installed and latest version
Package :openssl-devel-1.0.2k-.el7.x86_64 already installed and latest version
Package c-ares-devel-1.10.-.el7.x86_64 already installed and latest version
Package libuuid-devel-2.23.-.el7.x86_64 already installed and latest version
Package wget-1.14-.el7_4..x86_64 already installed and latest version
Package cmake-2.8.12.2-.el7.x86_64 already installed and latest version

2. 配置mosquitto

进入/etc/mosquitto目录

[root@ws2 mosquitto]# cp mosquitto.conf.example mosquitto.conf
[root@ws2 mosquitto]# cp pwfile.example pwfile

vi mosquitto.conf,做下面配置

allow_anonymous true
password_file /etc/mosquitto/pwfile

然后,在文件最末尾补充(mosquitto服务监听的端口):

port 1883

listener 9090

protocol websockets

3. 启动mosquitto

[root@ws2 mosquitto]# mosquitto -c /etc/mosquitto/mosquitto.conf
Error: Websockets support not available.
Error found at /etc/mosquitto/mosquitto.conf:.
Error: Unable to open configuration file.

这个错误,是因为我没有安装websockets模块,另外,config.mk里面的websocket没有配置为YES。所以将配置中websocket的注释掉即可。

#listener
#protocol websockets

再次启动:

[root@ws2 mosquitto]# mosquitto -c /etc/mosquitto/mosquitto.conf
: mosquitto version 1.5. starting
: Config loaded from /etc/mosquitto/mosquitto.conf.
: Opening ipv4 listen socket on port .
: Opening ipv6 listen socket on port .
: Error: Invalid user 'mosquitto'.

上述错误显示没有mosquitto用户,创建用户并添加组

[root@ws2 mosquitto]# groupadd mosquitto
[root@ws2 mosquitto]# useradd -g mosquitto mosquitto

最后再次启动:

[root@ws2 mosquitto]# mosquitto -c /etc/mosquitto/mosquitto.conf
: mosquitto version 1.5. starting
: Config loaded from /etc/mosquitto/mosquitto.conf.
: Opening ipv4 listen socket on port .
: Opening ipv6 listen socket on port .

此时,mosquitto以前台服务的方式在运行。。。如何使用,请看帮助信息:

[root@ws2 mosquitto]# mosquitto --help
mosquitto version 1.5. mosquitto is an MQTT v3.1.1 broker. Usage: mosquitto [-c config_file] [-d] [-h] [-p port] -c : specify the broker config file.
-d : put the broker into the background after starting.
-h : display this help.
-p : start the broker listening on the specified port.
Not recommended in conjunction with the -c option.
-v : verbose mode - enable all logging types. This overrides
any logging options given in the config file. See http://mosquitto.org/ for more information.

4. 测试验证

[root@ws2 ~]# mosquitto_sub -t rulee
mosquitto_sub: error while loading shared libraries: libmosquitto.so.: cannot open shared object file: No such file or directory

此处错误,表明动态库没有配合好路径,参照下面的操作(主要是红色的两行指令):

[root@ws2 ~]# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
[root@ws2 ~]#
[root@ws2 ~]# echo "/usr/local/lib" >> /etc/ld.so.conf
[root@ws2 ~]#
[root@ws2 ~]# vi /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/lib
[root@ws2 ~]# ldconfig

然后再次订阅一个rulee的topic:

然后,再启动一个终端,进行发布消息操作:

[root@ws2 ~]# mosquitto_pub -h localhost -t rulee -m "hello rule engine"

在上面的订阅窗口已经收到这个发布的消息

[root@ws2 ~]# mosquitto_sub -t rulee
hello rule engine

安全控制:

上面的操作,是针对无安全控制的,下面,进行带安全控制的配置,进入/etc/mosquitto目录:

[root@ws2 mosquitto]# cp mosquitto.conf.example mosquitto.conf
[root@ws2 mosquitto]# cp pwfile.example pwfile
[root@ws2 mosquitto]# cp aclfile.example aclfile

vi mosquitto.conf,做下面配置

allow_anonymous false
password_file /etc/mosquitto/pwfile
acl_file /etc/mosquitto/aclfile

然后,在文件最末尾补充监听端口(若没有配置的话): port 1883

添加安全用户信息:

[root@ws2 mosquitto]# mosquitto_passwd -c /etc/mosquitto/pwfile shihuc
Password:
Reenter password:

重新启动mosquitto服务:

[root@ws2 mosquitto]# mosquitto -d -c /etc/mosquitto/mosquitto.conf

订阅:

[root@ws2 ~]# mosquitto_sub -t rulee
hello rule engine
Connection Refused: not authorised.
[root@ws2 ~]#
[root@ws2 ~]#
[root@ws2 ~]# mosquitto_sub -t /taikang/rulee -u shihuc -P shihuc

发布:

[root@ws2 ~]# mosquitto_pub -h localhost -t /taikang/rulee -m "hello rule engine with auth"
Connection Refused: not authorised.
Error: The connection was refused.
[root@ws2 ~]#
[root@ws2 ~]#
[root@ws2 ~]# mosquitto_pub -u shihuc -P shihuc -h localhost -t /taikang/rulee -m "hello rule engine with auth"
[root@ws2 ~]#

MQTT研究之mosquitto:【环境搭建】的更多相关文章

  1. windows环境下mosquitto环境搭建与mqtt测试

    https://blog.csdn.net/pgpanda/article/details/51800865 工作需求,自己在windows下搭建了一个mosquitto环境测试mqtt 话不多说,直 ...

  2. MQTT研究之EMQ:【基础研究】

    EMQ版本V2, emqttd-centos7-v2.3.11-1.el7.centos.x86_64.rpm 下载地址:http://emqtt.com/downloads/2318/centos7 ...

  3. android 研究的环境搭建、高效工具、网站资源

    =================  2015 年 10 月 14 号 更新 著名的android开源社区xda有一个帖子,详细描述了android开发和研究环境的初始搭建过程: http://for ...

  4. MQTT研究之EMQ:【CoAP协议应用开发】

    本博文的重点是尝试CoAP协议的应用开发,其中包含CoAP协议中一个重要的开源工具libcoap的安装和遇到的问题调研.当然,为了很好的将EMQ的CoAP协议网关用起来,也调研了下EMQ体系下,CoA ...

  5. 转 【MQTT】在Windows下搭建MQTT服务器

    MQTT简介 MQ 遥测传输 (MQTT) 是轻量级基于代理的发布/订阅的消息传输协议,设计思想是开放.简单.轻量.易于实现.这些特点使它适用于受限环境.该协议的特点有: 使用发布/订阅消息模式,提供 ...

  6. 【定有惊喜】android程序员如何做自己的API接口?php与android的良好交互(附环境搭建),让前端数据动起来~

    一.写在前面 web开发有前端和后端之分,其实android还是有前端和后端之分.android开发就相当于手机app的前端,一般都是php+android或者jsp+android开发.androi ...

  7. Nexus(一)环境搭建

    昨天,成功搭建了自己的 Maven 环境(详见:Maven(一)环境搭建),今天就来研究和探讨下 Nexus 的搭建! 使用背景: 安装环境:Windows 10 -64位 JDK版本:1.7 Mav ...

  8. Hibernate 系列 02 - Hibernate介绍及其环境搭建

    引导目录: Hibernate 系列教程 目录 昨晚喝多了,下午刚清醒,继续搞Hibernate.走起. 觉得还行的话,记得点赞哈,给我这个渣渣点学习的动力.有错误的话也请指出,省的我在错误上走了不归 ...

  9. 【转】Android Studio下加入百度地图的使用 (一)——环境搭建

    最近有学 生要做毕业设计,会使用到定位及地图信息的功能,特此研究了一下,供大家参考,百度定位SDK已经更新到了5.0,地图SDK已经更新到了3.5,但是在 AndroidStudio中使用还是存在一些 ...

随机推荐

  1. EXCEL VBA——数组,使用数组提升程序效率

    数组的存在价值就是让代码提速. 数组和非数组的差异只在于数据的保存和读取方式不同,虽然操作这些数据的方法或者函数并没有不同,但是保存与读取上的差异却使VBA代码在处理数据时实现了质的飞跃.在完成相同工 ...

  2. MVC实战之排球计分软件(深入了解面向对象编程)

    在此篇博客之前,我已经写了一个实战系列的博客,虽然不太成熟但是相对比较实用,在这篇博客我将继续使用mvc编程此软件. 此篇博客会在一定的时间内完成,此次完成的软件的一个需求是提供给运动员的使用.我将在 ...

  3. SAP跳过权限检查/前提是有debug权限

    SE37,AUTH_CHECK_TCODE加断点: 执行事务代码,进入断点,修改sy-subrc = 0,执行,就可以跳过权限检查. 只给了事务代码权限,没有操作权限: CL_SUID_TOOLS== ...

  4. mysql索引 ->创建索引、修改索引、删除索引的命令语句

    查看表中已经存在 index:show index from table_name; 创建和删除索引索引的创建可以在CREATE TABLE语句中进行,也可以单独用CREATE INDEX或ALTER ...

  5. 用Apache Ant在Weka中嵌入新算法

    本文将介绍一种新的添加新的算法到Weka中的方法,国内的论坛基本都是通过IDE(Eclipse或NetBeans)编译,详细教程请见上一篇博客.经研究,发现国外的网站很流行用Ant这个方法,教程奉上. ...

  6. jquery+jquery.rotate实现图片旋转效果

    首先要下载jquery.min.js 和jquery.rotate.js文件 1.下载地址: https://www.jb51.net/jiaoben/554113.html 2.导入文件 <s ...

  7. 在 NLTK 中使用 Stanford NLP 工具包

    转载自:http://www.zmonster.me/2016/06/08/use-stanford-nlp-package-in-nltk.html 目录 NLTK 与 Stanford NLP 安 ...

  8. 给学习Linux系统小白的两三个建议

    前段时间看过一个针对国内Linux使用情况的调研表,有了不少感慨.现在听说过linux,会一点linux基本操作的人多如牛毛,然而真正能用linux做一点事情的确少之又少.无论是公司还是学校,办公基本 ...

  9. C++中 string 的用法大全

    之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至 ...

  10. Linux Shell基础(下)

    Linux Shell基础(下) 目录 一.shell特殊符号cut命令 二.cut.sort.wc.uniq命令 三.tee.tr.split命令 四.简易审计系统 五.fork, exec, so ...