零、缘起
看到目前各大云平台都搭建自己的mqtt服务器,本想直接用作项目。奈何算了下成本,实在太贵。尝试自己搭建mqtt服务器,遂拿尘封已久的树莓派进行测试。
一、安装
- wget -c https://github.com/eclipse/mosquitto/archive/v1.4.11.tar.gz
- tar zxvf v1.4.11.tar.gz
- cd mosquitto-1.4.11
- sudo apt install cmake g++ libssl-dev libc-ares-dev uuid-dev xsltproc docbook-xsl
- mkdir build
- cd build
- cmake ..
- make
- sudo make install
二、错误(安装的时候遇到错误,未解决,但不影响)
running /sbin/ldconfig 2>&1
CMake Error at man/cmake_install.cmake:36 (file):
file INSTALL cannot find
"/home/pi/mosquitto-1.4.11/man/mosquitto_passwd.1".
Call Stack (most recent call first):
cmake_install.cmake:49 (include)
Makefile:115: recipe for target 'install' failed
make: *** [install] Error 1
三、配置账号和密码
1、生成账号密码
添加用户'abc'密码’xxx,
#mosquitto_passwd -c ./pwfile.example abc
输入两遍密码
2、修改配置文件(mosquitto.conf )
2.1 、关闭匿名访问
allow_anonymous false
2.2、配置用户和密码文件路径
password_file ../build/pwfile.example
四、测试
4.1指定配置文件启动
mosquitto -c ./mosquitto-1.4.14/mosquitto.conf
4.2 测试
4.2.1订阅
mosquitto_sub -t test_topic -u 账户 -P 密码
4.2.2发布
mosquitto_pub -t test_topic -u 账户 -P 密码 -m "消息"
参考:
https://www.jianshu.com/p/f8d824afbe3d
http://blog.csdn.net/u012377333/article/details/69397124