CentOS 安装 Mosquitto

文章转载自 Alson

CentOS 安装步骤

  1. 查询 CentOS 是否已经安装 (可选,默认没有安装)

    [root@iZuf6bn8133scbluly1rheZ ~]# yum list installed mosquitto
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Error: No matching Packages to list
    
  2. 查询软件源中是否有 mosquitto 安装包

    [root@iZuf6bn8133scbluly1rheZ ~]# yum search mosquitto
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    ======================================================================== N/S matched: mosquitto =========================================================================
    mosquitto-devel.x86_64 : Development files for mosquitto
    mosquitto.x86_64 : An Open Source MQTT v3.1/v3.1.1 Broker
    
      Name and summary matches only, use "search all" for everything.
    
  3. 查看 mosquitto 详细信息 (可选)

    [root@iZuf6bn8133scbluly1rheZ ~]# yum info mosquitto
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Available Packages
    Name        : mosquitto
    Arch        : x86_64
    Version     : 1.6.10
    Release     : 1.el7
    Size        : 287 k
    Repo        : epel/x86_64
    Summary     : An Open Source MQTT v3.1/v3.1.1 Broker
    URL         : http://mosquitto.org/
    License     : BSD
    Description : Mosquitto is an open source message broker that implements the MQ Telemetry
    			: Transport protocol version 3.1 and 3.1.1 MQTT provides a lightweight method
    			: of carrying out messaging using a publish/subscribe model. This makes it
    			: suitable for "machine to machine" messaging such as with low power sensors
    			: or mobile devices such as phones, embedded computers or micro-controllers
    			: like the Arduino.
    
  4. 安装

    yum install mosquitto
    
  5. 查看 mosquitto 帮助信息

    [root@iZuf6bn8133scbluly1rheZ ~]# mosquitto --help
    mosquitto version 1.6.10
    
    mosquitto is an MQTT v3.1.1 broker.
    
    Usage: mosquitto [-c config_file] [-d] [-h] [-p port]
    
     -c : specify the broker config file.
     -d : put the broker into the background after starting.
     -h : display this help.
     -p : start the broker listening on the specified port.
    	  Not recommended in conjunction with the -c option.
     -v : verbose mode - enable all logging types. This overrides
    	  any logging options given in the config file.
    
    See http://mosquitto.org/ for more information.
    

Mosquitto 配置

认证方式:用户名和密码

  1. 添加新用户

    mosquitto_passwd -c mos_users alson
    

    mos_userspasswordfile,用来保存用户的 usernamepassword

  2. 修改配置文件,不允许匿名登录

    默认配置文件 /etc/mosquitto/mosquitto.conf,将 allow_anonymous 改为 false

    allow_anonymous false
    
  3. 修改配置文件,修改端口

    默认端口为 1883,可以通过修改 port 指定端口

    port 1883
    
  4. 修改配置文件,指定 passwordfile 路径

    password_file /root/mos_users
    
  5. 运行服务器

    [root@iZuf6bn8133scbluly1rheZ ~]# mosquitto -c /etc/mosquitto/mosquitto.conf -v
    1709098551: mosquitto version 1.6.10 starting
    1709098551: Config loaded from /etc/mosquitto/mosquitto.conf.
    1709098551: Opening ipv4 listen socket on port 1883.
    1709098551: Opening ipv6 listen socket on port 1883.
    

Subscriber 订阅 Topic

Subscriber 使用 MQTT.fx,具体使用方法请百度。订阅的主题为 info

info-sub

Publisher

CentOS 上新开一个终端, 使用 mosquitto_pub 发布 Topicinfo 的消息

mosquitto_pub -t info -u alson -P 12345678 -m "this is a test message"

info-recv

问题总结

  1. MQTT.fx 连接不上服务器。

    检查端口是否开放。对于云服务器来说,需要在安全选项里面放开端口。如果安全选项里面已经开放端口还是连接不上,继续检查是否有别的地方可以开放端口设置。(笔者使用 宝塔面板 在服务器上搭建了 WordPress 服务器,需要在 宝塔面板安全 选项中也要开放对应的端口)

    port

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值