环境
Linux:CentOS Linux release 7.5.1804
Redis:5.01
Mosquitto:1.5.8
mosquitto安装
请参考之前的文章:https://www.jianshu.com/p/d90fa3b6a12c
插件安装
下载地址:https://github.com/jpmens/mosquitto-auth-plug
安装之前应该安装hiredis可编译安装也可yum安装
#yum安装
yum install hiredis-devel
编辑config.mk指定开启redis,mosquitto源码目录
cp config.mk.in config.mk
vim config.mk
#修改如下配置项:
BACKEND_MYSQL ?= no
BACKEND_REDIS ?= yes
MOSQUITTO_SRC = /usr/local/mosquitto/mosquitto/src
# 编译
make
#mosquitto目录下创建plugin目录
cd /usr/local/mosquitto/mosquitto/src
mkdir plugin
#将插件中编译生成的.so文件复制到/usr/local/mosquitto/mosquitto/src/plugin目录下
cp auth-plug.so /usr/local/mosquitto/mosquitto/src/plugin
#修改mosquitto的配置文件
修改如下配置项
allow_anonymous false
在这一行下新增
auth_plugin /usr/local/mosquitto/mosquitto/src/plugin/auth-plug.so
auth_opt_backends redis
auth_opt_redis_host localhost
auth_opt_redis_port 6379
auth_opt_redis_userquery GET mqtt_auth_%s
生成密码
#生成密码为“123456”
./np -p admin #得到输出:PBKDF2$sha256$901$lzMxwbgXSoPWfG3U$J/UVQWp39U8AQWpaOZhO/Rf6hlqVogm0
#存储redis
set mqtt_auth_admin PBKDF2$sha256$901$lzMxwbgXSoPWfG3U$J/UVQWp39U8AQWpaOZhO/Rf6hlqVogm0
测试
mosquitto_sub -h localhost -p 1883 -u admin -P 123456 -t test