Ubuntu18下开机运行mosquitto

本文主要介绍下载mosquitto源码编译后,配置开机启动mosquitto.

首先讲述下使用apt安装的流程

#引入mosquitto仓库并更新
$sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
$sudo apt-get update

#安装mosquitto包
$sudo apt-get install mosquitto  

#安装mosquitto开发包
$sudo apt-get install libmosquitto-dev

#安装mosquitto客户端
$sudo apt-get install mosquitto-clients

重启后就可以进行测试

#开终端1输入
$mosquitto_sub -h localhost -t "mqtt" -v
#再一开终端2输入
$mosquitto_pub -h localhost -t "mqtt" -m "Hello"

会在终端1中收到 Hello


源码编译配置启动:
开机服务如下:

$ cat /etc/systemd/system/mqtt.service 
[Unit]
Description="mqtt_broker"
After=network.target

[Service]
ExecStart=/home/xx/robot_service/mosquitto

[Install]
WantedBy=multi-user.target

配置服务开机启动:

$ sudo systemctl daemon-reload 
$ sudo systemctl enable mqtt.service
Created symlink /etc/systemd/system/multi-user.target.wants/mqtt.service → /etc/systemd/system/mqtt.service.

重启后会发现mqtt broker 并没有启动原因如下:

~$ sudo systemctl status mqtt
[sudo] password for cruiser: 
● mqtt.service - "mqtt_broker"
   Loaded: loaded (/etc/systemd/system/mqtt.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sat 2022-03-26 19:59:08 CST; 4min 27s ago
  Process: 12183 ExecStart=/home/xx/robot_service/mosquitto (code=exited, status=1/FAILURE)
 Main PID: 12183 (code=exited, status=1/FAILURE)

Mar 26 19:59:08 robot systemd[1]: Started "mqtt_broker".
Mar 26 19:59:08 robot mosquitto[12183]: 1648295948: mosquitto version 1.6.2 starting
Mar 26 19:59:08 robot mosquitto[12183]: 1648295948: Using default config.
Mar 26 19:59:08 robot mosquitto[12183]: 1648295948: Opening ipv4 listen socket on port 1883.
Mar 26 19:59:08 robot mosquitto[12183]: 1648295948: Opening ipv6 listen socket on port 1883.
Mar 26 19:59:08 robot mosquitto[12183]: 1648295948: Error: Invalid user 'mosquitto'.
Mar 26 19:59:08 robot systemd[1]: mqtt.service: Main process exited, code=exited, status=1/FAILURE
Mar 26 19:59:08 robot systemd[1]: mqtt.service: Failed with result 'exit-code'.

处理方法:

  • 要么在配置文件mosquitto.conf中加入root 用户
  • 要么在系统新增mosquitto 用户。
    此处讲述新增mosquitto 用户方法。
 sudo adduser mosquitto

一直按enter键。
查询用户

~$ cat /etc/passwd|cut -f 1 -d:|grep mos
mosquitto

重新启动mqtt.service

~$ sudo systemctl start mqtt.service
~$ sudo systemctl status mqtt.service 
● mqtt.service - "mqtt_broker"
   Loaded: loaded (/etc/systemd/system/mqtt.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2022-03-26 20:26:42 CST; 31s ago
 Main PID: 13063 (mosquitto)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/mqtt.service
           └─13063 /home/xx/robot_service/mosquitto

Mar 26 20:26:42 robot systemd[1]: Started "mqtt_broker".
Mar 26 20:26:42 robot mosquitto[13063]: 1648297602: mosquitto version 1.6.2 starting
Mar 26 20:26:42 robot mosquitto[13063]: 1648297602: Using default config.
Mar 26 20:26:42 robot mosquitto[13063]: 1648297602: Opening ipv4 listen socket on port 1883.
Mar 26 20:26:42 robot mosquitto[13063]: 1648297602: Opening ipv6 listen socket on port 1883.

可以看出运行成功。

你可能感兴趣的:(Ubuntu18下开机运行mosquitto)