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