MQTT的学习之Mosquitto简要教程(安装&使用)

本文详细介绍Mosquitto MQTT代理服务器的安装与配置过程,包括源码安装、依赖包安装及参数配置等,并解决了安装过程中可能遇到的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mosquitto是一个实现了MQTT3.1协议的代理服务器,由MQTT协议创始人之一的Andy Stanford-Clark开发,它为我们提供了非常棒的轻量级数据交换的解决方案。本文的主旨在于记录Mosquitto服务的安装和使用,以备日后查阅。

获取&安装 

在Linux系统上安装Mosquitto,本人建议大家使用源码安装模式,最新的源码可从 Index of /files/source/ 地址中获取。解压之后,我们可以在源码目录里面找到主要的配置文件config.mk,其中包含了所有Mosquitto的安装选项,详细的参数说明如下: 

# 是否支持tcpd/libwrap功能.
#WITH_WRAP:=yes

# 是否开启SSL/TLS支持
#WITH_TLS:=yes

# 是否开启TLS/PSK支持
#WITH_TLS_PSK:=yes

# Comment out to disable client client threading support.
#WITH_THREADING:=yes

# 是否使用严格的协议版本(老版本兼容会有点问题)
#WITH_STRICT_PROTOCOL:=yes

# 是否开启桥接模式
#WITH_BRIDGE:=yes

# 是否开启持久化功能
#WITH_PERSISTENCE:=yes

# 是否监控运行状态
#WITH_MEMORY_TRACKING:=yes

这里需要注意的是,默认情况下Mosquitto的安装需要OpenSSL的支持;如果不需要SSL,则需要关闭config.mk里面的某些与SSL功能有关的选项(WITH_TLS、WITH_TLS_PSK)。接着,就是运行make install进行安装,完成之后会在系统命令行里发现mosquitto、mosquitto_passwd、mosquitto_pub和mosquitto_sub四个工具(截图如下),分别用于启动代理、管理密码、发布消息和订阅消息。 

实战项目中,没有安全监测是不可能的,因此我模拟真实环境,直接去安装了OpenSSL。

编译与安装 OpenSSL

prefix 是安装目录,openssldir 是配置文件目录,另外建议安装两次,shared 作用是生成动态连接库。linux版的OpenSSL下载地址为:https://www.openssl.org/source/,我所使用的是openssl-1.1.0f.tar.gz版本,并且将mosquitto-1.4.14.tar.gz提前也上传至该目录下并解压,后面会使用。

1、上传下载好的压缩包,并解压:

2、在/home/tool下创建文件夹openssl作为安装路径,在openssl下创建files文件夹;然后,cd到openssl-1.1.0f文件夹下:

3、执行命令

1、  ./config --prefix=/home/tool/openssl --openssldir=/home/tool/openssl/files

2、  make && make install

3、  ./config shared --prefix=/home/tool/openssl --openssldir=/home/tool/openssl/files
4、 make clean  

5、 make && make  install

至此,openssl安装完毕,我以为现在可以安装mosquitto了,结果在安装的过程中会报各种各样的错误,因为mosquitto还需要依赖很多其他的报,具体错误我就不一一说出来了,因为解决这些问题我就花了差不多2个小时,下面直接把安装的命令列出来,大家对这一一执行就行了。

安装其他依赖包:

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

因为我是安装好以后才开始整理文档的,有可能会丢一些依赖包,敬请谅解!

 安装mosquitto:

 1、切换到/home/tool/sourceDir/mosquitto-1.4.14目录下

 2、make && make install

 3、安装完以后切换到 /etc 目录下查看:

4、切换到 /etc/mosquitto目录下,执行

5、mosquitto参数详细说明:

# =================================================================
# 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

5、启动mosquitto服务:

mosquitto -c /etc/mosquitto/mosquitto.conf

 6、查看端口:

7、至此,mosquitto安装,启动完毕!

 补充问题:

   1、在启动过程中报错:

 Error: Invalid user 'mosquitto'.

  解决方法:

 2、重启Mosquitto

笔者在网上找了很多资料都没有提起怎样重启服务器,并且在官网上也没有相应命令提示。

所以我们使用Liunx终止进程命令,强行退出。

先kill掉,再重启:
[root@localhost ~]# ps -A | grep mosquitto
21674 ?        00:02:38 mosquitto
[root@localhost ~]# kill -9 21674

