# 3. 使用Docker部署MQTT平台mosquitto到云服务器 #### 1. 拉取Mosquitto Docker镜像(这里选择拉取1.6.14版本,因为最新版本报错Address not available) \`\`\`shell docker pull eclipse-mosquitto # 或者拉取1.6.14版本 docker pull eclipse-mosquitto:1.6.14 \`\`\` ### 2. 创建mosquitto配置文件 在运行容器之前我们需要先创建一个配置文件,定义mosquitto的日志、配置等文件的存储路径,我们在服务器的根目录下创建一个文件夹。 \`\`\`shell cd / # 创建文件夹 mkdir mosquitto # 创建配置文件夹、日志文件夹 mkdir -p /mosquitto/config mkdir -p /mosquitto/data mkdir -p /mosquitto/log \`\`\` 在config文件夹下编辑配置文件mosquitto.conf,并添加下列配置内容; \`\`\`shell # 开启持久化 persistence true # 指定持久化存储的路径 persistence_location /mosquitto/data # 指定日志文件的存储路径 log_dest file /mosquitto/log/mosquitto.log # 定义mosquitto监听的tcp端口 listener 1883 \`\`\` ==注意==:listener的端口也可改成其他的(如果后面报错"Address in use",同时端口又没有被占用的话可以试一下) 这个示例配置指定了MQTT代理监听的端口、持久化数据存储的位置以及日志文件的位置。存储地址可以根据自己的需求进行修改和调整。 为目录授予权限 \`\`\`shell chmod -R 755 /mosquitto chmod -R 777 /mosquitto/log \`\`\` ### 3. 创建mosquitto容器运行 在终端或命令行界面中运行以下命令来运行Mosquitto容器: \`\`\`shell docker run -it --name=mosquitto --privileged -p 1883:1883 -p 9001:9001 -v /mosquitto/config:/mosquitto/config -v /mosquitto/data:/mosquitto/data -v /mosquitto/log:/mosquitto/log -d eclipse-mosquitto \`\`\` !\[image-20240330160134416\](https://hgh-typora-image.oss-cn-guangzhou.aliyuncs.com/img/image-20240330160134416.png) ==注意:== 一定要看清楚本地文件路径 \> -itd:以交互模式运行容器,启动之后在后台运行,方便查看输出日志和控制台信息。 \> \> --privileged=true: 给予容器赋予root级别权限 \> \> -p 1883:1883:将宿主机的1883端口映射到容器内的1883端口,以便可以通过该端口与MQTT代理进行通信。 \> --name 容器名称 \> -v /mosquitto/config:/mosquitto/config:将本地的mosquitto-config文件夹挂载到容器内的/mosquitto/config路径,用于配置文件的持久化存储。 \> -v /mosquitto/data:/mosquitto/data:将本地的mosquitto-data文件夹挂载到容器内的/mosquitto/data路径,用于数据的持久化存储。 \> -v /mosquitto/log:/mosquitto/log:将本地的mosquitto-log文件夹挂载到容器内的/mosquitto/log路径,用于日志的持久化存储。 \> -d eclipse-mosquitto:指定要运行的Mosquitto镜像。 根据实际情况,将/mosquitto/config、/mosquitto/data和/mosquitto/log替换为你本地系统上的相应路径。 运行该命令后,Docker将会创建并运行Mosquitto容器,配置文件和数据将被挂载到容器中,使得配置的持久化和数据的保存得以实现。 看到mosquitto的容器名称证明启动成功。 \`\`\`shell docker ps 或者 docker ps -a \`\`\` \*\*配置权限\*\* 在/mosquitto/config/mosquitto.conf文件下添加: \`\`\`shell # 关闭匿名模式 allow_anonymous false # 指定密码文件 password_file /mosquitto/config/pwfile.conf \`\`\` \*\*进入容器\*\* \`\`\`shell docker exec -it \[容器名 或 容器ID\] sh \`\`\` \*\*生成密码\*\* \`\`\`shell touch /mosquitto/config/pwfile.conf chmod -R 755 /mosquitto/config/pwfile.conf \`\`\` 使用mosquitto_passwd命令创建用户,第一个hgh是用户名,第二个123456是密码。 \`\`\`shell mosquitto_passwd -b /mosquitto/config/pwfile.conf hgh 123456 \`\`\` !\[image-20240330160310247\](https://hgh-typora-image.oss-cn-guangzhou.aliyuncs.com/img/image-20240330160310247.png) \*\*查看日志是否启动成功\*\* \`\`\`shell mosquitto -v \`\`\` \*\*这是使用最新版本的问题,报错了。。。具体原因不明\*\* !\[image-20240330212159083\](https://hgh-typora-image.oss-cn-guangzhou.aliyuncs.com/img/image-20240330212159083.png) \*\*使用旧版本后:\*\* !\[image-20240331122834841\](https://hgh-typora-image.oss-cn-guangzhou.aliyuncs.com/img/image-20240331122834841.png) \*\*重启mqtt服务\*\* \`\`\`shell docker restart 容器ID \`\`\` \*\*可能用到的命令\*\* (1) 查看容器的挂载目录 \`\`\`shell docker inspect 容器名或ID \| grep Source -A 1 \`\`\` (2) 进入容器内部 \`\`\`shell docker exec -it \[容器名 或 容器ID\] bash \`\`\` 若报错:OCI runtime exec failed: exec failed: unable to start container process: exec: "bash": executable file not found in $PATH: unknown。可能是因为制作镜像时使用了精简版,只装了sh命令,未安装bash 可执行: \`\`\`shell docker exec -it \[容器名 或 容器ID\] sh \`\`\` (3) 设置容器自启动 \`\`\`shell docker update --restart=always \`\`\` (4)查看容器报错日志 \`\`\`shell docker logs 容器ID \`\`\` (5)删除镜像 \`\`\`shell docker rmi \[imageId\] \`\`\` (6)重启docker \`\`\`shell systemctl restart docker \`\`\` ##### attach与exec的主要区别 1、attach直接进入容器"启动命令"的终端,不会启动新的进程; 2、exec则是在容器中打开新的终端,并且可以启动新的进程; 3、如果想直接在终端中查看容器"启动命令"的输出,用attach;其他情况使用exec。