1. 程式人生 > >CentOS7.0 mosquitto的安裝和配置

CentOS7.0 mosquitto的安裝和配置

加入yum源

在/etc/yum.repos.d/目錄中新建一個mosquitto.repo檔案,裡面寫入:

[home_oojah_mqtt]
  name=mqtt (CentOS_CentOS-7)
  type=rpm-md
  baseurl=http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/
  gpgcheck=1
  gpgkey=http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7//repodata/repomd.xml.key
  enabled=1

安裝客戶端和服務端

yum search all mosquitto

mosquitto-clients.x86_64 : Mosquitto command line publish/subscribe clients
mosquitto-debuginfo.x86_64 : Debug information for package mosquitto
libmosquitto-devel.x86_64 : MQTT C client library development files
libmosquitto1.x86_64 : MQTT C client library
libmosquittopp-devel.x
86_64 : MQTT C++ client library development files libmosquittopp1.x86_64 : MQTT C++ client library mosquitto.x86_64 : MQTT version 3.1/3.1.1 compatible message broker yum install mosquitto-clients.x86_64 yum install mosquitto.x86_64

重點安裝客戶端和服務端就可以,其餘的可以根據自己需要選擇安裝!

配置mosquitto

cd /etc/mosquitto/

重要的配置檔案為 mosquitto.conf

# Place your local configuration in /etc/mosquitto/conf.d/   
 pid_file /var/run/mosquitto.pid

 persistence true
 persistence_location /var/lib/mosquitto/

  #log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d
  • 自定義的配置檔案是放在/etc/mosquitto/conf.d/資料夾中,檔案以.conf為副檔名。詳細的配置引數參考mosquitto.conf.example
# =================================================================
  # 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
  # 自己的配置可以放到以下目錄中
  include_dir /etc/mosquitto/conf.d

啟動(兩種方式二選一)

mosquitto -c /etc/mosquitto/mosquitto.conf -d
sudo /etc/init.d/mosquitto start

測試

一、開啟另一個終端視窗,執行訂閱程式mosquitto_sub:

注意:

訊息推送的釋出和訂閱要有主題,選項[-t] 主題,即:mosquitto -t 主題

如需指定使用者名稱稱則加選項[-i] 使用者名稱,即:mosquitto_sub -t 主題 -i 訂閱端

mosquitto_sub -t mqtt

二、開啟另一個終端視窗,執行釋出程式mosquitto_pub:

指定訊息推送的主題,釋出端使用者名稱和訊息:

mosquitto_pub -t 主題 -i 釋出端 -h 主機 -m 你好

*注意:如果訊息中間有空格則訊息要用引號括起來。

mosquitto_pub -h localhost -t mqtt -m "hello world."

這時候前面那個訂閱視窗就可以收到”hello world”的訊息了。

總體來說,安裝配置中沒有遇到什麼問題,可以平滑的進行安裝和測試工作