mqtt的使用以及 Linux 下安裝mosquitto
前言
Mosquitto是一個實現了MQTT3.1協議的代理伺服器,由MQTT協議創始人之一的Andy Stanford-Clark開發,它為我們提供了非常棒的輕量級資料交換的解決方案。本文的主旨在於記錄Mosquitto服務的安裝和使用,以備日後查閱
一、Linux安裝mosquitto
1. Mosquitto原始碼下載並解壓
下載
wget http://mosquitto.org/files/source/mosquitto-1.4.9.tar.gz
解壓
tar zxfv mosquitto-1.4.9.tar.gz
2. 在原始碼目錄裡面找到主要的配置檔案config.mk,其中包含了所有Mosquitto的安裝選項
# 是否支援tcpd/libwrap功能. #WITH_WRAP:=yes # 是否開啟SSL/TLS支援 #WITH_TLS:=yes # 是否開啟TLS/PSK支援 #WITH_TLS_PSK:=yes # Comment out to disable client client threading support. #WITH_THREADING:=yes # 是否使用嚴格的協議版本(老版本相容會有點問題) #WITH_STRICT_PROTOCOL:=yes # 是否開啟橋接模式 #WITH_BRIDGE:=yes # 是否開啟持久化功能 #WITH_PERSISTENCE:=yes # 是否監控執行狀態 #WITH_MEMORY_TRACKING:=yes
這裡需要注意的是,預設情況下Mosquitto的安裝需要OpenSSL的支援;如果不需要SSL,則需要關閉config.mk裡面的某些與SSL功能有關的選項(WITH_TLS、WITH_TLS_PSK)。接著,就是執行
make
&
make install
進行安裝,完成之後會在系統命令列裡發現mosquitto、mosquitto_passwd、mosquitto_pub和mosquitto_sub四個工具,分別用於啟動代理、管理密碼、釋出訊息和訂閱訊息。
mosquitto mosquitto_passwd mosquitto_pub mosquitto_sub
3. 編譯mosquitto可能出現的問題及解決方法
3.1、編譯工具缺失
報錯資訊:make、gcc工具缺失
解決方法:安裝工具:apt-get install build-essential
3.2、openssl/ssl.h 錯誤
報錯資訊:fatal error: openssl/ssl.h: No such file or directory
解決方法:
情況1:檢查是否安裝openssl,一般VPS都已經安裝好了的,未安裝則對其進行安裝:
apt-get install openssl
安裝完畢後執行openssl,若可以進入oepnssl模式,如下圖

