在centos下安装mqtt服务器

325 阅读2分钟
  1. 在官网下载mosquitto2.0.18,官方地址mosquitto.org/download/ 或者下载旧版本 mosquitto.org/files/sourc…
  2. 将下载下来的安装包放在centos自己创建的目录下,使用如下命令进行解压安装
  3. tar -zxvf mosquitto-2.0.18.tar.gz
  4. cd mosquitto-2.0.18
  5. make
  6. 如果在编译的过程中遇到下列错误

cc -I../mosquitto_passwd -DWITH_CJSON -I. -I../../ -I../../include -I../../src -I../../lib -DWITH_TLS -Wall -ggdb -O2 -Wconversion -Wextra -DVERSION=\""2.0.18\"" -c mosquitto_ctrl.c -o mosquitto_ctrl.o In file included from mosquitto_ctrl.c:19: ../../config.h:86:12: fatal error: cjson/cJSON.h: No such file or directory 86 | # include <cjson/cJSON.h> | ^~~~~~~~~~~~~~~ compilation terminated
则是缺少libcjson-dev,使用yum install cjson-devel 安装libcjson-dev(centos下名字不同) 安装cjson-devel之后 7. 返回4 5步骤
8. make install
9. 安装完毕之后 mosquitto -c 配置文件所在的目录 -d启动服务器
10. 执行mosquitto_pub
11. 如果遇到以下错误

mosquitto_pub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
12. 说明缺少libmosquitto.so.1 执行yum install mosquitto-devel(centos下名字不同)
13. 安装完之后 再次执行 mosquitto_pub 发现没有报错
14. 修改配置文件(修改直接建议先备份一个,防止改错之后无法启动,有一个恢复的可以兜底)
15. 配置为文件中默认user mosquitto. 如果不想创建此用户,可以修改成root
192 # When run as root, drop privileges to this user and its primary
193 # group.
194 # Set to root to stay as root, but this is not recommended.
195 # If run as a non-root user, this setting has no effect.
196 # Note that on Windows this has no effect and so mosquitto should
197 # be started by the user you wish it to run as.
198 #user mosquitto
199 user root
16. 配置是否允许匿名访问和用户名密码所在的位置
17. 执行 mosquitto_passwd -c /etc/mosquitto/pwfile.conf 你的用户名 之后输入俩次密码就可以了

646 # Defaults to true if no other security options are set. If password_file or
647 # psk_file is set, or if an authentication plugin is loaded which implements
648 # username/password or TLS-PSK checks, then allow_anonymous defaults to
649 # false.
651 #allow_anonymous true 是否允许匿名访问
652 allow_anonymous ture
666 # See the TLS client require_certificate and use_identity_as_username options
667 # for alternative authentication options. If an auth_plugin is used as well as
668 # password_file, the auth_plugin check will be made first.
669 #password_file
670 password_file /etc/mosquitto/pwfile.conf
18. 重启mosquitto。