安裝版本:mosquitto1.4.4
mosquitto官網(wǎng):http://mosquitto.org/
關(guān)于mqtt協(xié)議可參考:http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html
系統(tǒng)版本: centos 6.7
1. 安裝依賴(lài)包
yum install gcc gcc-c++ libstdc++-devel
yum install cmake
yum install openssl-devel //mosquitto默認(rèn)支持openssl
2. 下載并解壓mosquitto
mkdir /usr/local/mqtt
cd /usr/local/mqtt
wget http://mosquitto.org/files/source/mosquitto-1.4.4.tar.gz
tar -xzvf mosquitto-1.4.4.tar.gz
cd mosquitto-1.4.4
3. 修改配置文件
config.mk包括了多個(gè)選項(xiàng), 可按需關(guān)閉或開(kāi)啟,但一旦開(kāi)啟則需要先安裝對(duì)應(yīng)的模塊
vim config.mk
| 選項(xiàng) | 說(shuō)明 | make時(shí)的出錯(cuò)信息 |
|---|---|---|
| WITH_SRV | 啟用c-areas庫(kù)的支持,一個(gè)支持異步DNS查找的庫(kù),見(jiàn)http://c-ares.haxx.se | missing ares.h |
| WITH_UUID | 啟用lib-uuid支持,支持為每個(gè)連接的客戶(hù)端生成唯一的uuid | missing uuid.h |
| WITH_WEBSOCKETS | 啟用websocket支持,需安裝libwebsockets,對(duì)于需要使用websocket協(xié)議的應(yīng)用開(kāi)啟 | missing libwebsockets.h |
- 安裝c-areas
yum install c-ares-devel -y
- 安裝lib-uuid
yum install uuid-devel -y
yum install libuuid-devel -y
- 安裝libwebsockets
yum install openssl-devel -y
注意:若遇到以上模塊無(wú)法安裝的情況,可將對(duì)應(yīng)模塊選項(xiàng)關(guān)閉即可,但相應(yīng)功能也將無(wú)法提供
4. 開(kāi)始安裝
- make
make install
程序文件將默認(rèn)安裝到以下位置
| 路徑 | 程序文件 |
|---|---|
| /usr/local/sbin | mosquiotto server |
| /etc/mosquitto | configuration |
| /usr/local/bin | utility command |
- 修正鏈接庫(kù)路徑
由于操作系統(tǒng)版本及架構(gòu)原因,很容易出現(xiàn)安裝之后的鏈接庫(kù)無(wú)法被找到,如啟動(dòng)mosquitto客戶(hù)端可能出現(xiàn)找不到libmosquitto.so.1文件,因此需要添加鏈接庫(kù)路徑
vim /etc/ld.so.conf.d/liblocal.conf
在文件中添加以下內(nèi)容:
/usr/local/lib64
/usr/local/lib
刷新
ldconfig
5. 啟動(dòng)
- mosquitto默認(rèn)以mosquitto用戶(hù)啟動(dòng)(可以通過(guò)配置文件修改),需添加用戶(hù)
groupadd mosquitto
useradd -g mosquitto mosquitto
- 修改配置文件
mv /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
# =================================================================
# General configuration
# =================================================================
# 客戶(hù)端心跳的間隔時(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)用戶(hù)
#user mosquitto
# 客戶(hù)端心跳消息的最大并發(fā)數(shù)
#max_inflight_messages 10
# 客戶(hù)端心跳消息緩存隊(duì)列
#max_queued_messages 100
# 用于設(shè)置客戶(hù)端長(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
# 是否記錄客戶(hù)端連接信息
#connection_messages true
# 是否記錄日志時(shí)間
#log_timestamp true
# =================================================================
# Security
# =================================================================
# 客戶(hù)端ID的前綴限制,可用于保證安全性
#clientid_prefixes
# 允許匿名用戶(hù)
#allow_anonymous true
# 用戶(hù)/密碼文件,默認(rèn)格式:username:password
#password_file
# PSK格式密碼文件,默認(rèn)格式:identity:key
#psk_file
# pattern write sensor/%u/data
# ACL權(quán)限配置,常用語(yǔ)法如下:
# 用戶(hù)限制:user <username>
# 話(huà)題限制: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è)置橋接的客戶(hù)端ID
#clientid
# 橋接斷開(kāi)時(shí),是否清除遠(yuǎn)程服務(wù)器中的消息
#cleansession false
# 是否發(fā)布橋接的狀態(tài)信息
#notifications true
# 設(shè)置橋接模式下,消息將會(huì)發(fā)布到的話(huà)題地址
# $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
# 橋接客戶(hù)端的用戶(hù)名
#username
# 橋接客戶(hù)端的密碼
#password
# bridge_cafile:橋接客戶(hù)端的CA證書(shū)文件
# bridge_capath:橋接客戶(hù)端的CA證書(shū)目錄
# bridge_certfile:橋接客戶(hù)端的PEM證書(shū)文件
# bridge_keyfile:橋接客戶(hù)端的PEM密鑰文件
#bridge_cafile
#bridge_capath
#bridge_certfile
#bridge_keyfile
- 設(shè)置用戶(hù)名和密碼
將配置文件中#allow_anonymous true去掉注釋?zhuān)O(shè)置為false,#password_file去掉注釋并添加密碼文件保存的位置
allow_anonymous false
password_file /etc/mosquitto/pwfile.example
mosquitto_passwd -c /etc/mosquitto/pwfile.example 用戶(hù)名
之后需輸入兩次密碼
注意如果想添加用戶(hù)
mosquitto_passwd -b /etc/mosquitto/pwfile.example 用戶(hù)名 密碼
本文檔配置完成后,會(huì)在linux新建一個(gè)mosquitto用戶(hù),然后mqtt服務(wù)再新建一個(gè)”用戶(hù)名”賬戶(hù)
啟動(dòng)
mosquitto -c /etc/mosquitto/mosquitto.conf -d
測(cè)試
- 成功將啟動(dòng)1883端口監(jiān)聽(tīng)
- 新建兩個(gè)shell端口A/B
A 訂閱主題: mosquitto_sub -t 主題名 -h 主機(jī)IP -u 用戶(hù)名 -P 密碼
如:mosquitto_sub -t topic -h localhost -u bjfl -P BjFulei!@#$
B 推送消息: mosquitto_pub -t主題名-h主機(jī)IP -m "消息內(nèi)容" -u用戶(hù)名 -P 密碼
如:mosquitto_pub -t location -h localhost -m "你好,mqtt" -u bjfl -P BjFulei!@#$ - 可以在A窗口看到由B推送的消息,此外服務(wù)端窗口也可以看到客戶(hù)端連接和端口的日志
1443083396: New client connected from 127.0.0.1 as mosqpub/31924-iZ94eb8yq (c1, k60).
1443083396: Client mosqpub/31924-iZ94eb8yq disconnected.
注意:輸入密碼時(shí)特殊字符需要用轉(zhuǎn)義符“\”
問(wèn)題匯總
- 啟動(dòng)mosquitto報(bào)錯(cuò)
error while loading shared libraries: libwebsockets.so.4.0.0: cannot open shared object file: No such file or directory
或者
error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
解決方法
找不到鏈接庫(kù),通過(guò)locate或find命令找到libwebsockets.so.4.0.0,將其目錄添加至ldconfg配置中:
vim /etc/ld.so.conf.d/liblocal.conf
添加如下內(nèi)容
/usr/local/lib64
/usr/local/lib
ldconfig - 編譯找不到openssl/ssl.h
解決方法
yum install openssl-devel - 編譯報(bào)錯(cuò)
mosquitto.c:871: error: ‘struct mosquitto’ has no member named ‘a(chǎn)chan’
找不到areas.h
解決方法
安裝 c-areas模塊(見(jiàn)上文)或?qū)onfig.mk中WITH_SRV選項(xiàng)關(guān)閉 - make test 提示不支持協(xié)議
Address family not supported by protocol
一般是指所訪(fǎng)問(wèn)的地址類(lèi)型不被支持,比如IPV6,忽略該錯(cuò)誤即可