1. 安装mosquitto

    sudo apt install mosquitto
    sudo apt install libmosquitto-dev
    sudo apt install mosquitto-clients
    
  2. 启动mosquitto
    默认安装mosquitto后就会自动启动mosquitto进程,其配置文件在/etc/mosquitto下,默认监听端口为1883
    /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
    查看mosquitto的启动命令

    # mosquitto -h
    mosquitto version 1.4.15 (build date Tue, 18 Jun 2019 11:42:22 -0300)mosquitto is an MQTT v3.1.1/v3.1 broker.Usage: mosquitto [-c config_file] [-d] [-h] [-p port]-c : specify the broker config file.   (配置文件路径)-d : put the broker into the background after starting. (后台运行)-h : display this help. (显示帮助)-p : start the broker listening on the specified port.Not recommended in conjunction with the -c option. (指定端口)-v : verbose mode - enable all logging types. This overridesany logging options given in the config file. (打印日志)See http://mosquitto.org/ for more information.
    

    执行:mosquitto -p 5678 -v,在本地端口5678上启动mosquitto服务,并在前台运行打印日志

  3. 订阅
    使用mosquitto_sub命令启动订阅,查看mosquitto_sub的启动命令

    # mosquitto_sub --help
    mosquitto_sub is a simple mqtt client that will subscribe to a single topic and print all messages it receives.
    mosquitto_sub version 1.4.15 running on libmosquitto 1.4.15.Usage: mosquitto_sub [-c] [-h host] [-k keepalive] [-p port] [-q qos] [-R] -t topic ...[-C msg_count] [-T filter_out][-A bind_address] [-S][-i id] [-I id_prefix][-d] [-N] [--quiet] [-v][-u username [-P password]][--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]][{--cafile file | --capath dir} [--cert file] [--key file][--ciphers ciphers] [--insecure]][--psk hex-key --psk-identity identity [--ciphers ciphers]][--proxy socks-url]mosquitto_sub --help-A : bind the outgoing socket to this host/ip address. Use to control which interfacethe client communicates over.-c : disable 'clean session' (store subscription and pending messages when client disconnects).-C : disconnect and exit after receiving the 'msg_count' messages.-d : enable debug messages.-h : mqtt host to connect to. Defaults to localhost.-i : id to use for this client. Defaults to mosquitto_sub_ appended with the process id.-I : define the client id as id_prefix appended with the process id. Useful for when thebroker is using the clientid_prefixes option.-k : keep alive in seconds for this client. Defaults to 60.-N : do not add an end of line character when printing the payload.-p : network port to connect to. Defaults to 1883.-P : provide a password (requires MQTT 3.1 broker)-q : quality of service level to use for the subscription. Defaults to 0.-R : do not print stale messages (those with retain set).-S : use SRV lookups to determine which host to connect to.-t : mqtt topic to subscribe to. May be repeated multiple times.-T : topic string to filter out of results. May be repeated.-u : provide a username (requires MQTT 3.1 broker)-v : print published messages verbosely.-V : specify the version of the MQTT protocol to use when connecting.Can be mqttv31 or mqttv311. Defaults to mqttv31.--help : display this message.--quiet : don't print error messages.--will-payload : payload for the client Will, which is sent by the broker in case ofunexpected disconnection. If not given and will-topic is set, a zerolength message will be sent.--will-qos : QoS level for the client Will.--will-retain : if given, make the client Will retained.--will-topic : the topic on which to publish the client Will.--cafile : path to a file containing trusted CA certificates to enable encryptedcertificate based communication.--capath : path to a directory containing trusted CA certificates to enable encryptedcommunication.--cert : client certificate for authentication, if required by server.--key : client private key for authentication, if required by server.--ciphers : openssl compatible list of TLS ciphers to support.--tls-version : TLS protocol version, can be one of tlsv1.2 tlsv1.1 or tlsv1.Defaults to tlsv1.2 if available.--insecure : do not check that the server certificate hostname matches the remotehostname. Using this option means that you cannot be sure that theremote host is the server you wish to connect to and so is insecure.Do not use this option in a production environment.--psk : pre-shared-key in hexadecimal (no leading 0x) to enable TLS-PSK mode.--psk-identity : client identity string for TLS-PSK mode.--proxy : SOCKS5 proxy URL of the form:socks5h://[username[:password]@]hostname[:port]Only "none" and "username" authentication is supported.See http://mosquitto.org/ for more information.
    

    重要的选项:
    -h 指定连接mqtt服务的ip地址,默认为localhost
    -p 指定连接mqtt服务的端口
    -t 指定订阅主题(主题:mqtt中的最重要的概念)

    执行命令: mosquitto_sub -v -p 5678 -t topic, 订阅topic主题

  4. 发布
    使用mosquitto_pub命令发布,查看该命令的帮助与mosquitto_sub基本类似,使用同样的选项指定mqtt服务的ip地址,端口,以及发布主题等。
    -m 选项: 发布消息的内容
    执行命令: mosquitto_pub -p 5678 -t topic -m hello

  5. 运行结果
    执行发布命令后,在订阅窗口中打印接收到hello消息

