CentOs 6.5 MQTT 安装部署

时间:2024-03-18 12:38:41

CentOs 6.5 MQTT 安装部署
所需安装包:
mqtt官网:
http://mosquitto.org/download/
libwebsockets官网: https://libwebsockets.org/
libwebsockets-v1.6-stable.tar.gz,mosquitto-1.4.8.tar.gz
1、安装依赖

yum -y install gcc gcc-c++ openssl-devel c-ares-devel libuuid-devel wget cmake

2、为mosquitto增加websocket支持,需要安装websocket库文件
下载websocket,我用的是 libwebsockets-v1.6-stable.tar.gz,
解压缩:# tar zxfv libwebsockets-v1.6-stable.tar.gz
3、运行 websocket

cd libwebsockets-v1.6-stable

mkdir bulid

cd bulid

cmake …

make && make install

4、安装mosquitto
下载mqtt,我用的是 mosquitto-1.4.8.tar.gz,
解压缩:# tar zxfv mosquitto-1.4.8.tar.gz
5、修改config.mk文件以使后面编译的mosquitto文件支持websocket。

cd /mosquitto-1.4.8

找到mosquitto-1.4.8目录下的config.mk文件,把config.mk 文件中的 WITH_WEBSOCKETS:=no 改为yes
CentOs 6.5 MQTT 安装部署
保存后,执行

make && make install

ln -s /usr/local/lib/libwebsockets.so.6 /usr/lib64/libwebsockets.so.6

groupadd mosquitto

useradd -g mosquitto mosquitto

注意:如果make遇到错误解决方法参考第九点
6、创建mosquitto.conf、pwfile文件

cd /etc/mosquitto/

如果该目录下没有mosquitto.conf 和 pwfile
CentOs 6.5 MQTT 安装部署
执行

cp mosquitto.conf.example mosquitto.conf

cp pwfile.example pwfile

然后修改 文件mosquitto.conf ,
CentOs 6.5 MQTT 安装部署
并在文件最后加入
CentOs 6.5 MQTT 安装部署
7、启动 mqtt

mosquitto -c /etc/mosquitto/mosquitto.conf

用于测试动态显示mqtt服务的连接动态来调试mqtt服务
8、测试
在安装过程中,或测试过程中可能会遇到错误:
mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
解决方法:

cat /etc/ld.so.conf

echo “/usr/local/lib”>>/etc/ld.so.conf

ldconfig

需要打开2个窗口
订阅:
发送消息:
CentOs 6.5 MQTT 安装部署

如果订阅窗口打印出hello world ,证明MQTT安装成功。
CentOs 6.5 MQTT 安装部署

结合php测试参考(php测试mqtt文档)
9、错误解决
在安装过程中,或测试过程中可能会遇到错误:
(1)mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
解决方法:

cat /etc/ld.so.conf

echo “/usr/local/lib”>>/etc/ld.so.conf

ldconfig

(2)mosquitto_internal.h:51:20: error: ares.h: No such file or directory
其实是因为它新加的功能的原因。打开config.mk文件。
吧WITH_SRV:=yes 改成WITH_SRV:=no 就可以编译了
(3)mosquitto.c:47: fatal error: libwebsockets.h: No such file or directory
若使用websocke,则修改编译规则文件config.mk

Build with websockets support on the broker.

WITH_WEBSOCKETS:=yes
(4)使用过程中找不到libmosquitto.so.1
error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
【解决方法】修改libmosquitto.so位置

创建链接

sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1

(5)报错.read_handle_server.c:31:25: fatal error: uuid/uuid.h: No such file or directory
没有安装uuid 执行上边开始的准备yum安装即可

1540285672: New connection from 127.0.0.1 on port 1883.
1540285672: New client connected from 127.0.0.1 as phpMQTT-publisher (c1, k10).

New connection from 127.0.0.1 on port 1883.
1540285730: New client connected from 127.0.0.1 as api-client_id (c1, k10).
1540285730: Socket error on client api-client_id, disconnecting.

Mqtt服务的卸载:whereis mosquitto
删除已安装的mqtt文件即可,重新解压安装配置安装mqtt服务
mqtt服务配置文件 文件夹/etc/mosquitto
mqtt命令文件 /usr/local/sbin/mosquitto 相关mosquitto mosquitto_sub mosquitto_pub mosquitto_passwd