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
保存後,執行
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
執行
cp mosquitto.conf.example mosquitto.conf
cp pwfile.example pwfile
然後修改 文件mosquitto.conf ,
並在文件最後加入
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個窗口
訂閱:
發送消息:
如果訂閱窗口打印出hello world ,證明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