【linux环境搭建】MQTT服务器搭建——mosquitto

参考文章:https://blog.csdn.net/weixin_43673603/article/details/106761120

下载

下载mosquitto源码:
wget https://mosquitto.org/files/source/mosquitto-1.6.10.tar.gz
在这里插入图片描述

安装

解压
sudo apt install g++
tar zxvf mosquitto-1.6.10.tar.gz
cd mosquitto-1.6.10
sudo make &&make install
在这里插入图片描述
在这里插入图片描述

依赖库

sudo apt-get install libssl-dev
sudo apt-get install libc-ares-dev
sudo apt-get install uuid-dev

文件介绍

mosquitto:  /usr/local/sbin
root@VM-16-17-ubuntu:/usr/local/sbin# tree
.
└── mosquitto

configuration: /etc/mosquitto
root@VM-16-17-ubuntu:/etc/mosquitto# tree
.
├── aclfile.example
├── ca_certificates
│   └── README
├── certs
│   └── README
├── conf.d
│   ├── my.conf
│   └── README
├── mosquitto.conf
├── mosquitto.conf.example
├── pskfile.example
├── pwfile
└── pwfile.example


utility command: /usr/local/bin
root@VM-16-17-ubuntu:/usr/local/bin# tree
.
├── forever -> ../lib/node_modules/forever/bin/forever
├── hb
├── jsonschema
├── mosquitto_passwd
├── mosquitto_pub
├── mosquitto_rr
├── mosquitto_sub
└── npx -> ../lib/node_modules/npm/bin/npx-cli.js

查看状态信息

查看端口

sudo service mosquitto start
lsof -i:1883
在这里插入图片描述

添加用户

mosquitto_passwd -c /etc/mosquitto/pwfile temp

temp为用户名
-c为覆盖添加,追加则不需要输入-c

在这里插入图片描述

启动服务

方法1: mosquitto -v
这个可以直接用窗口查看log
在这里插入图片描述

方法2:sudo service mosquitto start
在这里插入图片描述

这个就是在后台运行了,想要查看log可以通过sudo service mosquitto status查看信息。
查看/etc/mosquitto/mosquitto.conf
可以知道log存放在/var/log/mosquitto/mosquitto.log
在这里插入图片描述

PS:两种方法使用一个即可,方法1如果按了ctrl+c就会自动关闭,方法2会在后台运行,所以如果遇到了Error: Address already in use,大概率是已经用了方法2,又执行了方法1

在这里插入图片描述

腾讯云防火墙设置

使用腾讯云比较方便的一点就是可以使用公网ip。
在这里插入图片描述

问题

我使用在线网址测试遇到如下两个问题,但是未解决。
Q1:报 Client disconnected due to protocol error.
Q2:Connect failed: AMQJS0007E Socket error:undefined.

后尝试使用MQTTBOX工具连接,可以正常使用。

log

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
Host输入自己的公网ip。

猜你喜欢

转载自blog.csdn.net/qq_38091632/article/details/124990369