centos7 yum安装 mqtt mosquitto

参考:https://blog.csdn.net/darkdragonking/article/details/144510965

 

1、添加EPEL软件包安装源

yum install epel-release

2、安装mosquitto

yum install mosquitto

 3、启动

systemctl start mosquitto

 

4、启用密码(可选)

        vi  /etc/mosquitto/mosquitto.conf 文件,关闭匿名登录、配置监听端口、配置账号文件

        其中,0.0.0.0表示允许在端口 1883 上监听所有网络接口

## 关闭匿名登录
allow_anonymous false
 
## 监听端口。0.0.0.0表示允许在端口 1883 上监听所有网络接口
listener 1883 0.0.0.0
 
## 设置账户密码文件位置
password_file /etc/mosquitto/pwfile.conf
 
## 设置账号权限
acl_file /etc/mosquitto/aclfile.example

上面提到账号密码存在  pwfile.conf 文件中,但刚刚安装后暂无此文件,需要基于 pwfile.example 创建

cp -rf /etc/mosquitto/pwfile.example /etc/mosquitto/pwfile.conf

然后向 pwfile.conf 中追加期望的账号,例如admin

mosquitto_passwd /etc/mosquitto/pwfile.conf admin

执行完上述命令后,mosquitto 会立即请你提供账号的密码

 

测试

开一终端,订阅端登录

mosquitto_sub -t testtopic -p 1883 -u 'admin' -P 'password@'

再开一终端,发布端登录

mosquitto_pub -t testtopic -p 1883 -u 'admin' -P 'password@' -m 'hello world!'

 

posted @ 2023-06-30 17:53  苦逼yw  阅读(332)  评论(0)    收藏  举报