当前位置:   article > 正文

Linux搭建MQTT服务器(mosquitto)并使用_linux 启动mqtt服务

linux 启动mqtt服务

一、Linux搭建MQTT服务器(mosquitto)并使用

1、安装依赖

yum install gcc-c++ cmake openssl-devel libuuid-devel c-ares-devel uuid-devel libwebsockets-devel.x86_64 libwebsockets.x86_64 -y

2、下载mosquitto

官网:https://mosquitto.org/

cd /home
wget --no-check-certificate https://mosquitto.org/files/source/mosquitto-1.6.8.tar.gz

3、解压 编译 安装

  1. tar -zxvf mosquitto-1.6.8.tar.gz
  2. cd mosquitto-1.6.8
  3. make
  4. make install

之后会碰到找不到libmosquitto.so.1这个问题,修改链接路径,重新加载动态链接库

  1. ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
  2. ldconfig

4、创建配置文件

  1. cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
  2. vim /etc/mosquitto/mosquitto.conf

配置文件中默认使用user mosquitto。 如果不想创建此用户,可以修改成root

  1. 192 # When run as root, drop privileges to this user and its primary
  2. 193 # group.
  3. 194 # Set to root to stay as root, but this is not recommended.
  4. 195 # If run as a non-root user, this setting has no effect.
  5. 196 # Note that on Windows this has no effect and so mosquitto should
  6. 197 # be started by the user you wish it to run as.
  7. 198 #user mosquitto
  8. 199 user root

 

5、启动、查看、关闭程序

  1. # 运行程序
  2. mosquitto -c /etc/mosquitto/mosquitto.conf -d
  3. # ps查看
  4. ps -aux | grep mosquitto
  5. # 关闭程序
  6. kill -9 $(pidof mosquitto)

  

6、设置用户名密码

  1. # 我这里用户名是user
  2. mosquitto_passwd -c /etc/mosquitto/pwfile.conf user
  3. # 然后输入俩遍密码即可

编辑配置文件

vim /etc/mosquitto/mosquitto.conf

增加下面内容

  1. 646 # Defaults to true if no other security options are set. If `password_file` or
  2. 647 # `psk_file` is set, or if an authentication plugin is loaded which implements
  3. 648 # username/password or TLS-PSK checks, then `allow_anonymous` defaults to
  4. 649 # false.
  5. 650 #
  6. 651 #allow_anonymous true
  7. 652 allow_anonymous ture
  8. ......
  9. 666 # See the TLS client require_certificate and use_identity_as_username options
  10. 667 # for alternative authentication options. If an auth_plugin is used as well as
  11. 668 # password_file, the auth_plugin check will be made first.
  12. 669 #password_file
  13. 670 password_file /etc/mosquitto/pwfile.conf

然后重启即可


7、启动代理服务

  1. [root@localhost mosquitto]# mosquitto -v
  2. 1712762876: mosquitto version 1.6.8 starting
  3. 1712762876: Using default config.
  4. 1712762876: Opening ipv4 listen socket on port 1883.
  5. 1712762876: Opening ipv6 listen socket on port 1883.

8、mosquitto 终端命令

  1. mosquitto_pub 命令参数说明
  2. -d 打印debug信息
  3. -f 将指定文件的内容作为发送消息的内容
  4. -h 指定要连接的域名 默认为localhost
  5. -i 指定要给哪个clientId的用户发送消息
  6. -I 指定给哪个clientId前缀的用户发送消息
  7. -m 消息内容
  8. -n 发送一个空(null)消息
  9. -p 连接端口号
  10. -q 指定QoS的值(0,1,2)
  11. -t 指定topic
  12. -u 指定broker访问用户
  13. -P 指定broker访问密码
  14. -V 指定MQTT协议版本
  15. --will-payload 指定一个消息,该消息当客户端与broker意外断开连接时发出。该参数需要与--will-topic一起使用
  16. --will-qos Will的QoS值。该参数需要与--will-topic一起使用
  17. --will-retain 指定Will消息被当做一个retain消息(即消息被广播后,该消息被保留起来)。该参数需要与--will-topic一起使用
  18. --will-topic 用户发送Will消息的topic
  1. mosquitto_sub 命令参数说明
  2. -c 设定‘clean session’为无效状态,这样一直保持订阅状态,即便是已经失去连接,如果再次连接仍旧能够接收的断开期间发送的消息。
  3. -d 打印debug信息
  4. -h 指定要连接的域名 默认为localhost
  5. -i 指定clientId
  6. -I 指定clientId前缀
  7. -k keepalive 每隔一段时间,发PING消息通知broker,仍处于连接状态。 默认为60秒。
  8. -q 指定希望接收到QoS为什么的消息 默认QoS为0
  9. -R 不显示陈旧的消息
  10. -t 订阅topic
  11. -v 打印消息
  12. --will-payload 指定一个消息,该消息当客户端与broker意外断开连接时发出。该参数需要与--will-topic一起使用
  13. --will-qos Will的QoS值。该参数需要与--will-topic一起使用
  14. --will-retain 指定Will消息被当做一个retain消息(即消息被广播后,该消息被保留起来)。该参数需要与--will-topic一起使用
  15. --will-topic 用户发送Will消息的topic

打开一个订阅者

  1. # 无密码
  2. mosquitto_sub -t test1
  3. # 有密码
  4. mosquitto_sub -u ycgl -P ycglmosquitto123 -t test1

打开一个发布者

  1. # 无密码
  2. mosquitto_pub -t test1 -m "发布内容"
  3. # 有密码
  4. mosquitto_pub -u ycgl -P ycglmosquitto123 -t test1 -m "发布内容"

相同topic的双方,发布者pub发送 “发布内容”给订阅者sub

二、安装过程中报错解决


1.mosquitto_ctrl.h:21:25: 致命错误:cjson/cJSON.h:没有那个文件或目录

解决方法:缺少cJSON库,安装cJSON库即可。

cJSON下载地址:https://github.com/arnoldlu/cJSON

我用的是这个版本:https://codeload.github.com/arnoldlu/cJSON/tar.gz/refs/tags/v1.3.2

将下载的cJSON-x.x.x.tar.gz压缩包上传到/home 文件夹中,并解压到cJSON文件夹中

  1. cd /home
  2. tar -zxvf cJSON-1.3.2.tar.gz
  3. cd cJSON-1.3.2
  4. make
  5. make install

 

                       

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/930677
推荐阅读
相关标签
  

闽ICP备14008679号