编译过程中问题:

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

调测问题:

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

 调测步骤:

1、先开启一个客户端,模拟broker

 2、再开启一个客户端,模拟订阅

3、最后开启一个客户端,模拟发布

4、订阅界面可以实时收到发布消息

&lt;think&gt;好的,用户需要关于安装和配置MQTT服务器及客户端的指南,特别是参考了提供的引用内容。首先,我得确认用户提到的引用来源是否足够覆盖安装步骤。引用[1]提到了在Ubuntu下安装Mosquitto,引用[2]涉及ActiveMQ和Apollo,引用[3]关于启动Mosquitto服务,引用[4]则是SpringBoot集成。用户可能更倾向于开源的Mosquitto,因为引用[1]和[3]有详细步骤,而ActiveMQ在引用[2]中也有涉及,但配置可能更复杂。需要分步骤说明安装服务器和客户端,包括验证发布和订阅,可能还需要提到不同服务器的选择,比如Mosquitto和ActiveMQ的区别。另外,引用[4]提到了客户端集成,可能需要补充客户端工具的使用,比如mosquitto_pub和mosquitto_sub。用户可能是在Ubuntu环境下操作,所以步骤要针对该环境。还要注意是否需要配置安全设置,比如用户名密码,但引用中没有提到,可能需要简要提及。最后生成相关问题,覆盖服务器选择、安全配置、客户端集成和常见问题排查。要确保步骤清晰,命令准确,避免错误。&lt;/think&gt;### 安装和配置MQTT服务器及客户端的指南 #### 一、安装MQTT服务器(以Mosquitto为例) 1. **在Ubuntu中安装Mosquitto服务端** 执行以下命令安装Mosquitto及其客户端工具: ```bash sudo apt-get update sudo apt-get install mosquitto mosquitto-clients ``` 安装完成后,服务会自动启动。通过`systemctl`验证状态: ```bash sudo systemctl status mosquitto ``` 若未启动,可通过`sudo systemctl start mosquitto`手动启动[^1][^3]。 2. **配置Mosquitto(可选)** 默认配置支持基础的MQTT通信。如需修改端口或添加安全认证,编辑配置文件: ```bash sudo nano /etc/mosquitto/mosquitto.conf ``` 例如,添加用户名密码认证: ```conf allow_anonymous false password_file /etc/mosquitto/passwd ``` 生成密码文件: ```bash sudo mosquitto_passwd -c /etc/mosquitto/passwd your_username ``` --- #### 二、验证MQTT通信 1. **订阅主题** 打开一个终端,使用`mosquitto_sub`订阅主题`test/topic`: ```bash mosquitto_sub -h localhost -t &quot;test/topic&quot; -v ``` 2. **发布消息** 另开一个终端,使用`mosquitto_pub`发布消息: ```bash mosquitto_pub -h localhost -t &quot;test/topic&quot; -m &quot;Hello MQTT&quot; ``` 订阅端会立即收到消息,表明服务运行正常[^1][^3]。 --- #### 三、其他MQTT服务器选项(如ActiveMQ/Apollo) 1. **安装ActiveMQ** ActiveMQ支持MQTT协议,适合复杂场景。在Ubuntu中安装: ```bash sudo apt-get install activemq ``` 修改配置文件`activemq.xml`,确认MQTT端口(默认1883)已启用[^2]: ```xml &lt;transportConnector name=&quot;mqtt&quot; uri=&quot;mqtt://0.0.0.0:1883&quot;/&gt; ``` 2. **连接服务器** 客户端连接时需指定服务器IP和端口(如`192.168.1.62:1883`)[^2]。 --- #### 四、客户端开发集成(以SpringBoot为例) 1. **添加MQTT依赖** 在`pom.xml`中加入: ```xml &lt;dependency&gt; &lt;groupId&gt;org.springframework.integration&lt;/groupId&gt; &lt;artifactId&gt;spring-integration-mqtt&lt;/artifactId&gt; &lt;/dependency&gt; ``` 2. **配置客户端连接** 在代码中初始化发布者/订阅者并连接服务器: ```java @Bean public MqttPahoClientFactory mqttClientFactory() { DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory(); factory.setServerURIs(&quot;tcp://localhost:1883&quot;); factory.setUserName(&quot;admin&quot;); factory.setPassword(&quot;password&quot;); return factory; } ``` 启动时自动订阅指定主题[^4]。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值