2.5 安装及踩坑
1.源码包下载:http://mosquitto.org/files/source/
或者wget http://mosquitto.org/files/source/mosquitto-1.4.9.tar.gz
解压:tar -zxvf mosquitto-1.4.tar.gz
进入目录:cd mosquitto-1.4
2.编译安装
打开配置文件,去掉暂且不需要的功能:
vi config.mk
如:WITH_TLS,WITH_TLS_PSK, WITH_SRV, WITH_WEBSOCKETS, WITH_SOCKS, WITH_UUID等
保存退出:wq
安装mosquitto
make
make install
踩过的坑:
a】编译找不到openssl/ssl.h
安装openssl sudo apt-get install libssl-dev
【b】编译过程找不到ares.h
sudo apt-get install libc-ares-dev
【c】编译过程找不到uuid/uuid.h
sudo apt-get install uuid-dev
【d】使用过程中找不到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
【e】make: g++:命令未找到
【解决方法】
安装g++编译器
sudo apt-get install g++
启动 mosquitto broker
mosquitto -c /etc/mosquitto/mosquitto.conf.example &
-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.
订阅消息:
./mosquitto_sub -h 127.0.0.1 -p 1883 -t "/sports/wordcup"
发布消息:
./mosquitto_pub -h 127.0.0.1 -p 1883 -t "/sports/wordcup " -m "this is carter hello"
或者
./mosquitto_sub -h 10.129.4.12 -p 1883 -t "/sports/wordcup"
./mosquitto_pub -h 10.129.4.12 -p 1883 -t "/sports/wordcup" -m "this is carter hello 666"
外网地址不行,所以我在本机用paho代码一直报timeOut异常,原因是服务器防火墙未放开端口
【解决办法】
放开防火墙端口:firewall-cmd --add-port=1883/tcp –permanent
重启防火墙:systemctl restart firewalld