個人智能家居系統 - MQTT服務器搭建(centOS7.3)


個人智能家居系統 - 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
    	uid=$(sed -n "$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
    	uid=$(sed -n "$i, 1p" /tmp/graduation.tmp | awk '{print $1}')
    	if [ $uid == "mosquit+" ] ; then
    		break
    	fi
    done
    
    if [ $i != $lines ] ; then
    	pid=$(sed -n "$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
    
    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM