服务器MQTT环境的搭建

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

MQTT服务端mosquitto安装和环境配置
  • 先添加软件源
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有区别的)
sudo apt-get install mosquitto
  • 修改配置文件信息
    • Mosquitto升级到2.x版本后改变了监听器的策略,在2.x版本后没有配置监听器的情况下,只允许本地客户端(也就是localhost或127.0.0.1)的地址连接Mosquitto,在这种情况下是允许匿名连接,也就是说没有配置监听器时,你在mosquitto.conf配置的用户校验是无效的,要想解决很简单在mosquitto.conf配置监听器就行了。添加listener 1883就行了。版本1.x不用添加
    • 注意区别:
    pid_file /var/run/mosquitto.pid  # 版本1.x才有
    listener 1883  # 版本2.x才有
    
    • 修改/etc/mosquitto/mosquitto.conf文件如下:
    # 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
    
    • 认证配置文件,创建文件
    touch /etc/mosquitto/pwfile
    
    • 创建用户名和密码(用户名是自己取的,每个项目单独再取用户名),输入下面指令后会要求输入两次密码
    mosquitto_passwd /etc/mosquitto/pwfile KCareU   # 例如用户名为 KCareU, 密码是gamma123
    
    • 完成后用户名和密码自动写入pwfile文件中
    • 创建权限配置文件
    touch /etc/mosquitto/aclfile
    
    • 按需求编辑自己的权限配置文件aclfile
    user KCareU
    topic write KCareU/#
    topic read KCareU/#
    
  • 启动
mosquitto -c /etc/mosquitto/mosquitto.conf -d  # -c:指定配置文件  -d:后台运行
  • 查询启动指令
service mosquitto start  # 启动
service mosquitto status  # 状态
service mosquitto stop  # 停止

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

  • 目前官网好像有Bug,下载不了。我上传一下我下载过的mqttfx-1.7.1-windows-x64.exe,需要的翻一翻我的记录免费下载
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值