MQTT入門1 -- mosquitto 安裝


原文鏈接:https://www.cnblogs.com/NickQ/p/9247638.html

MQTT入門1 -- mosquitto 安裝

簡介:
MQTT(Message Queuing Telemetry Transport,消息隊列遙測傳輸)是IBM開發的一個即時通訊協議,有可能成為物聯網的重要組成部分。
MQTT服務器有好多種,mosquitto 是其中之一。由於mosquitto相對來說,教程比較多,對於初學者更容易入手。因此,我選擇了mosquitto。
Mosquitto是一個實現了MQTT3.1協議的代理服務器,由MQTT協議創始人之一的Andy Stanford-Clark開發,它為我們提供了非常棒的輕量級數據交換的解決方案。本文的主旨在於記錄Mosquitto服務的安裝和使用,以備日后查閱。

安裝依賴包

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

下載源代碼:https://mosquitto.org/files/source/

解壓,編譯,安裝

[nick@XQLY mqtt]$ tar -zxvf ./mosquitto-1.4.15.tar.gz 

[nick@XQLY mqtt]$ sudo make && make install
 

創建配置文件

[nick@XQLY mqtt]$ cd /etc/mosquitto/
[nick@XQLY mosquitto]$ ls
aclfile.example  mosquitto.conf.example pskfile.example  pwfile.example
[nick@XQLY mosquitto]$ cp ./mosquitto.conf.example  ./mosquitto.conf

修改配置文件mosquitto.conf 部分內容

# When run as root, drop privileges to this user and its primary
# group.
# Leave blank to stay as root, but this is not recommended.
# If run as a non-root user, this setting has no effect.
# Note that on Windows this has no effect and so mosquitto should
# be started by the user you wish it to run as.
user mosquitto


# Boolean value that determines whether clients that connect
# without providing a username are allowed to connect. If set to
# false then a password file should be created (see the
# password_file option) to control authenticated client access.
# Defaults to true.
allow_anonymous false


# See the TLS client require_certificate and use_identity_as_username options
# for alternative authentication options.
password_file /etc/mosquitto/password_file



配置文件說明
# =================================================================
# 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

創建用戶/密碼

在mosquitto.conf中指明了,用戶密碼文件路徑,在路徑下創建密碼文件;
格式:  username:password

使用配置文件啟動服務

[nick@XQLY mosquitto]$ mosquitto -c /etc/mosquitto/mosquitto.conf 

查看服務啟動狀態

[nick@XQLY ~]$ ps -A | grep mosquitto

查看服務啟動狀態

[nick@XQLY ~]$ ss -tanl

補充問題:

1、在啟動過程中報錯:

Error: Invalid user 'mosquitto'.

解決方法:

解決辦法

2、重啟Mosquitto

使用Liunx終止進程命令,強行退出。 先kill掉,再重啟:

3、編譯過程中問題:

1. ssh.h找不到。我之前安裝了openssl,為什么還會報這個錯誤呢,因為我安裝好了以后並沒有配置環境變量      下面的命令是安裝開發環境,執行此命令,上面的openssl就可以不安裝了

yum install openssl-devel

2.ares.h找不到

yum install c-ares-devel

3.#include <uuid/uuid.h> 找不到文件解決方法:

yum install e2fsprogs-devel
yum install uuid-devel
yum install libuuid-devel

4、 調測問題:

1、報錯./mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory

解決:執行以下命令:

建立軟鏈接
sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1

刷新鏈接
ldconfig 

調試

開啟服務

[nick@XQLY mosquitto]$ mosquitto -c /etc/mosquitto/mosquitto.conf 
1530344877: mosquitto version 1.4.15 (build date 2018-06-30 14:03:02+0800) starting
1530344877: Config loaded from /etc/mosquitto/mosquitto.conf.
1530344877: Opening ipv4 listen socket on port 1883.
1530344877: Opening ipv6 listen socket on port 1883.

建立一個客戶端,訂閱主題(用戶名nick 密碼zhangshan)

[nick@XQLY mosquitto]$ mosquitto_sub -u nick -P zhangshan -t mass

建立一個客戶端,向mass主題發送一個消息

[nick@XQLY ~]$ mosquitto_pub -t mass -u nick -P zhangshan -m "this is a messeage"

服務器顯示

[nick@XQLY mosquitto]$ mosquitto -c /etc/mosquitto/mosquitto.conf 
1530344877: mosquitto version 1.4.15 (build date 2018-06-30 14:03:02+0800) starting
1530344877: Config loaded from /etc/mosquitto/mosquitto.conf.
1530344877: Opening ipv4 listen socket on port 1883.
1530344877: Opening ipv6 listen socket on port 1883.
1530344892: New connection from ::1 on port 1883.
1530344892: New client connected from ::1 as mosqsub|9252-XQLY (c1, k60, u'nick').
1530344900: New connection from ::1 on port 1883.
1530344900: New client connected from ::1 as mosqpub|9254-XQLY (c1, k60, u'nick').
1530344900: Client mosqpub|9254-XQLY disconnected.

訂閱者顯示

[nick@XQLY mosquitto]$ mosquitto_sub -u nick -P zhangshan -t mass
this is a messeage

本文參考:
https://blog.csdn.net/qq_29350001/article/details/76680646
https://www.cnblogs.com/chen1-kerr/p/7258487.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM