mosquitto(mqtt)服务部署+redis 认证模块
物联网交流群:651219170
功能说明
运行的环境(系统版本,mosquitto版本,redis版本)
1.centos6.5 及以上.
2.mosquitto-1.4.9
3.redis 3.0.1
- 1
- 2
- 3
- 4
部署准备(外部依赖)
1.openssl
2.c-ares-devel
3.libc-ares2
4.libuuid-devel
5.gcc
6.cmake
7.hiredis
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
配置文件的说明
部署环境搭建步骤
0.安装redis
wget http://download.redis.io/releases/redis-3.0.1.tar.gz
tar xzf redis-3.0.1.tar.gz
cd redis-3.0.1
make && make install
mkdir /etc/redis && cp redis.conf /etc/redis/6379.conf
后台启动
vim /etc/redis/6379.conf
修改文件
daemonize yes
修改 redis 的启动脚步
vim utils/redis_init_script
把原注释替换为
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
#
设置开机启动
cp utils/redis_init_script /etc/init.d/redisd
chkconfig redisd on
1.安装外部依赖
1.1 yum 安装依赖
yum install openssl-devel
yum install c-ares-devel
yum install libuuid-devel
yum install gcc*
1.2 编译安装hiredis客户端
1.2.1 下载tar.gz文件
wget https://github.com/redis/hiredis/archive/v0.13.3.tar.gz
1.2.2 解压编译安装
tar -zxvf v0.13.3.tar.gz
cd hiredis-0.13.3
make -j8
make install
2.安装mosquitto实例:
2.1 下载tar.gz文件
wget https://mosquitto.org/files/source/mosquitto-1.4.9.tar.gz
2.2 解压
tar -zxvf mosquitto-1.4.9.tar.gz
2.3 进入安装目录
cd mosquitto-1.4.9
2.4 编译
sudo make
sudo make install
2.5 加载.so.1文件
vim /etc/ld.so.conf.d/user.conf
加入内容:
/usr/local/lib
加载以上配置
ldconfig
2.6 更新动态链接库
sudo ldconfig
2.7 增加mosquitto用户
adduser mosquitto
2.8 测试mosquitto安装
启动mosquitto
mosquitto -c mosquitto.conf
使用"功能验证建议"中的工具进行连接测试
3.安装redis认证插件
3.1 下载tar.gz
wget https://github.com/jpmens/mosquitto-auth-plug/archive/0.1.2.tar.gz
3.2 解压,修改配置
tar -zxvf 0.1.2.tar.gz
cd mosquitto-auth-plug-0.1.2
cp config.mk.in config.mk
3.3 编辑config.mk指定开启redis,mosquitto源码目录
vim config.mk
修改如下配置项:
BACKEND_MYSQL ?= no
BACKEND_REDIS ?= yes
MOSQUITTO_SRC = /usr/local/mosquitto-env/mosquitto-1.4.9/src
编译
make
3.4 mosquitto目录下创建plugin目录
mkdir plugin
3.5 将插件中的.so文件复制到plugin目录下
cp auth-plug.so mosquitto/plugin
3.6 修改mosquitto的配置文件
vim mosquitto.conf
修改如下配置项
allow_anonymous false
在这一行下新增
auth_plugin /usr/local/mosquitto-env/mosquitto-1.4.9/plugin/auth-plug.so
auth_opt_backends redis
auth_opt_redis_host 192.168.25.162
auth_opt_redis_port 6379
auth_opt_redis_userquery GET %s
3.7 在认证插件中使用np.c文件生成密码(例如,密码是admin的加密方式如下)
./np -p admin
得到输出: PBKDF2$sha256$901$lzMxwbgXSoPWfG3U$J/UVQWp39U8AQWpaOZhO/Rf6hlqVogm0
3.8 将用户名和加密后的密码添加到redis中
set admin PBKDF2$sha256$901$lzMxwbgXSoPWfG3U$J/UVQWp39U8AQWpaOZhO/Rf6hlqVogm0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
相关注意事项
功能验证建议
1.根据如下url下载不同操作系统下的mosquitto图形化客户端工具
https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.ui.app/1.1.1/
2. 新建连接
连接地址:tcp://url:port(默认端口为1883)
输入用户名密码 admin admin
连接,连接成功即完成搭建