Docker 安装MQTT ,并设置用户名和密码

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。

设备端与阿里云IoT平台建立MQTT通道的CONNECT报文有三个关键参数:客户端标识符 (ClientId) ,用户名(User Name),密码(Password)。这三个参数由设备三元组(productKey,deviceName,deviceSecret)按照阿里云IoT签名规则生成。 参数 生成方式 描述 username deviceName+"&"+productKey password sign_hmac(deviceSecret,content) sign_hmac为mqttClientId中的signmethod content为 "clientId${id}deviceName${deviceName}productKey${productKey}timestamp${timestamp}" 其中 id:表示客户端ID,64字符内。 timestamp:表示当前时间毫秒值。 clientId id+"|securemode=3,signmethod=hmacsha1,timestamp="+timestamp+"|" id:表示客户端ID,64字符内。 timestamp:表示当前时间毫秒值。 securemode:表示安全模式:可选值有2(TLS加密)和3(非加密) signmethod:表示签名算法类型。支持hmacmd5,hmacsha1和hmacsha256 2. 三元组接入示例 2.1 设备三元组信息 设备三元组 productKey = a14Xib5kdYd deviceName = light1983432 deviceSecret = oLyaKqVxtRvjH284LdhqVgVUx1UPy6zq 建立MQTT连接时参数 clientId = SN1928339 timestamp = 1539421321846 signmethod = hmacsha1 2.2 参数生成结果 mqttClientId = SN1928339|securemode=3,signmethod=hmacsha1,timestamp=1539421321846| mqttUsername = light1983432&a14Xib5kdYd mqttPassword = b2488041f64f425016b467ee1c94959ebd592ad1 生成password的content content=clientIdSN1928339deviceNamelight1983432productKeya14Xib5kdYdtimestamp1539421321846 2.3 建立连接 mqttClientId作为MQTT客户端标识符 (ClientId) mqttUsername作为MQTT用户名(User Name) mqttPassword作为MQTT密码(Password) 在线Password生成算法验证
要使用docker-compose安装和配置MQTT,你可以按照以下步骤进行操作: 1. 首先,在你的项目目录下创建一个名为docker-compose.yml的文件,并在其中添加以下内容: ``` version: "3.7" services: mqtt: image: eclipse-mosquitto:2 container_name: mqtt privileged: true ports: - 1883:1883 - 9001:9001 volumes: - ./config:/mosquitto/config - ./data:/mosquitto/data - ./log:/mosquitto/log ``` 这将定义一个名为mqtt的服务,并使用eclipse-mosquitto:2镜像来运行Mosquitto MQTT代理。它还指定了容器的名称、映射的端口和卷。 2. 在项目目录下创建一个config文件夹,并在其中创建一个名为mosquitto.conf的文件。在该文件中,你可以配置Mosquitto代理的一些参数。例如,可以使用以下配置: ``` persistence true listener 1883 persistence_location /mosquitto/data log_dest file /mosquitto/log/mosquitto.log ``` 这将启用持久化,配置监听端口和指定日志文件的位置。 3. 启动Mosquitto服务。在终端中导航到你的项目目录,并运行以下命令: ``` docker-compose up -d ``` 这将启动Mosquitto MQTT代理容器,并将其作为后台服务运行。 4. 设置用户名密码。进入MQTT容器内部,可以使用以下命令: ``` docker exec -it mqtt sh ``` 然后,创建密码文件并设置权限: ``` touch /mosquitto/config/pwfile.conf chmod -R 755 /mosquitto/config/pwfile.conf ``` 接下来,使用mosquitto_passwd命令创建用户名密码。例如,可以运行以下命令来创建名为admin的用户和密码admin123: ``` mosquitto_passwd -b /mosquitto/config/pwfile.conf admin admin123 ``` 最后,退出容器: ``` exit ``` 5. 重新启动Mosquitto服务以应用更改: ``` docker-compose restart ``` 现在,你可以使用MQTT客户端(如MQTTX)连接到Mosquitto MQTT代理,并使用设置用户名密码进行身份验证。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [使用docker-compose 搭建mqtt服务器](https://blog.csdn.net/xy3233/article/details/126728187)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [docker-compose安装mqtt](https://blog.csdn.net/xiaohanshasha/article/details/127666240)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天道酬勤~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值