我基本上遇到了与这帖子相同的问题:我正在Raspberry上运行一个python脚本,Raspbian /Linux10通过paho mqtt与MQTT代理连接,但我得到了Connection refused,,除非我以前调用了mosquitto -d --因为它工作得很好。
这就是我称之为蚊子状态时得到的结果:
pi@raspberrypi:~ $ systemctl status mosquitto
● mosquitto.service - Mosquitto MQTT v3.1/v3.1.1 Broker
Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2020-11-28 12:53:33 CET; 20h ago
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
Process: 486 ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf (code=exited, status=3)
Main PID: 486 (code=exited, status=3)
Nov 28 12:53:33 raspberrypi systemd[1]: mosquitto.service: Service RestartSec=100ms expired, scheduling restart.
Nov 28 12:53:33 raspberrypi systemd[1]: mosquitto.service: Scheduled restart job, restart counter is at 5.
Nov 28 12:53:33 raspberrypi systemd[1]: Stopped Mosquitto MQTT v3.1/v3.1.1 Broker.
Nov 28 12:53:33 raspberrypi systemd[1]: mosquitto.service: Start request repeated too quickly.
Nov 28 12:53:33 raspberrypi systemd[1]: mosquitto.service: Failed with result 'exit-code'.
Nov 28 12:53:33 raspberrypi systemd[1]: Failed to start Mosquitto MQTT v3.1/v3.1.1 Broker.整体错误:
Traceback (most recent call last):
File "mqtt_test.py", line 20, in <module>
client.connect(BROKER_ADDRESS)
File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 941, in connect
return self.reconnect()
File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1075, in reconnect
sock = self._create_socket_connection()
File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 3546, in _create_socket_connection
return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
File "/usr/lib/python3.7/socket.py", line 727, in create_connection
raise err
File "/usr/lib/python3.7/socket.py", line 716, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused来自/etc/mosquitto/conf.d/mosquitto.conf
Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
allow anonymous false
password_file /etc/mosquitto/pwfile
listener 1883来自/var/log/moquitto/mosquitto.log
1601020477: mosquitto version 1.5.7 starting
1601020477: Config loaded from /etc/mosquitto/mosquitto.conf.
1601020477: Opening ipv4 listen socket on port 1883.
1601020477: Opening ipv6 listen socket on port 1883.
1601020533: New connection from ::1 on port 1883.
1601020533: New client connected from ::1 as mosqsub|1315-raspberryp (c1, k60).
1601020675: Socket error on client mosqsub|1315-raspberryp, disconnecting.
1601020862: New connection from ::1 on port 1883.
1601020862: New client connected from ::1 as mosqpub|1333-raspberryp (c1, k60).
1601020862: Client mosqpub|1333-raspberryp disconnected.
1601020978: New connection from ::1 on port 1883.
1601020978: New client connected from ::1 as mosqpub|1344-raspberryp (c1, k60).
1601020978: Client mosqpub|1344-raspberryp disconnected.
1601021092: New connection from ::1 on port 1883.
1601021092: New client connected from ::1 as mosqpub|1579-raspberryp (c1, k60).
1601021092: Client mosqpub|1579-raspberryp disconnected.
1601022277: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601024078: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601025879: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601027680: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601029481: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601031282: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601033083: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601034884: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601036685: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601038486: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601040287: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601042088: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601043889: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601045690: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601047491: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601049292: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601050572: mosquitto version 1.5.7 terminating
1601050591: mosquitto version 1.5.7 starting
1601050591: Config loaded from /etc/mosquitto/mosquitto.conf.
1601050591: Opening ipv4 listen socket on port 1883.
1601050591: Opening ipv6 listen socket on port 1883.
1601309165: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1601309344: mosquitto version 1.5.7 terminating我尝试在下面的python测试代码中传递用户名和密码,但是仍然会引发相同的错误,尽管用户名和密码是正确的(我是通过
sudo mosquitto_passwd -c /etc/mosquitto/pwfile pi。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
def on_message(client, userdata, message):
msg = str(message.payload.decode("utf-8"))
print("message received: ", msg)
print("message topic: ", message.topic)
def on_connect(client, userdata, flags, rc):
client.subscribe('/home/data')
BROKER_ADDRESS = "192.168.178.201"
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.tls_set()
client.username_pw_set(username="pi", password ="password")
client.connect(BROKER_ADDRESS)
print("Connected to MQTT Broker: " + BROKER_ADDRESS)
client.loop_forever()新日志文件:
1606671705: mosquitto version 1.5.7 starting
1606671705: Config loaded from /etc/mosquitto/mosquitto.conf.
1606671705: Opening ipv4 listen socket on port 1883.
1606671705: Opening ipv6 listen socket on port 1883.
1606671781: mosquitto version 1.5.7 terminating
1606671794: mosquitto version 1.5.7 starting
1606671794: Config loaded from /etc/mosquitto/mosquitto.conf.
1606671794: Opening ipv4 listen socket on port 1883.
1606671794: Opening ipv6 listen socket on port 1883.
1606671798: mosquitto version 1.5.7 terminating
1606671837: mosquitto version 1.5.7 starting
1606671837: Config loaded from /etc/mosquitto/mosquitto.conf.
1606671837: Opening ipv4 listen socket on port 1883.
1606671837: Opening ipv6 listen socket on port 1883.
1606671988: mosquitto version 1.5.7 terminating是否有办法解决这个问题,或者在mosquitto -d启动时自动调用RPi?
我试过sudo systemctl enable mosquitto.service,但没有起作用。也没有删除日志文件。
任何帮助都将不胜感激!
发布于 2020-11-29 09:15:26
好的,所以您的mosquitto.conf文件包含一个密码文件,它将执行身份验证。
允许匿名假password_file /etc/蚊虫/pwfile
开始使用-d选项时,不会使用配置文件,因此不会强制执行用户名/密码要求。(可以使用-c命令行选项指定配置文件)
连接拒绝错误的最可能原因是您的python代码没有传递用户名/密码。
https://stackoverflow.com/questions/65048916
复制相似问题