MQTT Centos7安装

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/websocke/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

找到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 admin@123

第二种方式安装

添加EPEL库

yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

查看EPEL安装结果

yum repolist all | grep enabled

安装Mosquitto

查找 mosquitto 软件包:yum list all | grep mosquitto
查看 mosquitto 信息: yum info mosquitto
安装 mosquitto 软件包: yum install mosquitto
查看安装结果:yum list installed | grep mosquitto

测试Mosquitto

启动 mosquitto 主程序:mosquitto
新建一个ssh窗口;订阅》执行命令:mosquitto_sub -t test
新建一个ssh窗口;发布》执行命令:mosquitto_pub -t test -h localhost -m "hello, world"
可以看到订阅窗口收到:hello, world

开机自启mosquitto

查找 mosquitto 服务:find / -iname mosquitto* | grep service
设置 mosquitto 服务:systemctl enable mosquitto.service
启动 mosquitto 服务:systemctl start mosquitto.service
查看 mosquitto 状态:systemctl status mosquitto.service
查看进程:ps -aux | grep mosquitto
重启 CentOS:reboot,查看 mosquitto 服务是否自动启动:systemctl list-units | grep mosquitto

mosquitto_pub 命令参数说明

  1. -d 打印debug信息
  2. -f 将指定文件的内容作为发送消息的内容
  3. -h 指定要连接的域名 默认为localhost
  4. -i 指定要给哪个clientId的用户发送消息
  5. -I 指定给哪个clientId前缀的用户发送消息
  6. -m 消息内容
  7. -n 发送一个空(null)消息
  8. -p 连接端口号
  9. -q 指定QoS的值(0,1,2)
  10. -t 指定topic
  11. -u 指定broker访问用户
  12. -P 指定broker访问密码
  13. -V 指定MQTT协议版本
  14. –will-payload 指定一个消息,该消息当客户端与broker意外断开连接时发出。该参数需要与–will-topic一起使用
  15. –will-qos Will的QoS值。该参数需要与–will-topic一起使用
  16. –will-retain 指定Will消息被当做一个retain消息(即消息被广播后,该消息被保留起来)。该参数需要与–will-topic一起使用
  17. –will-topic 用户发送Will消息的topic

mosquitto_sub 命令参数说明

  1. -c 设定‘clean session’为无效状态,这样一直保持订阅状态,即便是已经失去连接,如果再次连接仍旧能够接收的断开期间发送的消息。
  2. -d 打印debug信息
  3. -h 指定要连接的域名 默认为localhost
  4. -i 指定clientId
  5. -I 指定clientId前缀
  6. -k keepalive 每隔一段时间,发PING消息通知broker,仍处于连接状态。 默认为60秒。
  7. -q 指定希望接收到QoS为什么的消息 默认QoS为0
  8. -R 不显示陈旧的消息
  9. -t 订阅topic
  10. -v 打印消息
  11. –will-payload 指定一个消息,该消息当客户端与broker意外断开连接时发出。该参数需要与–will-topic一起使用
  12. –will-qos Will的QoS值。该参数需要与–will-topic一起使用
  13. –will-retain 指定Will消息被当做一个retain消息(即消息被广播后,该消息被保留起来)。该参数需要与–will-topic一起使用
  14. –will-topic 用户发送Will消息的topic
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值