个人智能家居系统 - MQTT服务器搭建(centOS7.3)
0x00 参考
- 在CentOS7 上安装mosquitto1.4.1服务器,实现MQTT信息推送功能并增加websocket功能
- mosquitto的鉴权配置
0x01 配置
- General configuration
所见即所得,保持默认即可 - Default listener
# 设置端口port 1883# 设置最大连接数max_connections -1# 使用协议,mqtt或者websocketprotocol 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
启动停止
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
- 停止```shell#! /bin/bashps -ef | grep mosquitto | tee /tmp/graduation.tmplines=$(awk 'END{print NR}' /tmp/graduation.tmp)lines=`expr $lines + 1`for ((i=1; i<$lines; ++i))douid=$(sed -n "$i, 1p" /tmp/graduation.tmp | awk '{print $1}')if [ $uid == "mosquit+" ] ; thenbreakfidoneif [ $i != $lines ] ; thenpid=$(sed -n "$i, 1p" /tmp/graduation.tmp | awk '{print $2}')echo "killing $pid ..."kill $pidecho "the mosquitto1.4.1 has been stopped"elseecho "the mosquitto1.4.1 has not been started yet"firm -f /tmp/graduation.tmp
/root/graduation/stop.sh /root/graduation/start.sh ```
