1.安装依赖

yum install gcc-c++ cmake openssl-devel libuuid-devel c-ares-devel uuid-devel libwebsockets-devel.x86_64 libwebsockets.x86_64 -y

 

2.下载mosquitto

官网:https://mosquitto.org/

cd /home
wget --no-check-certificate https://mosquitto.org/files/source/mosquitto-1.6.8.tar.gz

3.解压 编译 安装

tar -zxvf mosquitto-1.6.8.tar.gz
cd mosquitto-1.6.8
make
make install

之后会碰到找不到libmosquitto.so.1这个问题,修改链接路径,重新加载动态连接库

ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
ldconfig

4.创建配置文件

cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
vim /etc/mosquitto/mosquitto.conf

配置文件中默认使用user mosquitto。如果不想创建此用户,可以修改成root。

 5. 启动、查看、关闭程序

# 运行程序
mosquitto -c /etc/mosquitto/mosquitto.conf -d
# ps 查看
ps -aux | grep mosquitto
# 关闭程序
kill -9 $(pidof mosquitto)

6.设置用户名密码

# 我这里设置的用户名是 admin
mosquitto_passwd -c /etc/mosquitto/pwfile.conf admin
# 然后输入两遍密码即可

注意,如果提示不存在pwfile.conf,则需要手动创建该文件。

 

编辑配置文件

vim /etc/mosquitto/mosquitto.conf

增加下面内容:

password_file /etc/mosquitto/pwfile.conf

然后重启即可。

7.查看版本

mosquitto -v

8.本地测试

 

打开一个订阅者

 打开一个发布者

 相同topic的双方,发布者pub发送“发布内容”给订阅者sub。

 

注意:如果其他机器连接服务需要修改配置文件中 allow_anonymous true

 

 

9.下载客户端测试

下载地址:https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.ui.app/1.0.2/

 双击运行paho.exe

 点击“+”号创建连接。

 

 

参考地址:https://blog.csdn.net/tswc_byy/article/details/130766747

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。