ubuntu搭建mqtt服务器及验证相关推荐

  1. Ubuntu搭建Mqtt服务器

    快速上手mosquitto在ubuntu18.04下的安装及测试_小刀_的博客-CSDN博客 0.基本认识 每个机器可以订阅,发布.谁订阅了某个topic,消息就会发给谁. 比如机器0订阅了msg1, ...

  2. linux如何搭建mqtt服务器,【MQTT】在Ubuntu下搭建MQTT服务器

    前言 博主之前写了一篇<在Windows下搭建MQTT服务器>,这次要尝试在Ubuntu下搭建MQTT服务器.实际上,下载好源码包后,后面的都和那篇文章差不多了. 开发环境 虚拟机 Ubu ...

  3. Ubuntu搭建Anki服务器

    Ubuntu搭建Anki服务器 第一步安装Anki 阿里云的服务器,xshell远程登录上 1 #以root用户进行操作 2 3 #安装Akni服务 4 easy_install AnkiServer ...

  4. Ubuntu16.04LTS环境下搭建MQTT服务器以及QT实现MQTT客户端

    MQTT服务器搭建 使用EMQX搭建MQTT服务器,EMQX 4.2.14 for Ubuntu 16.04下载 下载zip包,选择安装方式为zip,cpu架构选择x86_64 按照网页的指示进行下载 ...

  5. UbuntuServer搭建MQTT服务器

    一.MQTT协议简介 MQTT 是一个基于发布/订阅模式的消息传输协议.它具有轻量级.开放.简单,易于实现,通信带宽要求低等特点.这些特点使得它对机器与机器的通信(M2M)以及物联网应用(IoT)来说 ...

  6. nodejs快速搭建MQTT服务器

    简介 MQTT(消息队列遥测传输)是ISO 标准(ISO/IEC PRF 20922)下基于发布/订阅范式的消息协议.它工作在TCP/IP协议族上,是为硬件性能低下的远程设备以及网络状况糟糕的情况下而 ...

  7. Windows下搭建MQTT服务器

    Windows下搭建MQTT服务器 1.MQTT服务器软件筛选 2.MQTT服务器软件mosquitto下载 3.修改mosquitto配置,并启动mosquitto服务 4.利用mosquitto工 ...

  8. Windows搭建MQTT服务器

    Win10下搭建MQTT服务器 一.获取软件包 链接:https://pan.baidu.com/s/1sKsL3ninhpwiawm69cOQ9w 提取码:9vhm mqtt服务器安装包为apach ...

  9. Ubuntu搭建NFS服务器,NFS协议详细分析

    目录 1. Ubuntu搭建NFS服务器 2. NFS协议分析 2.1 实验拓扑: 2.2 在kali抓包分析 1. Ubuntu搭建NFS服务器 ​ NFS(Network FileSystem,网 ...

最新文章

  1. 网络推广外包——网络推广外包网站专员如何避免“网站过度优化”
  2. Linux DMA 驱动学习总结
  3. golang导入包的几个说明:import
  4. js 对 URL 参数进行 加密 解密
  5. (一二六)第十一章编程练习
  6. JavaScript 设计模式核⼼原理与应⽤实践之单例模式——Vuex的数据管理哲学
  7. IT职场人生系列之七:学外语
  8. 廖雪峰Java1-1Java入门-java简介
  9. error: #error This file requires compiler and library support for the ISO C++ 2011 standard.
  10. 代码的自动生成...
  11. 基于51单片机的呼吸灯程序编写
  12. honeywell Xenon TM 1900二维码扫描枪扫描不显示中文
  13. scratch迷宫小游戏
  14. 内存管理API之get_unmapped_area
  15. power apps 里 嵌套 html
  16. ps使用教程 核心蒙版
  17. 记录学习Android基础的心得07:硬件控制P1
  18. 知识表示学习研究进展
  19. js判断时间是否在某一时间段内
  20. NAACL2021阅读理解论文整理

热门文章

  1. 计算机在新能源发电上的应用,新能源的发展与应用
  2. 月薪8000的出租车司机给我上了震撼一课...
  3. The server time zone value ‘?й???’ is unrecognized or represents more than one time zone
  4. 华为C8812E打开log日志功能
  5. ftp上传工具中文版,6款最好用的ftp上传工具中文版,使用指南
  6. 电子元件符号及字母表示
  7. C/C++语言的服务器LS调研 (Language Server 实现代码索引 跳转定义 智能提示等功能)
  8. 如何在国际学术会议上作一场精彩的口头报告
  9. 织梦(DedeCMS)通用免登陆发布插件
  10. ADG - Active