安装启动Mosquitto以及问题error: cjson/cJSON.h: No such file or directory解决
安装Mosquitto
在官方下载地址:https://mosquitto.org/files/source/ 选择版本下载
安装环境是linux centos7,上传 mosquitto-2.0.18.tar.gz 文件到 /mqtt 文件夹下
tar -xvf mosquitto-2.0.18.tar.gz #解压
cd mosquitto-2.0.18/ #切换到解压目录下
make && make install # 安装
问题:安装mosquitto时报g++:命令未找到
解决方案:
安装编译工具 yum install -y gcc-c++
问题:重新执行make && make install,继续报错config.h:86:12: fatal error: cjson/cJSON.h: No such file or directory
需要安装 cJSON ,使用git 安装,没有git先安装git
yum install git
git clone https://github.com/DaveGamble/cJsoN.git # 下载cJSON
tar -zxvf mosquitto-2.0.18.tar.gz # 解压
cd cJSON/ #切换到 cJSON目录下
make && make install # 安装
安装完成后,切回mosquitto-2.0.18目录,重新安装
make && make install # 安装
没有报错,安装完成。
启动
修改 mosquitto.conf 文件
- 设置允许匿名用户
#(大概532行)
allow_anonymous true
- 修改监听地址和端口
注意检查端口是否被占用
listener 2883 0.0.0.0
如果禁止匿名用户,需要 配置使用的密码文件,注意要写绝对路径
# (大约550行)
password_file /mqtt/mosquitto-2.0.18/pwfile
- 创建密码文件,在 mosquitto-2.0.18目录下
cp pwfile.example pwfile
- 创建用户,执行命令后,输入两次密码
mosquitto_passwd -c /mqtt/mosquitto-2.0.18/pwfile admin
- 启动
mosquitto -c /mqtt/mosquitto-2.0.18/mosquitto.conf
- 报错无法打开密码文件,可能是权限不足,修改文件权限
chmod 600 /mqtt/mosquitto - 2.0.18/pwfile