【问题标题】:Mosquitto 2.0 config still not working on Raspberry PiMosquitto 2.0 配置仍然无法在 Raspberry Pi 上运行
【发布时间】:2021-12-30 21:37:37
【问题描述】:

我在作为代理和客户端的同一 Raspberry Pi Bullseye (3 A+) 上运行 MQTT 服务器 mosquitto 版本 2.0.11。我有代码工作,但明白需要修改 .conf 文件才能让事情正常工作。我一定还是不明白,因为这是我的文件:

# I had pid_file /run/mosquitto/mosquitto.pid below, but changed this when docs suggested below should be included if running automatically when device boots, which it will be.
pid_file /var/run/mosquitto/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

listener 1883
allow_anonymous true

现在当我尝试像这样运行 mosquitto 时:

mosquitto -c /etc/mosquitto/conf.d/mosquitto.conf

我收到此错误:

1637370455: Loading config file /etc/mosquitto/conf.d/mosquitto.conf
1637370455: Error: Duplicate pid_file value in configuration.
1637370455: Error found at /etc/mosquitto/conf.d/mosquitto.conf:7.
1637370455: Error found at /etc/mosquitto/conf.d/mosquitto.conf:14.

第 7 行是pid_file /var/run/mosquitto/mosquitto.pid 第 14 行是include_dir /etc/mosquitto/conf.d

我可以使用 localhost 进行基本的 pub 和 sub 测试,但主机名仍然没有运气。是的,我知道您应该使用安全性,但我有一个通过本地 WiFi 控制机器人的应用程序,并且希望在不更改该组件的情况下保留应用程序的使用。

任何帮助我回到正轨,让 Mosquitto 经纪人和客户在同一个 pi 上工作,允许匿名访问和运行,非常感谢。我已经浏览了文档、示例文件,并参考了史蒂夫的其他教程,但正确的配置仍然不清楚。谢谢!

【问题讨论】:

  • 检查include_dir 中的文件是否有另一个pid_file 行!
  • 谢谢。我那里只有 mosquitto.conf 和 README。我已经注释掉了包含行,但随后在 PID 行上出现错误。然后在运行时: mosquitto -c /etc/mosquitto/conf.d/mosquitto.conf 我得到 1637421962: Error: Unable to write pid file。还注释掉了 PID 行,但得到:1637422031:错误:无法打开日志文件 /var/log/mosquitto/mosquitto.log 进行写入。但是,似乎服务器当时正在运行,因为我没有返回到提示符。关于摆脱错误的任何想法?
  • 还想知道如何让 mosquitto 在启动时自动使用这个 .conf 文件。我一直在自动运行 mosquitto: sudo systemctl enable mosquitto.service 但这不包括 conf 文件的自动运行,例如: mosquitto -c /etc/mosquitto/conf.d/mosquitto.conf 知道如何修复?

标签: raspberry-pi mqtt mosquitto


【解决方案1】:

首先,无法打开 pid 或日志文件的错误是因为您以普通用户(可能是 pi)运行 mosquitto。此用户无权读取/写入 /var/run/var/log 中的文件,因此当您尝试“手动”运行它时会失败。

你没有说你是如何安装 2.0.11 的,因为 Bullseys 捆绑的默认版本仍然是 1.5.x 版本。假设您使用了 mosquitto.org 存储库,那么 mosquitto 服务将已安装和配置。它会自动选择/etc/mosquitto/mosquitto.conf 处的默认配置文件,如下所示:

$ sudo service mosquitto status
● mosquitto.service - Mosquitto MQTT Broker
   Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor preset
   Active: active (running) since Sun 2021-10-31 17:28:52 GMT; 2 weeks 5 days ag
     Docs: man:mosquitto.conf(5)
           man:mosquitto(8)
  Process: 499 ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto (code=exited
  Process: 505 ExecStartPre=/bin/chown mosquitto /var/log/mosquitto (code=exited
  Process: 507 ExecStartPre=/bin/mkdir -m 740 -p /run/mosquitto (code=exited, st
  Process: 510 ExecStartPre=/bin/chown mosquitto /run/mosquitto (code=exited, st
  Process: 25679 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCE
 Main PID: 511 (mosquitto)
    Tasks: 1 (limit: 2181)
   CGroup: /system.slice/mosquitto.service
           └─511 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Nov 19 00:00:10 www systemd[1]: Reloading Mosquitto MQTT Broker.
Nov 19 00:00:10 www systemd[1]: Reloaded Mosquitto MQTT Broker.
Warning: Journal has been rotated since unit was started. Log output is incomple

启用从其他计算机访问的最简单方法是执行以下操作:

  • 将默认配置文件重置为安装时的状态

     # 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/mosquitto.pid
    
     persistence true
     persistence_location /var/lib/mosquitto/
    
     log_dest file /var/log/mosquitto/mosquitto.log
    
     port 1883
    
     include_dir /etc/mosquitto/conf.d
    
  • /etc/mosquitto/conf.d 中创建一个新文件,例如叫connect.conf

     listener 1883
    
     allow_anonymous true
    
  • sudo service mosquitto restart重启服务

【讨论】:

  • 这非常有效。非常感谢您花时间提供详尽的答案。我教了一门物理计算课程,并且切换到 2.0 使我们用来构建 MQTT 控制机器人的应用程序失败了。从文档中不清楚这两个 .conf 文件是如何相关的。你是最好的。希望大多数 SO 贡献者跟进您的黄金标准质量答案。和平!
猜你喜欢
  • 2017-07-31
  • 2016-01-16
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多