1.png
情況2:已安裝oepnssl,則先檢視/usr/local/include和/usr/include目錄,看是否有openssl資料夾,若沒有,需安裝libssl-dev: apt-get install libssl-dev
3.3、ares 錯誤
報錯資訊:fatal error:ares.h: No such file or directory
解決方法:
方法一:修改mosquitto目錄下config.mk檔案,將WITH_SRV=yes改為WITH_SRV=no。
方法二:安裝libc-ares-dev:apt-get install libc-ares-dev
3.4、uuid 錯誤
報錯資訊:fatal error: uuid/uuid.h: No such file or directory
解決方法:安裝uuid-dev:apt-get install uuid-dev
4. 安裝其他依賴包
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
5. 安裝完後目錄
mosquitto:/usr/local/sbin configuration: /etc/mosquitto utility command: /usr/local/bin
此時可輸入mosquitto直接執行,此時執行的是預設配置檔案(也可自定義配置檔案執行)
6. 定義配置檔案
進入 /etc/mosquitto 目錄,新建配置檔案
touch mosquitto.conf
# ================================================================= # General configuration # ================================================================= # 客戶端心跳的間隔時間 #retry_interval 20 # 系統狀態的重新整理時間 #sys_interval 10 # 系統資源的回收時間,0表示儘快處理 #store_clean_interval 10 # 服務程序的PID #pid_file /var/run/mosquitto.pid # 服務程序的系統使用者 #user mosquitto # 客戶端心跳訊息的最大併發數 #max_inflight_messages 10 # 客戶端心跳訊息快取佇列 #max_queued_messages 100 # 用於設定客戶端長連線的過期時間,預設永不過期 #persistent_client_expiration # ================================================================= # Default listener # ================================================================= # 服務繫結的IP地址 #bind_address # 服務繫結的埠號 #port 1883 # 允許的最大連線數,-1表示沒有限制 #max_connections -1 # cafile:CA證書檔案 # capath:CA證書目錄 # certfile:PEM證書檔案 # keyfile:PEM金鑰檔案 #cafile #capath #certfile #keyfile # 必須提供證書以保證資料安全性 #require_certificate false # 若require_certificate值為true,use_identity_as_username也必須為true #use_identity_as_username false # 啟用PSK(Pre-shared-key)支援 #psk_hint # SSL/TSL加密演算法,可以使用“openssl ciphers”命令獲取 # as the output of that command. #ciphers # ================================================================= # Persistence # ================================================================= # 訊息自動儲存的間隔時間 #autosave_interval 1800 # 訊息自動儲存功能的開關 #autosave_on_changes false # 持久化功能的開關 persistence true # 持久化DB檔案 #persistence_file mosquitto.db # 持久化DB檔案目錄 #persistence_location /var/lib/mosquitto/ # ================================================================= # Logging # ================================================================= # 4種日誌模式:stdout、stderr、syslog、topic # none 則表示不記日誌,此配置可以提升些許效能 log_dest none # 選擇日誌的級別(可設定多項) #log_type error #log_type warning #log_type notice #log_type information # 是否記錄客戶端連線資訊 #connection_messages true # 是否記錄日誌時間 #log_timestamp true # ================================================================= # Security # ================================================================= # 客戶端ID的字首限制,可用於保證安全性 #clientid_prefixes # 允許匿名使用者 #allow_anonymous true # 使用者/密碼檔案,預設格式:username:password #password_file # PSK格式密碼檔案,預設格式:identity:key #psk_file # pattern write sensor/%u/data # ACL許可權配置,常用語法如下: # 使用者限制:user <username> # 話題限制:topic [read|write] <topic> # 正則限制:pattern write sensor/%u/data #acl_file # ================================================================= # Bridges # ================================================================= # 允許服務之間使用“橋接”模式(可用於分散式部署) #connection <name> #address <host>[:<port>] #topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix] # 設定橋接的客戶端ID #clientid # 橋接斷開時,是否清除遠端伺服器中的訊息 #cleansession false # 是否釋出橋接的狀態資訊 #notifications true # 設定橋接模式下,訊息將會發布到的話題地址 # $SYS/broker/connection/<clientid>/state #notification_topic # 設定橋接的keepalive數值 #keepalive_interval 60 # 橋接模式,目前有三種:automatic、lazy、once #start_type automatic # 橋接模式automatic的超時時間 #restart_timeout 30 # 橋接模式lazy的超時時間 #idle_timeout 60 # 橋接客戶端的使用者名稱 #username # 橋接客戶端的密碼 #password # bridge_cafile:橋接客戶端的CA證書檔案 # bridge_capath:橋接客戶端的CA證書目錄 # bridge_certfile:橋接客戶端的PEM證書檔案 # bridge_keyfile:橋接客戶端的PEM金鑰檔案 #bridge_cafile #bridge_capath #bridge_certfile #bridge_keyfile
修改配置檔案的幾項內容
# 允許匿名使用者(設定為不允許匿名登入) allow_anonymous false # 使用者/密碼檔案,預設格式:username:password password_file /etc/mosquitto/pwfile.conf
此設定,連線mosquitto時,必須輸入使用者名稱密碼
在 /etc/mosquitto目錄下新建pwfile.conf檔案
mosquitto_passwd /etc/mosquitto/pwfile.conf 使用者名稱
此時,會讓連續輸入兩次密碼,輸入完成後,建立了一個使用者,用
vi pwfile.conf
如下圖:

2.png
我的此時實有三個使用者,admin、user、user1
7. 使用自定義的配置檔案執行mosquitto
mosquitto -c /etc/mosquitto/mosquitto.conf -d
執行起來後,可用客戶端使用使用者名稱、密碼連結
後續
在配置檔案中定義log日誌 log_dest 後要加file引數
log_dest file /var/log/mosquitto/mosquitto.log
此種情況必須先建立mosquitto.log檔案,並且
chmod -R 777 mosquitto
修改mosquitto許可權目錄許可權,這樣日誌才能寫進去