MQTT服务器部署

 

安装 Mosquitto

https://learningsky.io/install-mosquitto-mqtt-message-broker-on-raspberry-pi-and-python-publish-subscription/

systemctl daemon-reload
使用systemctl命令来控制frpc
#启动
sudo systemctl start frpc
#关闭
sudo systemctl stop frpc
#重启
sudo systemctl restart frpc
#查看状态
sudo systemctl status frpc
设置开机自动启动
sudo systemctl enable frpc

stevenroc@stevenroc:~ $ mosquitto -v

726890289: mosquitto version 2.0.11 starting

步骤 1. 获取终端更新服务器的套件文件清单

一、

请在终端机输入下方指令

sudo apt-get update

sudo apt update && sudo apt upgrade -y

 

二、

sudo apt-get install -y mosquitto mosquitto-clients

 

三、

1设置 Mosquitto

步骤1.获取Raspberry PiIP

hostname -I

192.168.110.15

 

2、修改IP地址192.168.110.15

sudo vi /etc/mosquitto/mosquitto.conf

allow_anonymous true

##listener 1883 192.168.110.15

#listener 1883 Rpi-IP

#allow_anonymous true
#allow_anonymous false
#listener 1883 192.168.110.15
#password_file /etc/mosquitto/pwdfile

allow_anonymous false
listener 1883
protocol mqtt
password_file /etc/mosquitto/pwdfile

# Websockets
listener 8083
protocol websockets
#后面增加SSL 证书配置文件支持ssl访问连接

3延迟启动

sudo systemctl enable mosquitto

 

sudo systemctl restart mosquitto

sudo systemctl status mosquitto

 

sudo service mosquitto stop

sudo service mosquitto start

4

mosquitto_sub -h Rpi-IP -t msg/info

mosquitto_sub -h 192.168.110.15 -t msg/info

 

5

mosquitto_pub -h Rpi-IP -t msg/info -m “hello world”

mosquitto_pub -h 192.168.110.15 -t msg/info -m “hello world”

 6、增加第2个用户名 mqtt 和密码 mqtt1289A

stevenroc@stevenroc:/opt/openAI/frp $ sudo mosquitto_passwd /etc/mosquitto/pwdfile mqtt
Password:
Reenter password:
stevenroc@stevenroc:/opt/openAI/frp $

用户名:mqtt

密码:mqtt1289A

四、python3编译py脚本

pip install paho-mqtt==1.6.1 –break-system-packages

 

五、sub订阅

cd /opt/openAI/project/002.mqtt

sudo nano mqttsub.py                                                                                    

import paho.mqtt.client as mqtt 

import json

 

MQTT_SERVER = “192.168.110.15” 

MQTT_PORT = 1883 

MQTT_ALIVE = 60 

MQTT_TOPIC = “msg/info”

 

def on_connect(client, userdata, flags, rc):

    print(“Connected with result code “+str(rc))

    client.subscribe(MQTT_TOPIC)

 

def on_message(client, userdata, msg):

    # print(f”{msg.topic}: {msg.payload.decode(‘utf-8’)}”

    print(f”{msg.topic} – data1: {json.loads(msg.payload)[‘data1’]}, data2: {json.loads(msg.payload)[‘data2’]}”)

 

mqtt_client = mqtt.Client()

mqtt_client.on_connect = on_connect

mqtt_client.on_message = on_message

mqtt_client.connect(MQTT_SERVER, MQTT_PORT, MQTT_ALIVE) 

mqtt_client.loop_forever()

 

六、发布

cd /opt/openAI/project/002.mqtt

sudo nano mqttpub.py

 

import paho.mqtt.client as mqtt 

import json

import random

 

MQTT_SERVER = “192.168.110.15”

MQTT_PORT = 1883 

MQTT_ALIVE = 60 

MQTT_TOPIC = “msg/info”

 

mqtt_client = mqtt.Client() 

mqtt_client.connect(MQTT_SERVER, MQTT_PORT, MQTT_ALIVE)   

 

def publish():

  payload = {

    ‘data1’: random.random(),

    ‘data2’: random.random()

  }

  print(f”payload: {payload}”)

  mqtt_client.publish(MQTT_TOPIC, json.dumps(payload), qos=1)

  mqtt_client.loop(2,10)

 

no = 1

 

while no < 51:

  publish()

  no = no + 1

 

七、测试

stevenroc@stevenroc:/opt/openAI/project/002.mqtt $ python mqttsub.py

stevenroc@stevenroc:/opt/openAI/project/002.mqtt $ python mqttpub.py

 

一、错误如下

stevenroc@stevenroc:~ $ pip install paho-mqtt==1.6.1

error: externally-managed-environment

 

× This environment is externally managed

╰─> To install Python packages system-wide, try apt install

    python3-xyz, where xyz is the package you are trying to

    install.

   

    If you wish to install a non-Debian-packaged Python package,

    create a virtual environment using python3 -m venv path/to/venv.

    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make

    sure you have python3-full installed.

   

    For more information visit http://rptl.io/venv

 

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing –break-system-packages.

解决如下:

https://www.cnblogs.com/zhaohuaxishi/p/17515019.html

解决使用pip3 install 时出现的“error: externally-managed-environment”方案

  当我使用pip3 install 来安装一些需要使用到的包时,出现了如下错误:

pip install paho-mqtt==1.6.1 –break-system-packages

 

 

 

一、mosquitto.service服务延迟10s启动

mosquitto查找服务文件位置 mosquitto.service

