1. 程式人生 > 其它 >MQTT伺服器搭建——Liunx安裝mosquitto,並設定使用者密碼

MQTT伺服器搭建——Liunx安裝mosquitto,並設定使用者密碼

一、安裝

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 -d -c /etc/mosquitto/mosquitto.conf

6、關閉,kill程序

ps -A | 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檔案,在新的一行中加入庫檔案所在目錄;
執行ldconfig,以更新/etc/ld.so.cache檔案;

方法2:
在/etc/ld.so.conf.d/目錄下新建任何以.conf為字尾的檔案,在該檔案中加入庫檔案所在的目錄;
執行ldconfig,以更新/etc/ld.so.cache檔案;

四、參考以下文章

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