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$