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').
相关推荐
Jay Chou why did几秒前
程序启动地址0x80000000
linux
落笔映浮华丶37 分钟前
c程序的翻译过程 linux版
linux·c语言
阮松云37 分钟前
code-server 配置maven
java·linux·maven
Pomelo_刘金1 小时前
Linux I/O 方式进化史(内核/性能视角):从“睡死”到“就绪队列”再到“完成队列”
linux
提伯斯6461 小时前
解决 PX4 + ROS px4ctrl 「No odom!」自动起飞失败问题
linux·ros·px4·fastlio·mid360·egoplanner
牛奶咖啡131 小时前
shell脚本编程(八)
linux·shell脚本编程·while循环语句·计数器控制的while循环·标志控制的while循环·until循环·select循环菜单
Q16849645152 小时前
知识点-创建、查看和编辑文本文件
linux·运维
小宇的天下2 小时前
Calibre 3Dstack --每日一个命令days11【dangling_ports】(3-11)
linux·运维·服务器
HIT_Weston2 小时前
97、【Ubuntu】【Hugo】搭建私人博客:搜索功能(二)
linux·运维·ubuntu
chen_mangoo2 小时前
HDMI简介
android·linux·驱动开发·单片机·嵌入式硬件