stevenroc@stevenroc:/ $ sudo find / -name mosquitto.service

/sys/fs/cgroup/system.slice/mosquitto.service

/etc/systemd/system/multi-user.target.wants/mosquitto.service

find: /run/user/1000/gvfs: Permission denied

find: /run/user/1000/doc: Permission denied

/usr/lib/systemd/system/mosquitto.service

/var/lib/systemd/deb-systemd-helper-enabled/multi-user.target.wants/mosquitto.service

mosquitto.service

二、查找快捷方式文件具体位置

stevenroc@stevenroc:/ $ sudo ls -l /etc/systemd/system/multi-user.target.wants

total 0

lrwxrwxrwx 1 root root 35 Aug 29 20:46 apache2.service -> /lib/systemd/system/apache2.service

lrwxrwxrwx 1 root root 37 Sep 17 10:54 mosquitto.service -> /lib/systemd/system/mosquitto.service

 

三、修改mosquitto服务延迟启动

/lib/systemd/system/mosquitto.service

sudo nano /lib/systemd/system/mosquitto.service

[Unit]

Description=Mosquitto MQTT Broker

Documentation=man:mosquitto.conf(5) man:mosquitto(8)

After=network.target

Wants=network.target

 

[Service]

Type=notify

NotifyAccess=main

 

ExecStartPre=/bin/sleep 10

#Restart=on-failure

RestartSec=10s

 

ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

ExecReload=/bin/kill -HUP $MAINPID

Restart=on-failure

ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto

ExecStartPre=/bin/chown mosquitto /var/log/mosquitto

ExecStartPre=/bin/mkdir -m 740 -p /run/mosquitto

ExecStartPre=/bin/chown mosquitto /run/mosquitto

 

[Install]

WantedBy=multi-user.target

 

八、mqtt连接、mqtt ws web连接

 

mqtt mosquitto  mqtt web配置

#allow_anonymous true
#allow_anonymous false
#listener 1883 192.168.110.15
#password_file /etc/mosquitto/pwdfile

allow_anonymous false
listener 1883
protocol mqtt
password_file /etc/mosquitto/pwdfile

# Websockets
listener 8083
protocol websockets

 

九、mqtt测试 (用户名密码测试)

1、mosquitto

MQTT服务器部署

2、mqtt客户端测试

 

3、mqtt客户端测试

 

4、

https://www.cnblogs.com/zkwarrior/p/10950294.html
https://blog.csdn.net/qq_22111417/article/details/84142509

设置用户名和密码

找到用户密码文件在安装bin下:

1: 打开mosquitto.conf文件,找到allow_anonymous节点,这个节点作用是,是否开启匿名用户登录,默认是true。打开此项配置(将前面的 # 号去掉)之后将其值改为true

修改前:#allow_anonymous

修改后:allow_anonymous false

2: 找到password_file节点,这个节点是告诉服务器你要配置的用户将存放在哪里。打开此配置并指定pwfile.example文件路劲(注意是绝对路劲)

修改前:#password_file

修改后:password_file /etc/mosquitto/pwfile.example (这里的地址根据自己文件实际位置填写)

3: 创建用户名和密码、打开命令窗口 键入如下命令:

mosquitto_passwd -c /etc/mosquitto/pwfile.example admin
提示连续两次输入密码、创建成功。命令解释: -c 创建一个用户、/etc/mosquitto/pwfile.example 是将用户创建到 pwfile.example 文件中、admin 是用户名。

4: 创建mosquitto用户。在命令窗口键入如下命令:

mosquitto_passwd /etc/mosquitto/pwfile.example mosquitto
同样连续会提示连续输入两次密码。注意第二次创建用户时不用加 -c 如果加 -c 会把第一次创建的用户覆盖。

至此两个用户创建成功,此时如果查看 pwfile.example 文件会发现其中多了两个用户。

mosquitto_sub.exe -h 127.0.0.1 -p 1883 -u admin -P 111 –cafile G:/mosquitto/InstallMosquitto/OpenSSL-Win64/bin/ca.crt -v -t #
mosquitto_pub.exe -h 127.0.0.1 -p 1883 -u admin -P 111 –cafile G:/mosquitto/InstallMosquitto/OpenSSL-Win64/bin/ca.crt -t topic -m “hello world”
mosquitto.exe -v -c mosquitto.conf

六、

Mosquitto用户名密码配置-MQTT

tuzirou

于 2024-01-09 14:53:27 发布

阅读量4.1k
收藏 19

点赞数 12
文章标签: 网络协议
版权
Mosquitto服务器搭建成功之后,可以考虑设置用户名与密码,增加服务其安全性。

方法如下:

1、找到并打开Mosquitto服务器的配置文件 /etc/mosquitto/mosquitto.conf

2、将其中的配置选项allow_anonymous改为 false,禁止匿名登录

allow_anonymous false
3、将密码配置选项配置如下:

password_file /etc/mosquitto/pwfile
4、如果该目录下没有该文件,则进入该目录,并拷贝一份,命令如下:

cp pwfile.example pwfile
5、添加用户信息。在终端执行以下代码,应用mosquitto_passwd命令创建用户名

mosquitto_passwd -c /etc/mosquitto/pwfile username
执行以后会提示输入密码,重复2次输入之后,用户名密码配置完成。

6、重新启动mosquitto服务之后,用户名密码生效

mosquitto -c /etc/mosquitto/mosquitto.conf
————————————————

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/tuzirou/article/details/135480278

您可能还喜欢...

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注