Mosquitto 2.0 配置仍然无法在 Raspberry Pi 上运行

Mosquitto 2.0 config still not working on Raspberry Pi

我运行在同一个 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 上工作、允许匿名访问和 运行ning 的任何帮助。我已经浏览了文档、示例文件,并参考了其他教程,比如史蒂夫的教程,但正确的配置仍然不清楚。谢谢!

首先,有关无法打开 pid 或日志文件的错误是因为您 运行ning mosquitto 作为普通用户(可能是 pi)。此用户无权 read/write 在 /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