一、安裝
1、下載mosquitto安裝包
地址:http://mosquitto.org/files/source/
2、安裝依賴包
yum install gcc gcc-c++ libstdc++-devel
yum install openssl-devel -y
yum install c-ares-devel -y
yum install uuid-devel -y
yum install libuuid-devel -y
yum install c-ares-devel
3、編譯並安裝
解壓下載的安裝包:tar zxvf mosquitto-2.0.10.tar.gz
進入解壓后的文件夾:cd mosquitto-2.0.10
執行命令:make && make install
4、安裝后進入 /etc/mosquitto
中,復制配置文件
cp mosquitto.conf.example mosquitto.conf
5、啟動
mosquitto -c /etc/mosquitto/mosquitto.conf
//后台啟動
mosquitto -c /etc/mosquitto/mosquitto.conf -d
6、關閉,kill進程
ps -aux | grep mosquitto
kill -9 2438
至此安裝mosquitto完畢!
二、設置用戶密碼
1、打開配置文件為/etc/mosquitto/mosquitto.conf
配置如下 :
listener 1883 //配置允許外部訪問的端口設置
allow_anonymous false //配置不允許匿名訪問,需輸入賬號密碼才可訂閱或者發布
password_file /etc/mosquitto/pwfile.example //配置賬號密碼存放的路徑
2、設置用戶密碼
命令如下:
mosquitto_passwd /etc/mosquitto/pwfile.example 用戶名
回車后,按照提示輸入兩次密碼即可!
3、用戶測試驗證
啟動 mosquitto:
mosquitto -c /etc/mosquitto/mosquitto.conf
(訂閱端)客戶端啟動:
mosquitto_sub -h 地址 -t 主題 -u 用戶名 -P 密碼
(發布者)客戶端啟動:
mosquitto_pub -h 地址 -t 主題 -u 用戶名 -P 密碼 -m 發布內容
三、補充問題
編譯問題
1、fatal error: cjson/cJSON.h: No such file or directory
解決:需要安裝cJSON(這里cJSON的安裝,yum和apt不一定能找到,可以直接從github上下載源碼壓縮包,然后解壓,進入目錄,並make,make install)
啟動訂閱客戶端問題
1、error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
解決:執行以下命令:
sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
ldconfig
2、error while loading shared libraries: libcjson.so.1: cannot open shared object file: No such file or directory
解決:
方法1:
編輯/etc/ld.so.conf文件,在新的一行中加入庫文件所在目錄(/usr/local/lib);
運行ldconfig,以更新/etc/ld.so.cache文件;
方法2:
在/etc/ld.so.conf.d/目錄下新建任何以.conf為后綴的文件(touch mqtt.conf),在該文件中加入庫文件所在的目錄;
運行ldconfig,以更新/etc/ld.so.cache文件;
3、Warning: Unable to drop privileges to 'mosquitto' because this user does not exist. Trying 'nobody' instead.
解決:
在allow_anonymous 前面加一個 user root
user root
allow_anonymous false
或者創建mosquitto用戶和組
groupadd mosquitto
useradd -g mosquitto mosquitto
chown -R mosquitto:mosquitto /etc/mosquitto/
四、參考以下文章
https://www.cnblogs.com/chen1-kerr/p/7258487.html
https://www.cnblogs.com/IC1101/p/14749722.html
https://blog.csdn.net/u012377333/article/details/69397124
https://www.cnblogs.com/x_wukong/p/4722903.html
https://blog.csdn.net/houjixin/article/details/46711547