必備庫和工具:gcc gcc-c++ openssl-devel c-ares-devel libuuid-devel wget cmake。(centOS用的在線安裝工具是yum,而非apt-get) 安裝源碼:mosquitto-1.4.11.tar.gz 安裝過程: 一、下載mqtt,我用的是 mosquitto-1.4.11.tar.gz, 解壓縮:# tar zxfv mosquitto-1.4.11.tar.gz 二、修改config.mk文件以使後面編譯的mosquitto文件支持websocket。c++
cd /mosquitto-1.4.11
找到mosquitto-1.4.11目錄下的config.mk文件, 把config.mk 文件中的 WITH_WEBSOCKETS:=no 改成yes 三、保存後,執行web
make && make install
ln -s /usr/local/lib/libwebsockets.so.6 /usr/lib64/libwebsockets.so.6
groupadd mosquitto
useradd -g mosquitto mosquitto
四、建立mosquitto.conf、pwfile文件算法
cd /etc/mosquitto/
若是該目錄下沒有mosquitto.conf 和 pwfile , 五、執行緩存
cp mosquitto.conf.example mosquitto.conf
cp pwfile.example pwfile
而後修改 文件mosquitto.conf , 並在文件最後加入 六、啓動 mqtt安全
mosquitto -c /etc/mosquitto/mosquitto.conf
###################################### mosquitto.conf配置文件解析服務器
=================================================================
General configuration
=================================================================
客戶端心跳的間隔時間
#retry_interval 20websocket
系統狀態的刷新時間
#sys_interval 10session
系統資源的回收時間,0表示儘快處理
#store_clean_interval 10併發
服務進程的PID
#pid_file /var/run/mosquitto.pidsocket
服務進程的系統用戶
#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
本身的配置能夠放到如下目錄中
include_dir /etc/mosquitto/conf.d
出現的問題: (1)沒有arec.h sudo yum install c-ares-devel (2)沒有uuid.h sudo yum install e2fsprogs-devel sudo yum install uuid-devel sudo yum install libuuid-devel (3)使用過程當中找不到libmosquitto.so.1 error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory 【解決方法】——修改libmosquitto.so位置
建立連接
sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
更新動態連接庫
sudo ldconfig