1.源碼的獲取:http://mosquitto.org/files/source/
2.直接解壓tar文件,就可以得到所有的源碼,里面有個配置文件config.mk,這個文件包含了Mosquitto安裝選項。
3.編譯:
在根目錄下執行make,報如下錯誤
因為默認情況下Mosquitto的安裝需要OpenSSL的支持;如果不需要SSL,則需要關閉config.mk里面的某些與SSL功能有關的選項(WITH_TLS、WITH_TLS_PSK)。
再次編譯,又報如下錯誤:
handle_connect.c:33:25: fatal error: uuid/uuid.h: No such file or directory
需要安裝uuid-dev:
sudo apt-get install uuid-dev
再次編譯ok,在編譯目錄會看到幾個文件:
位置分別在:
以及:
b.安裝:
sudo make install
就會把mosquitto安裝到/etc/目錄下:
進入這個目錄看看:
文件mosquitto.conf.example就是一個參數例子,如果使用這個例子參數,直接將其拷貝成mosquitto.conf
sudo cp mosquitto.conf.example mosquitto.conf
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
4.啟動mosquitto:
在阿里雲服務器上遇到以下問題:
解決方法:
之后再次啟動就沒問題了:
查看下端口:
至此為止,mosquitto編譯安裝,並且成功啟動。
5.重啟:
ps的參數:
-a 顯示所有終端機下執行的進程,除了階段作業領導者之外。
-A 顯示所有進程
6.測試:
a.先開啟一個客戶端,模擬broker
kent@ubuntu:~$ mosquitto -c /etc/mosquitto/mosquitto.conf -v 1553418333: mosquitto version 1.5.8 starting 1553418333: Config loaded from /etc/mosquitto/mosquitto.conf. 1553418333: Opening ipv4 listen socket on port 1883. 1553418333: Opening ipv6 listen socket on port 1883.
b.再開啟一個客戶端,模擬訂閱
kent@ubuntu:~$ mosquitto_sub -v -t topic111 mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
出現了錯誤,就是找不到這個動態庫,看看它是去哪里找的:
建立一個軟連接即可:
kent@ubuntu:~$ sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1 kent@ubuntu:~$ ldconfig
再次創建一個客戶端,模擬訂閱者:
kent@ubuntu:~$ mosquitto_sub -v -t topic111
在broker那一側也有對應的信息輸出:
測試下收發數據: