【物联网】mosquitto_connect Connection refused问题解决

00. 目录

在这里插入图片描述

01. 问题描述

当执行mosquitto_pub使用IP地址的时候,出现连接拒绝。

deng@local:~/test$ mosquitto_pub -t 'deng' -h 10.36.100.74 -m 'shenzhen'
Error: Connection refused
deng@local:~/test$ 

02. 问题分析

默认启动mosquitto服务端的时候,使用默认的配置,只允许本地连接,如果需要允许远程连接,需要设置配置文件。

deng@local:/usr/local$ mosquitto
1686562915: mosquitto version 2.0.15 starting
1686562915: Using default config.
1686562915: Starting in local only mode. Connections will only be possible from clients running on this machine.
1686562915: Create a configuration file which defines a listener 
to allow remote access.
1686562915: For more details see https://mosquitto.org/documentation/authentication-methods/
1686562915: Opening ipv4 listen socket on port 1883.
1686562915: Opening ipv6 listen socket on port 1883.
1686562915: mosquitto version 2.0.15 running

03. 问题解决

deng@local:/etc/mosquitto$ pwd
/etc/mosquitto

# 拷贝一个配置文件
deng@local:/etc/mosquitto$ cp mosquitto.conf.example mosquitto.conf 


deng@local:/etc/mosquitto$ pwd
/etc/mosquitto

# 打开配置文件
deng@local:/etc/mosquitto$ sudo vim mosquitto.conf


# 添加以下内容
# =================================================================
# General configuration
# =================================================================

# Use per listener security settings.
#
# It is recommended this option be set before any other options.
#
# If this option is set to true, then all authentication and access control
# options are controlled on a per listener basis. The following options are
# affected:
#
# acl_file
# 端口 和所有的IP
listener 1883 0.0.0.0

# 允许匿名方式
 allow_anonymous true

启动服务端,指定配置文件

deng@local:~/tools/mosquitto-2.0.15$ mosquitto -c /etc/mosquitto/mosquitto.conf


04. 问题验证

启动服务端

deng@local:~/tools/mosquitto-2.0.15$ mosquitto -c /etc/mosquitto/mosquitto.conf
1686563342: mosquitto version 2.0.15 starting
1686563342: Config loaded from /etc/mosquitto/mosquitto.conf.
1686563342: Opening ipv4 listen socket on port 1883.
1686563342: mosquitto version 2.0.15 running

启动客户端

deng@local:~/test$ mosquitto_pub -t 'deng' -h 10.36.100.74 -m 'shenzhen'
deng@local:~/test$ 

05. 附录

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沧海一笑-dj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值