我是靠谱客的博主 长情钻石,这篇文章主要介绍服务器MQTT环境的搭建,现在分享给大家,希望可以做个参考。

MQTT服务端mosquitto安装和环境配置

        • MQTT服务端mosquitto安装和环境配置

MQTT服务端mosquitto安装和环境配置

  • 先添加软件源
复制代码
1
2
3
4
5
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa # 若有错误则添加自己的key,如下 sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 5E64E954262C4500 sudo apt update
  • 安装(版本1和2有区别的)
复制代码
1
2
sudo apt-get install mosquitto
  • 修改配置文件信息
    • Mosquitto升级到2.x版本后改变了监听器的策略,在2.x版本后没有配置监听器的情况下,只允许本地客户端(也就是localhost或127.0.0.1)的地址连接Mosquitto,在这种情况下是允许匿名连接,也就是说没有配置监听器时,你在mosquitto.conf配置的用户校验是无效的,要想解决很简单在mosquitto.conf配置监听器就行了。添加listener 1883就行了。版本1.x不用添加
    • 注意区别:
    复制代码
    1
    2
    3
    4
    5
    pid_file /var/run/mosquitto.pid # 版本1.x才有 listener 1883 # 版本2.x才有
    • 修改/etc/mosquitto/mosquitto.conf文件如下:
    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    # Place your local configuration in /etc/mosquitto/conf.d/ # # A full description of the configuration file is at # /usr/share/doc/mosquitto/examples/mosquitto.conf.example # 版本1.x才有 # pid_file /var/run/mosquitto.pid # 消息持久存储 persistence true persistence_location /var/lib/mosquitto/ # 日志文件 log_dest file /var/log/mosquitto/mosquitto.log # 其他配置 include_dir /etc/mosquitto/conf.d # 版本2.x才有 listener 1883 # 禁止匿名访问 allow_anonymous false # 认证配置 password_file /etc/mosquitto/pwfile # 权限配置 acl_file /etc/mosquitto/aclfile
    • 认证配置文件,创建文件
    复制代码
    1
    2
    touch /etc/mosquitto/pwfile
    • 创建用户名和密码(用户名是自己取的,每个项目单独再取用户名),输入下面指令后会要求输入两次密码
    复制代码
    1
    2
    3
    mosquitto_passwd /etc/mosquitto/pwfile KCareU # 例如用户名为 KCareU, 密码是gamma123
    • 完成后用户名和密码自动写入pwfile文件中
    • 创建权限配置文件
    复制代码
    1
    2
    touch /etc/mosquitto/aclfile
    • 按需求编辑自己的权限配置文件aclfile
    复制代码
    1
    2
    3
    4
    user KCareU topic write KCareU/# topic read KCareU/#
  • 启动
复制代码
1
2
3
4
mosquitto -c /etc/mosquitto/mosquitto.conf -d # -c:指定配置文件 -d:后台运行
  • 查询启动指令
复制代码
1
2
3
4
5
6
7
service mosquitto start # 启动 service mosquitto status # 状态 service mosquitto stop # 停止

windows端MQTT.fx客户端下载链接: MQTT.fx

  • 目前官网好像有Bug,下载不了。我上传一下我下载过的mqttfx-1.7.1-windows-x64.exe,需要的翻一翻我的记录免费下载

最后

以上就是长情钻石最近收集整理的关于服务器MQTT环境的搭建的全部内容,更多相关服务器MQTT环境内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部