前言
Mosquitto是一個(gè)實(shí)現(xiàn)了MQTT3.1協(xié)議的代理服務(wù)器,由MQTT協(xié)議創(chuàng)始人之一的Andy Stanford-Clark開(kāi)發(fā),它為我們提供了非常棒的輕量級(jí)數(shù)據(jù)交換的解決方案。本文的主旨在于記錄Mosquitto服務(wù)的安裝和使用,以備日后查閱
一、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的安裝選項(xiàng)
# 是否支持tcpd/libwrap功能.
#WITH_WRAP:=yes
# 是否開(kāi)啟SSL/TLS支持
#WITH_TLS:=yes
# 是否開(kāi)啟TLS/PSK支持
#WITH_TLS_PSK:=yes
# Comment out to disable client client threading support.
#WITH_THREADING:=yes
# 是否使用嚴(yán)格的協(xié)議版本(老版本兼容會(huì)有點(diǎn)問(wèn)題)
#WITH_STRICT_PROTOCOL:=yes
# 是否開(kāi)啟橋接模式
#WITH_BRIDGE:=yes
# 是否開(kāi)啟持久化功能
#WITH_PERSISTENCE:=yes
# 是否監(jiān)控運(yùn)行狀態(tài)
#WITH_MEMORY_TRACKING:=yes
這里需要注意的是,默認(rèn)情況下Mosquitto的安裝需要OpenSSL的支持;如果不需要SSL,則需要關(guān)閉config.mk里面的某些與SSL功能有關(guān)的選項(xiàng)(WITH_TLS、WITH_TLS_PSK)。接著,就是運(yùn)行
make
&
make install
進(jìn)行安裝,完成之后會(huì)在系統(tǒng)命令行里發(fā)現(xiàn)mosquitto、mosquitto_passwd、mosquitto_pub和mosquitto_sub四個(gè)工具,分別用于啟動(dòng)代理、管理密碼、發(fā)布消息和訂閱消息。
mosquitto mosquitto_passwd mosquitto_pub mosquitto_sub
3. 編譯mosquitto可能出現(xiàn)的問(wèn)題及解決方法
3.1、編譯工具缺失
報(bào)錯(cuò)信息:make、gcc工具缺失
解決方法:安裝工具:apt-get install build-essential
3.2、openssl/ssl.h 錯(cuò)誤
報(bào)錯(cuò)信息:fatal error: openssl/ssl.h: No such file or directory
解決方法:
情況1:檢查是否安裝openssl,一般VPS都已經(jīng)安裝好了的,未安裝則對(duì)其進(jìn)行安裝:
apt-get install openssl
安裝完畢后執(zhí)行openssl,若可以進(jìn)入oepnssl模式,如下圖
1.png
情況2:已安裝oepnssl,則先查看/usr/local/include和/usr/include目錄,看是否有openssl文件夾,若沒(méi)有,需安裝libssl-dev: apt-get install libssl-dev
3.3、ares 錯(cuò)誤
報(bào)錯(cuò)信息: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 錯(cuò)誤
報(bào)錯(cuò)信息: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
此時(shí)可輸入mosquitto直接運(yùn)行,此時(shí)運(yùn)行的是默認(rèn)配置文件(也可自定義配置文件運(yùn)行)
6. 定義配置文件
進(jìn)入 /etc/mosquitto 目錄,新建配置文件
touch mosquitto.conf
# =================================================================
# General configuration
# =================================================================
# 客戶端心跳的間隔時(shí)間
#retry_interval 20
# 系統(tǒng)狀態(tài)的刷新時(shí)間
#sys_interval 10
# 系統(tǒng)資源的回收時(shí)間,0表示盡快處理
#store_clean_interval 10
# 服務(wù)進(jìn)程的PID
#pid_file /var/run/mosquitto.pid
# 服務(wù)進(jìn)程的系統(tǒng)用戶
#user mosquitto
# 客戶端心跳消息的最大并發(fā)數(shù)
#max_inflight_messages 10
# 客戶端心跳消息緩存隊(duì)列
#max_queued_messages 100
# 用于設(shè)置客戶端長(zhǎng)連接的過(guò)期時(shí)間,默認(rèn)永不過(guò)期
#persistent_client_expiration
# =================================================================
# Default listener
# =================================================================
# 服務(wù)綁定的IP地址
#bind_address
# 服務(wù)綁定的端口號(hào)
#port 1883
# 允許的最大連接數(shù),-1表示沒(méi)有限制
#max_connections -1
# cafile:CA證書(shū)文件
# capath:CA證書(shū)目錄
# certfile:PEM證書(shū)文件
# keyfile:PEM密鑰文件
#cafile
#capath
#certfile
#keyfile
# 必須提供證書(shū)以保證數(shù)據(jù)安全性
#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
# =================================================================
# 消息自動(dòng)保存的間隔時(shí)間
#autosave_interval 1800
# 消息自動(dòng)保存功能的開(kāi)關(guān)
#autosave_on_changes false
# 持久化功能的開(kāi)關(guān)
persistence true
# 持久化DB文件
#persistence_file mosquitto.db
# 持久化DB文件目錄
#persistence_location /var/lib/mosquitto/
# =================================================================
# Logging
# =================================================================
# 4種日志模式:stdout、stderr、syslog、topic
# none 則表示不記日志,此配置可以提升些許性能
log_dest none
# 選擇日志的級(jí)別(可設(shè)置多項(xiàng))
#log_type error
#log_type warning
#log_type notice
#log_type information
# 是否記錄客戶端連接信息
#connection_messages true
# 是否記錄日志時(shí)間
#log_timestamp true
# =================================================================
# Security
# =================================================================
# 客戶端ID的前綴限制,可用于保證安全性
#clientid_prefixes
# 允許匿名用戶
#allow_anonymous true
# 用戶/密碼文件,默認(rèn)格式:username:password
#password_file
# PSK格式密碼文件,默認(rèn)格式:identity:key
#psk_file
# pattern write sensor/%u/data
# ACL權(quán)限配置,常用語(yǔ)法如下:
# 用戶限制:user <username>
# 話題限制:topic [read|write] <topic>
# 正則限制:pattern write sensor/%u/data
#acl_file
# =================================================================
# Bridges
# =================================================================
# 允許服務(wù)之間使用“橋接”模式(可用于分布式部署)
#connection <name>
#address <host>[:<port>]
#topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]
# 設(shè)置橋接的客戶端ID
#clientid
# 橋接斷開(kāi)時(shí),是否清除遠(yuǎn)程服務(wù)器中的消息
#cleansession false
# 是否發(fā)布橋接的狀態(tài)信息
#notifications true
# 設(shè)置橋接模式下,消息將會(huì)發(fā)布到的話題地址
# $SYS/broker/connection/<clientid>/state
#notification_topic
# 設(shè)置橋接的keepalive數(shù)值
#keepalive_interval 60
# 橋接模式,目前有三種:automatic、lazy、once
#start_type automatic
# 橋接模式automatic的超時(shí)時(shí)間
#restart_timeout 30
# 橋接模式lazy的超時(shí)時(shí)間
#idle_timeout 60
# 橋接客戶端的用戶名
#username
# 橋接客戶端的密碼
#password
# bridge_cafile:橋接客戶端的CA證書(shū)文件
# bridge_capath:橋接客戶端的CA證書(shū)目錄
# bridge_certfile:橋接客戶端的PEM證書(shū)文件
# bridge_keyfile:橋接客戶端的PEM密鑰文件
#bridge_cafile
#bridge_capath
#bridge_certfile
#bridge_keyfile
修改配置文件的幾項(xiàng)內(nèi)容
# 允許匿名用戶(設(shè)置為不允許匿名登錄)
allow_anonymous false
# 用戶/密碼文件,默認(rèn)格式:username:password
password_file /etc/mosquitto/pwfile.conf
此設(shè)置,連接mosquitto時(shí),必須輸入用戶名密碼
在 /etc/mosquitto目錄下新建pwfile.conf文件
mosquitto_passwd /etc/mosquitto/pwfile.conf 用戶名
此時(shí),會(huì)讓連續(xù)輸入兩次密碼,輸入完成后,建立了一個(gè)用戶,用
vi pwfile.conf
如下圖:
我的此時(shí)實(shí)有三個(gè)用戶,admin、user、user1
7. 使用自定義的配置文件運(yùn)行mosquitto
mosquitto -c /etc/mosquitto/mosquitto.conf -d
運(yùn)行起來(lái)后,可用客戶端使用用戶名、密碼鏈接
后續(xù)
在配置文件中定義log日志 log_dest 后要加file參數(shù)
log_dest file /var/log/mosquitto/mosquitto.log
此種情況必須先創(chuàng)建mosquitto.log文件,并且
chmod -R 777 mosquitto
修改mosquitto權(quán)限目錄權(quán)限,這樣日志才能寫(xiě)進(jìn)去