MQTT 服务器搭建

举报
苌奥林 发表于 2021/11/30 23:08:21 2021/11/30
【摘要】 MQTT 服务器搭建 版本Ubuntu 20.04系统mosquitto 安装方式 Step1 增加repositorysudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa# More info: https://launchpad.net/~mosquitto-dev/+archive/ubuntu/mosquitto-ppa# ...

MQTT 服务器搭建

版本

安装方式

Step1 增加repository

sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
# More info: https://launchpad.net/~mosquitto-dev/+archive/ubuntu/mosquitto-ppa
# Press [ENTER] to continue or Ctrl-c to cancel adding it.
# Hit:1 http://repo.huaweicloud.com/ubuntu focal InRelease
# Hit:2 http://repo.huaweicloud.com/ubuntu focal-updates InRelease
# Hit:3 http://repo.huaweicloud.com/ubuntu focal-backports InRelease       
# Hit:4 http://repo.huaweicloud.com/ubuntu focal-security InRelease        
# Hit:5 http://ppa.launchpad.net/mosquitto-dev/mosquitto-ppa/ubuntu focal InRelease
# Reading package lists... Done

Step2 更新软件源

sudo apt-get update
# Hit:1 http://repo.huaweicloud.com/ubuntu focal InRelease
# Hit:2 http://repo.huaweicloud.com/ubuntu focal-updates InRelease         
# Hit:3 http://repo.huaweicloud.com/ubuntu focal-backports InRelease       
# Hit:4 http://repo.huaweicloud.com/ubuntu focal-security InRelease        
# Hit:5 http://ppa.launchpad.net/mosquitto-dev/mosquitto-ppa/ubuntu focal InRelease
# Reading package lists... Done

Step3 安装mosquittp

sudo apt-get install mosquitto  
# Reading package lists... Done
# Building dependency tree       
# Reading state information... Done
# The following additional packages will be installed:
# libmosquitto1 libwebsockets18
# The following NEW packages will be installed:
# libmosquitto1 libwebsockets18 mosquitto
# 0 upgraded, 3 newly installed, 0 to remove and 169 not upgraded.
# Need to get 526 kB of archives.
# After this operation, 1300 kB of additional disk space will be used.
# Do you want to continue? [Y/n] y
# Get:1 http://ppa.launchpad.net/mosquitto-dev/mosquitto-ppa/ubuntu focal/main amd64 libmosquitto1 amd64 2.0.14-0mosquitto1~focal1 [92.0 kB]
# ....
# Processing triggers for systemd (245.4-4ubuntu3.4) ...
# Processing triggers for man-db (2.9.1-1) ...
# Processing triggers for libc-bin (2.31-0ubuntu9.2) ...

Step4 查看运行状态

sudo service mosquitto status                               #查看运行状态

● mosquitto.service - Mosquitto MQTT Broker
     Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-11-30 21:54:22 CST; 1min 52s ago
       Docs: man:mosquitto.conf(5)
             man:mosquitto(8)
   Main PID: 88452 (mosquitto)
      Tasks: 1 (limit: 4619)
     Memory: 1.0M
     CGroup: /system.slice/mosquitto.service
             └─88452 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Nov 30 21:54:22 ecs-s6-large-2-linux-20200215160452 systemd[1]: Starting Mosquitto MQTT Broker...
Nov 30 21:54:22 ecs-s6-large-2-linux-20200215160452 systemd[1]: Started Mosquitto MQTT Broker.
root@ecs-s6-large-2-linux-20200215160452:~# 

Step5 修改配置文件

sudo service mosquitto stop # 先停止服务
vim /etc/mosquitto/conf.d/mqtt.conf # 在/etc/mosquitto/conf.d目录下,新建mqtt.conf配置文件
# 输入以下内容(注意,注释信息不能和配置信息放在同一行):

# 禁止匿名访问
allow_anonymous false
# 用户配置文件
password_file /etc/mosquitto/conf.d/user.txt

Step6 增加MQTT服务器用户

mosquitto_passwd -c /etc/mosquitto/conf.d/user.txt mqtt # 用户名为mqtt, 回车后连续输入2次用户密码即可
# Password: 
# Reenter password: 

Step7 重新启动mosquitto

sudo service mosquitto start      #启动服务

Step8 链接测试 (client.py)

# coding=utf-8
# @filename     :client.py
# @time         :2021/11/30 10:26 pm
# @author       :changaolin@gmail.com
# @description  :MQTT Client Demo

import paho.mqtt.client as mqtt


def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))


def on_message(client, userdata, msg):
    print(msg.topic + " " + str(msg.payload))


client = mqtt.Client()
client.username_pw_set("mqtt", "mqtt")
client.on_connect = on_connect
client.on_message = on_message

client.connect("127.0.0.1", 1883, 60)
client.loop_forever()
pip install paho-mqtt==1.6.1
python client.py
# Connected with result code 0 表示链接成功
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。