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

一、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、解压 编译 安装

tar -zxvf mosquitto-1.6.8.tar.gz
cd mosquitto-1.6.8
make
make install

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

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

4、创建配置文件

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

配置文件中默认使用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

 

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

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

  

6、设置用户名密码

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

编辑配置文件

vim /etc/mosquitto/mosquitto.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.
650 #
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

然后重启即可


7、启动代理服务

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

8、mosquitto 终端命令

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

打开一个订阅者

# 无密码
mosquitto_sub -t test1
# 有密码
mosquitto_sub -u ycgl -P ycglmosquitto123 -t test1

打开一个发布者

# 无密码
mosquitto_pub -t test1 -m "发布内容"
# 有密码
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文件夹中

cd /home
tar -zxvf cJSON-1.3.2.tar.gz
cd cJSON-1.3.2
make
make install

 

                       

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值