个人智能家居系统 - MQTT服务器搭建,centOS7.3

0x00 参考

0x01 配置

  • General configuration

    所见即所得,保持默认即可

  • Default listener

    # 设置端口
    port 1883
    # 设置最大连接数
    max_connections -1
    # 使用协议,mqtt或者websocket
    protocol mqtt
    
  • SSL/TLS support

    用于 default listerner 的安全设置,暂未设置

  • Extra listeners

    用于 websocket ,暂未设置

  • SSL/TLS support

    用于 Extra listeners 的安全设置,暂未设置

  • Persistence

    持续性设置,即 mosquitto 重启后恢复设置,暂未设置

    而且客户端的断线重连机制更加稳妥

  • Logging

    开启服务时重定向 stdout 等信息至文件,这里只设置 type

  • Security

    # 设置前缀
    clientid_prefixes guduyl
    # 禁止匿名登录
    allow_anonymous false
    # 设置用户名密码文件
    password_file /etc/mosquitto/pwfile
    # 设置权限信息文件
    acl_file /etc/mosquitto/aclfile
    
  • Bridges

    用于分布式服务器,暂未设置

  • SSL/TLS support

    分布式服务器安全设置,暂未设置

  • External config files

  • rsmb options

用户名密码设置

  • mosquitto_passwd 命令,查看帮助即可

权限文件设置

  • 仿照 aclfile.example 文件

    • test/jh/# 可匹配 test/jh/a/b/c, test/jh/a/b, test/jh/a.test/jh
    • test/jh/+ 可匹配 test/jh/a, test/jh/b, 但是不能匹配 test/jh/a/b

启动停止

  • 启动

    #! /bin/bash
    
    ps -ef | grep mosquitto | tee /tmp/graduation.tmp
    
    lines=$(awk 'END{print NR}' /tmp/graduation.tmp)
    lines=`expr $lines + 1`
    for ((i=1; i<$lines; ++i))
    do
            u$i, 1p" /tmp/graduation.tmp | awk '{print $1}')
            if [ $uid == "mosquit+" ] ; then
                    break
            fi
    done
    
    if [ $i != $lines ] ; then
            echo "the mosquitto1.4.1 had been started already"
    else
            echo "Starting the mosquitto1.4.1 ..."
            mosquitto -d -c /etc/mosquitto/mosquitto.conf > /root/graduation/mosquitto.log 2>&1
            echo "the mosquitto1.4.1 has been started"
            echo "the log file is /root/graduation/mosquitto.log"
    fi
    
    rm -f /tmp/graduation.tmp
    
    
  • 停止

    #! /bin/bash
    
    ps -ef | grep mosquitto | tee /tmp/graduation.tmp
    
    lines=$(awk 'END{print NR}' /tmp/graduation.tmp)
    lines=`expr $lines + 1`
    for ((i=1; i<$lines; ++i))
    do
            u$i, 1p" /tmp/graduation.tmp | awk '{print $1}')
            if [ $uid == "mosquit+" ] ; then
                    break
            fi
    done
    
    if [ $i != $lines ] ; then
            p$i, 1p" /tmp/graduation.tmp | awk '{print $2}')
            echo "killing $pid ..."
            kill $pid
            echo "the mosquitto1.4.1 has been stopped"
    else
            echo "the mosquitto1.4.1 has not been started yet"
    fi
    
    rm -f /tmp/graduation.tmp
    
    
  • 重启

    #! /bin/bash
    
    /root/graduation/stop.sh
    /root/graduation/start.sh