Linux 安裝Mosquitto服務

创客未来發表於2024-03-13

1.安裝依賴

yum install gcc-c++ cmake openssl-devel libuuid-devel c-ares-devel uuid-devel libwebsockets-devel.x86_64 libwebsockets.x86_64 -y

2.下載mosquitto

官網:https://mosquitto.org/

cd /home
wget --no-check-certificate https://mosquitto.org/files/source/mosquitto-1.6.8.tar.gz

3.解壓 編譯 安裝

tar -zxvf mosquitto-1.6.8.tar.gz
cd mosquitto-1.6.8
make
make install

之後會碰到找不到libmosquitto.so.1這個問題,修改連結路徑,重新載入動態連線庫

ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
ldconfig

4.建立配置檔案

cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
vim /etc/mosquitto/mosquitto.conf

配置檔案中預設使用user mosquitto。如果不想建立此使用者,可以修改成root。

5. 啟動、檢視、關閉程式

# 執行程式
mosquitto -c /etc/mosquitto/mosquitto.conf -d
# ps 檢視
ps -aux | grep mosquitto
# 關閉程式
kill -9 $(pidof mosquitto)

6.設定使用者名稱密碼

# 我這裡設定的使用者名稱是 admin
mosquitto_passwd -c /etc/mosquitto/pwfile.conf admin
# 然後輸入兩遍密碼即可

注意,如果提示不存在pwfile.conf,則需要手動建立該檔案。

編輯配置檔案

vim /etc/mosquitto/mosquitto.conf

增加下面內容:

password_file /etc/mosquitto/pwfile.conf

然後重啟即可。

7.檢視版本

mosquitto -v

8.本地測試

開啟一個訂閱者

開啟一個釋出者

相同topic的雙方,釋出者pub傳送“釋出內容”給訂閱者sub。

注意:如果其他機器連線服務需要修改配置檔案中 allow_anonymous true

9.下載客戶端測試

下載地址:https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.ui.app/1.0.2/

雙擊執行paho.exe

點選“+”號建立連線。

參考地址:https://blog.csdn.net/tswc_byy/article/details/130766747

相關文章