Centos7安装MQTT

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

猜你喜欢

转载自blog.csdn.net/qiuziqiqi/article/details/118117840