1、环境准备
安装Docker和Debian:快速部署 Docker 同时发布多个网站或服务_usdnfo的专栏-CSDN博客
2、安装MQTT
docker run --name mqtt -d --network=mynet --ip 172.19.12.2 -p 1883:1883 -p 9001:9001 eclipse-mosquitto
3、将默认配置文件复制出来
docker cp mqtt:/mosquitto /home/mqtt
3、修改配置文件
vim /home/mqtt/config/mosquitto.conf
#第一处修改
allow_anonymous false
#第二处修改
password_file /mosquitto/config/pwfile
4、添加密码文件
touch /home/mqtt/config/pwfile
5、删除刚刚创建的容器
docker stop mqtt
docker rm mqtt
6、再次创建容器
docker run --name mqtt -d --network=mynet --ip 172.19.12.2 -p 1883:1883 -p 9001:9001 -v /home/mqtt:/mosquitto eclipse-mosquitto
7、运行MQTT命令创建用户
docker exec -it mqtt mosquitto_passwd -c /mosquitto/config/pwfile admin
8、安装 MQTT.fx
9、测试
pip install paho-mqtt
# !/usr/bin/python
# -*-coding:utf-8-*-
import paho.mqtt.client as mqtt
client = mqtt.Client()
client.username_pw_set("admin", "admin")
HOST = "192.168.0.162"
client.connect(HOST, 1883, 60)
for i in range(100):
client.publish(topic="home/garden/fountain",payload="快过年了 %s"%i)
client.loop_forever()
python ./test.py
收到消息,OK。