原標題:解決Mosquitto創建MQTT服務器提示Starting in local only mode

一、問題現象

使用新下載的Mosquitto創建MQTT服務器提示Starting in local only mode. Connections will only be possible from clients running on this machine.測試發現服務器只能接入本機的客戶端,而其他設備上的客戶端在連接時直接被重置了。

二、原因分析

隨後查閱了一下Mosquitto的更新記錄,原來在其2.0.0大版本更新後如果不加載配件文件則使用迴環接口(僅可用於本地Socket通信)。

Breaking changes
· When the Mosquitto broker is run without configuring any listeners it will now bind to the loopback interfaces 127.0.0.1 and/or ::1. This means that only connections from the local host will be possible.
· Running the broker as mosquitto or mosquitto -p 1883 will bind to the loopback interface.
· Running the broker with a configuration file with no listeners configured will bind to the loopback interface with port 1883.
· Running the broker with a listener defined will bind by default to 0.0.0.0 / :: and so will be accessible from any interface. It is still possible to bind to a specific address/interface.

附Mosquitto更新記錄: https://mosquitto.org/blog/2020/12/version-2-0-0-released

三、解決方法

先修改軟件安裝目錄中的配置文件mosquitto.conf然後加載配置文件來啓動服務器。

  • 配置端口號,編輯mosquitto.conf,搜索listener去掉行首的#並加上端口號,示例端口爲1883
# On systems that support Unix Domain Sockets, it is also possible
# to create a # Unix socket rather than opening a TCP socket. In
# this case, the port number should be set to 0 and a unix socket
# path must be provided, e.g.
# listener 0 /tmp/mosquitto.sock
#
# listener port-number [ip address/host name/unix socket path]
listener 1883
  • 啓用匿名訪問,若不允許匿名訪問則只有添加客戶端鑑權信息才能接入。爲簡化測試我們啓用匿名訪問:將mosquitto.conf裏的allow_anonymous選項改爲true並保存
# Boolean value that determines whether clients that connect
# without providing a username are allowed to connect. If set to
# false then a password file should be created (see the
# password_file option) to control authenticated client access.
#
# Defaults to false, unless there are no listeners defined in the configuration
# file, in which case it is set to true, but connections are only allowed from
# the local machine.
allow_anonymous true
  • 加載配置文件啓動服務器測試,執行命令mosquitto.exe -c mosquitto.conf -v,此時LOG上已經沒了Starting in local only mode且非本地客戶端可以正常接入了

文章版權歸優易通物聯網所有,轉載請註明出處

相關文章