【笔记】MacOS上部署MQTT环境

前言

MacOS上部署MQTT环境

安装MQTT

1
brew install mosquitto

配置用户名密码

修改配置文件

  • 复制用户名密码配置文件
1
cp /opt/homebrew/etc/mosquitto/pwfile.example /opt/homebrew/etc/mosquitto/pwfile
  • 去除配置文件第23行的注释,并将允许任何人访问改为false
/opt/homebrew/etc/mosquitto/mosquitto.conf
1
allow_anonymous false
  • 去除配置文件第26行的注释,并指定用户名密码配置文件路径
1
password_file /opt/homebrew/etc/mosquitto/pwfile

创建用户

-c:覆盖配置文件并向配置文件添加新用户
/opt/homebrew/etc/mosquitto/pwfile:指定用户名密码配置文件
admin:指定用户名

1
mosquitto_passwd -c /opt/homebrew/etc/mosquitto/pwfile admin
  • 指定密码
1
2
Password: 
Reenter password:

仅添加新用户

1
mosquitto_passwd /opt/homebrew/etc/mosquitto/pwfile admin
  • 指定密码
1
2
Password: 
Reenter password:

启动服务

通过brew启动服务

  • 默认监听端口为1883
1
brew services start mosquitto

手动启动服务

-c <filename>:指定配置文件
-p 1883:指定端口号
-d:在后台运行

1
mosquitto

订阅者

-h 127.0.0.1:指定MQTT服务器IP地址,缺省值为127.0.0.1
-p 1883:指定MQTT服务器IP端口号,缺省值为1883
-t topic/test:指定主题
-u <username>:指定用户名
-P <password>:指定密码

1
mosquitto_sub -t topic/test

发布者

-h 127.0.0.1:指定MQTT服务器IP地址,缺省值为127.0.0.1
-p 1883:指定MQTT服务器IP端口号,缺省值为1883
-t topic/test:指定主题
-m "<text>":指定消息内容
-u <username>:指定用户名
-P <password>:指定密码

1
mosquitto_pub -t topic/test -m "<text>"

完成

参考文献

whatled的博客
CSDN——bj_wasin
mqttx官网文档
mqttx官网文档
哔哩哔哩——谷雨课堂
博客园——_海阔天空