Centos7安装MQTT

本文详细介绍了在CentOS7系统中如何安装MQTT服务器(Mosquitto)及其依赖库,包括下载资源包、安装依赖、配置编译选项、编译安装、测试运行等步骤。同时,针对安装过程中可能出现的libwebsockets库找不到或MQTT启动报错的问题,提供了相应的解决方法,如创建软链接和配置文件修改。此外,还讲解了设置MQTT服务器的用户密码和权限控制,确保服务器安全稳定运行。
摘要由CSDN通过智能技术生成

MQTT Centos7安装
第一种方式安装
下载资源包
# wget https://mosquitto.org/files/source/mosquitto-1.6.7.tar.gz      
# tar zxvf mosquitto-1.6.7.tar.gz
# mv mosquitto-1.6.7 /usr/local/mosquitto-1.6.7


安装MQTT运行依赖环境
以下三个必须安装,不然make时无法通过。 另外安装MQTT之前,我们也可以# cat compiling.txt文件阅读一下官方的说明。

# yum -y install openssl-devel
# yum -y install gcc-c++
# yum -y install cmake

其他扩展
# yum install -y c-ares-devel
# yum install -y uuid-devel 
# yum install -y libuuid-devel
# yum install -y libwebsockets  //yum安装会出错,请使用编译安装

修改config.mk配置文件
# cd /usr/local/mosquitto-1.6.7
# vim config.mk

修改以下为yes,如果前面有#就去掉,保存退出。

WITH_SRV:=yes

WITH_WEBSOCKETS:=yes

WITH_ADNS:=yes

编译安装
# make
# make install

以上执行# make时,如果不报错的话,就跳过以下说明。如果报错: mosquitto.c:49:29: fatal error: libwebsockets.h: No such file or directory

那么说明libwebsockets 没有安装好,执行卸载# yum remove libwebsockets,下载新的源包的进行安装,步骤如下。

源包链接:https://github.com/warmcat/libwebsockets 我们选择一个版本复制zip下载链接,这里我选择的是1.5版本。

# wget https://github.com/warmcat/libwebsockets/archive/v1.5-stable.zip   
# yum -y install unzip
# unzip v1.5-stable.zip            
# mkdir -p /usr/local/websocket
# mv libwebsockets-1.5-stable /usr/local/websocket
# cd /usr/local/websocket/libwebsockets-1.5-stable
# cmake .
# make
# make install

执行以上安装ibwebsockets后,重新编译安装MQTT # cd /usr/local/mosquitto-1.6.7 # make # make install 这3个命令前面已经列出过了。

测试
创建用户:

groupadd mosquitto
useradd -g mosquitto mosquitto

程序配置:
mv /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf

启动程序:
mosquitto -c /etc/mosquitto/mosquitto.conf -d
默认端口为1883


最后我们再打开一个服务器窗口,在一个(订阅)窗口输入:
mosquitto_sub -t hello

另一个(发布)窗口输入:
mosquitto_pub -t hello -h localhost -m "hello world"


启动MQTT报错解决
错误信息:mosquitto: error while loading shared libraries: libwebsockets.so.5: cannot open shared object file: No such file or directory
解决方法:使用whereis可以查找 libwebsockets.so.5的所在路径,建立一个软链接到/usr/lib 下即可。

# ln -s /usr/local/lib/libwebsockets.so.5 /usr/lib/libwebsockets.so.5
# ldconfig

错误信息mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
ldconfig

mosquitto重启
1、查看mosquitto的进程
ps -aux | grep mosquitto
 
2、杀掉进程
kill -9 18248
 
3.启动mosquitto
mosquitto -c /etc/mosquitto/mosquitto.conf -d

设置用户名密码
打开mosquitto.conf文件,找到allow_anonymous节点,这个节点作用是,是否开启匿名用户登录,默认是true。打开此项配置(将前面的 # 号去掉)之后将其值改为true

修改前:#allow_anonymous
修改后:allow_anonymous false
1
2
找到password_file节点,这个节点是告诉服务器你要配置的用户将存放在哪里。打开此配置并指定pwfile.example文件路径

修改前:#password_file
修改后:password_file /etc/mosquitto/pwfile

创建用户名和密码、打开命令窗口 键入如下命令:

mosquitto_passwd -c /etc/mosquitto/pwfile admin
提示连续两次输入密码、创建成功。命令解释: -c 创建一个用户 /etc/mosquitto/pwfile 是将用户创建到 pwfile文件中、admin 是用户名

设置权限
打开mosquitto.conf文件,找到password_file节点

修改前:#acl_file
修改后:acl_file /etc/mosquitto/aclfile

认证配置pwfile,没有则创建文件

# This affects access control for clients with no username.
topic read $SYS/#

# This only affects clients with username "roger".
user roger
topic foo/bar

# This affects all clients.
#pattern write $SYS/broker/connection/%c/state
#admin 可读写hello topic
user admin
topic hello/#

#bing 只能读 
user bing
topic read hello/#

mosquitto_sub -t hello -h 127.0.0.1 -u admin -P admin@123
mosquitto_pub -t hello -h localhost -m "hello world" -u admin -P ad
————————————————
版权声明:本文为CSDN博主「bing03246」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhesir/article/details/107707166

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值