linux下MQTT订阅发布验证-mosquitto安装测试流程

本文详细介绍了,如何在linux环境搭建一个MQTT server, 并同时安装 了客户端 ,进行了mqtt消息发布、订阅验证。

mosquitto 服务端安装(ubuntu)

复制代码
#添加源
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa

sudo apt update 

# install mosquitto
sudo apt-get -y install  mosquitto



#修改配置文件
cat  /etc/mosquitto/mosquitto.conf
persistence true
persistence_location /var/lib/mosquitto/

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

include_dir /etc/mosquitto/conf.d

# start
sudo /etc/init.d/mosquitto start

mosquitto 客户端安装

复制代码
(base) WuGTech@WuGTech:~$ sudo apt-get install mosquitto-clients

验证安装效果

订阅消息

复制代码
mosquitto_sub -t 5g
#或者
mosquitto_sub -u xxx -P xxx -t 5g

#或者
mosquitto_sub -h localhost -t "5g/#" -i "client1"  -u xxx -P xxx

发布消息

复制代码
mosquitto_pub -t 5g
#或者
mosquitto_pub -u xxx -P xxx -t 5g

#或者
mosquitto_pub -h localhost -t "5g/test" -i "client2" -m "How are you?" -u xxx -P xxx

验证

复制代码
^C(base) WuGTech@WuGTech:~$ mosquitto_sub -h localhost -t "5g/#" -i "client1"  -u xxx -P xxx
How are you?

(base) WuGTech@WuGTech:~$ mosquitto_pub -h localhost -t "5g/test" -i "client3" -m "How are you?" -u xxx -P xxx
(base) WuGTech@WuGTech:~$ 

日志

复制代码
(base) WuGTech@WuGTech:~$ sudo cat /var/log/mosquitto/mosquitto.log

1750320791: New connection from ::1:40694 on port 1883.
1750320791: New client connected from ::1:40694 as client1 (p2, c1, k60, u'bruce').
1750320875: Client client1 disconnected.
1750320878: New connection from ::1:52248 on port 1883.
1750320878: New client connected from ::1:52248 as auto-0C77BCD5-EDD7-7913-D5BE-B4D5F5655632 (p2, c1, k60).
1750320895: Client auto-0C77BCD5-EDD7-7913-D5BE-B4D5F5655632 disconnected.
1750320896: New connection from ::1:36050 on port 1883.
1750320896: New client connected from ::1:36050 as auto-16E03451-D46A-55FE-BFFA-3AB710949EE2 (p2, c1, k60, u'yang').
相关推荐
狮子再回头几秒前
relhat9.1 yum无法安装问题
linux·运维·centos
暮云星影2 分钟前
全志linux开发 USB接口设置
linux·arm开发·驱动开发
江华森20 分钟前
Linux 系统实战完全指南
linux·运维·服务器
Safeploy安策数据22 分钟前
政务云加密太慢?万兆服务器密码机如何破解高并发性能瓶颈
linux·运维·github
阿泽·黑核31 分钟前
06 keyflow 多平台移植指南:STM32/51/ESP32/Linux
linux·stm32·嵌入式硬件
开开心心_Every32 分钟前
近200个工具的电脑故障修复合集
linux·运维·服务器·leetcode·智能手机·电脑·模拟退火算法
NGINX开源社区33 分钟前
NGINX Gateway Fabric 支持 Gateway API Inference Extension
linux·服务器·网络
不会C语言的男孩36 分钟前
Linux 系统编程 · 第 9 章:进程创建
linux·c语言·开发语言
babytiger39 分钟前
银河麒麟v11,apt 安装不好用了,要打开维护模式
linux·运维·服务器
Android小码家41 分钟前
andoird13 + bazel 编译 Linux kernel
linux·运维·服务器