1、今天与客户联调,客户反映我们搭建的mqtt server连不上,我之前一直程序直连的,为了验证这个问题,特地安装了一个客户端。
先将安装过程记录如下:
1、下载客户端源码包,网上用的比较多的是mosquitto,网上找了个下载地址:
http://www.eclipse.org/downloads/download.php?file=/mosquitto/source/mosquitto-1.4.15.tar.gz
2、解压到 /opt目录下
tar -zxvf mosquitto-1.4.15.tar.gz
3、make 编译,这个过程比较曲折,各种报错,好不折腾。
错误1:
mosquitto.c:884: error: ‘struct mosquitto’ has no member named ‘achan’
mosquitto.c:887: warning: implicit declaration of function ‘ares_fds’
mosquitto.c:887: error: ‘struct mosquitto’ has no member named ‘achan’
mosquitto.c:992: error: ‘struct mosquitto’ has no member named ‘achan’
mosquitto.c:993: warning: implicit declaration of function ‘ares_process’
mosquitto.c:993: error: ‘struct mosquitto’ has no member named ‘achan’
解决方案:
到解压包的路径下修改conifg.mk 中的WITH_SRV=YES =>WITH_SRV=no
# Build with SRV lookup support.
WITH_SRV:=no
错误2:
g++: Command not found
解决方案:
编译之前,需要安装c++插件
yum install gcc gcc-c++
错误3:
read_handle_server.c:31:25: error: uuid/uuid.h: No such file or directory
read_handle_server.c: In function ‘client_id_gen’:
read_handle_server.c:46: error: ‘uuid_t’ undeclared (first use in this function)
read_handle_server.c:46: error: (Each undeclared identifier is reported only once
read_handle_server.c:46: error: for each function it appears in.)
read_handle_server.c:46: error: expected ‘;’ before ‘uuid’
read_handle_server.c:59: warning: implicit declaration of function ‘uuid_generate_random’
read_handle_server.c:59: error: ‘uuid’ undeclared (first use in this function)
read_handle_server.c:60: warning: implicit declaration of function ‘uuid_unparse_lower’
方案2:
yum install openssl-devel(根据需要)
yum install c-ares-devel(必须)
yum install libuuid-devel(必须)
3、安装
sudo make install //安装时需要管理员权限
4、安装完成后,验证
[root@localhost mosquitto-1.4.5]# mosquitto --help mosquitto version 1.4.5 (build date 2018-04-18 01:26:54-0700) mosquitto is an MQTT v3.1 broker.
....
5、测试
切换两个窗口:
订阅方:
mosquitto_sub -t topicA -h 111.222.67.243 -u tbox-client -P 1qaz2wsx
发布方:
mosquitto_pub -t topicA -m Msgfrom1 -h 111.222.67.243 -u tbox-client -P 1qaz2wsx
出现意外状态:
找不到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
# 更新动态链接库
sudo ldconfig
附录参数解释:
-h broker地址
-p 端口 (不指定默认1883)
-u 用户名(连接的用户名,mqtt server默认是开启匿名用户访问的,不需要)
-P 密码 (连接的密码,mqtt server默认是开启匿名用户访问的,不需要)
-m 消息内容
-t topic名字
参考文档:
https://www.cnblogs.com/xiaoerhei/p/3777157.html
http://blog.163.com/allegro_tyc/blog/static/33743768201667101816858/
https://blog.csdn.net/laughing_cui/article/details/46530785