mosquitto服务器搭建及配置

喜欢ヅ旅行 2024-04-01 10:53 164阅读 0赞

1、搭建mosquitto服务器,根据电脑系统选择合适的版本

在这里插入图片描述

2、安装文件目录

在这里插入图片描述

3、文件作用


































文件名称 作用
mosquitto.conf mosquitto服务器配置文件
mosquitto.exe 用来开启broker
mosquitto_passwd.exe 用来设置用户的账号与密码
mosquitto_pub.exe 用来发布消息
mosquitto_sub.exe 用来订阅消息
pwfile.example 通过mosquitto_passwd来存储用户账号密码的文件

4、配置系统环境变量

此电脑(右键)–>属性–>高级系统设置–>环境变量–>编辑Path变量

在这里插入图片描述

5、打开配置文件mosquitto.conf更改默认配置

  1. # 设置允许匿名连接,则省略第6步;如果禁止匿名连接,则需要配置账号、密码连接,则配置第6步
  2. allow_anonymous true
  3. # 密码文件路径,禁止匿名连接配置,允许匿名则不用配置
  4. password_file D:\SoftWare1\mosquitto\pwfile.example
  5. # mqtt 协议配置,分为ipv4和ipv6两种,当前可以只配置启用一种
  6. listener 1883 0.0.0.0
  7. socket_domain ipv4
  8. protocol mqtt
  9. listener 1883 ::
  10. socket_domain ipv6
  11. protocol mqtt
  12. # webscoket 协议配置,分为ipv4和ipv6两种,当前可以只配置启用一种
  13. listener 1884 0.0.0.0
  14. protocol websockets
  15. socket_domain ipv4
  16. listener 1884 ::
  17. protocol websockets
  18. socket_domain ipv4
  19. # 允许的最大连接数,-1表示没有限制
  20. max_connections -1

6、配置mosquitto账号密码,输入密码界面不会显示密码,直接输入回车即可,需要连续输入两次。保证pwfile.example的路径和上面的配置一致。创建成功后pwfile.example会出现刚刚添加的用户信息。Win+R键输入cmd并进入mosquitto根目录输入:

  1. # 使用-c 参数会导致清空密码文件,重新插入用户
  2. mosquitto_passwd -c pwfile.example 用户名
  3. # 不使用-c 表示追加用户,不影响旧用户
  4. mosquitto_passwd -c pwfile.example 用户名

7、重启mosquitto服务,配置文件的修改才能生效

此电脑(右键)–>管理–>服务和应用程序–>服务–>启动Mosquitto Broker

在这里插入图片描述

8、mosquitto服务器使用

  1. # 启动服务器
  2. mosquitto.exe -c mosquitto.conf -v
  3. # 订阅者
  4. mosquitto_sub.exe -t "test"
  5. # 发布者
  6. mosquitto_pub.exe -t "test" -m "测试信息"

9、开启两个cmd窗口用于测试

mosquitto订阅者使用

在这里插入图片描述

mosquitto发布者使用

在这里插入图片描述
常见问题:端口占用
①查找被占用的端口

  1. # 查找使用的端口
  2. netstat -ano
  3. # 查找指定的端口
  4. netstat -ano |findstr 1883

在这里插入图片描述

②停止端口

  1. # 停用端口
  2. taskkill -PID 8984 -F
  3. # 查看占用此端口的进程,在任务管理器中强制结束
  4. tasklist|findstr 8984

发表评论

表情:
评论列表 (有 0 条评论,164人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Nginx服务器以及配置

    刚开始听到nginx负载均衡感觉很牛逼的样子,还有就是服务器集群,反向代理这些字眼感觉可高大上,后来发现,去实现一下发现,原来是纸老虎。 1.下载nginx, http