1、系统 centos7,使用yum命令进行安装
yum install mosquitto
2、在centos系统上执行如下指令启动运行
systemctl start mosquitto
3、设置开机自启动
systemctl enable mosquitto
4、修改端口和开启密码登录
vim /etc/mosquitto/mosquitto.conf
4.1、修改端口
监听端口,默认为 1883,需要修改删除前面 # 注释,改为自己的端口
listener 1883
4.2、开启密码登录
是否启用密码,为 true 不需要,false 需要,默认为 false,如果不要用户名和密码可连接时,删除掉前面的 # 注释,后面添加 true
allow_anonymous
需要密码,配置密码文件,删除 password_file 前面的 # 注释,后面添加密码配置文件
password_file /etc/mosquitto/passwordFile
拷贝创建密码文件
cp /etc/mosquitto/pwfile.example /etc/mosquitto/passwordfile
5、创建用户密码&重启停止服务
5.1、创建用户密码
mosquitto_passwd -c /etc/mosquitto/passwd username
其中 username 需要换成自己的账户名,比如 admin,回车执行后,会提示输入密码和输入确认密码, 完成后重启服务生效
5.2、重启停止服务
重启服务
systemctl restart mosquitto
停止服务
systemctl stop mosquitto
6、连接测试
下载 MQTTX 工具进行测试连接可发布消息,https://mqttx.app/zh/downloads
如果 填写名称、IP / 域名,端口,账号密码,点击 Connect 连接,连接成功左侧状态 会是绿色,失败会提示,连接成功发布消息 test 为主题 —topic,下面输入消息内容 —message,点击发送