原文链接:https://www.cnblogs.com/NickQ/p/9247638.html

MQTT入门1 -- mosquitto 安装

简介:

MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,有可能成为物联网的重要组成部分。

MQTT服务器有好多种,mosquitto 是其中之一。由于mosquitto相对来说,教程比较多,对于初学者更容易入手。因此,我选择了mosquitto。

Mosquitto是一个实现了MQTT3.1协议的代理服务器,由MQTT协议创始人之一的Andy Stanford-Clark开发,它为我们提供了非常棒的轻量级数据交换的解决方案。本文的主旨在于记录Mosquitto服务的安装和使用,以备日后查阅。

安装依赖包

  1. yum install gcc gcc-c++ libstdc++-devel
  2. yum install openssl-devel -y
  3. yum install c-ares-devel -y
  4. yum install uuid-devel -y
  5. yum install libuuid-devel -y

下载源代码:https://mosquitto.org/files/source/

解压,编译,安装

  1. [nick@XQLY mqtt]$ tar -zxvf ./mosquitto-1.4.15.tar.gz
  2. [nick@XQLY mqtt]$ sudo make && make install

创建配置文件

  1. [nick@XQLY mqtt]$ cd /etc/mosquitto/
  2. [nick@XQLY mosquitto]$ ls
  3. aclfile.example mosquitto.conf.example pskfile.example pwfile.example
  4. [nick@XQLY mosquitto]$ cp ./mosquitto.conf.example ./mosquitto.conf

修改配置文件mosquitto.conf 部分内容

  1. # When run as root, drop privileges to this user and its primary
  2. # group.
  3. # Leave blank to stay as root, but this is not recommended.
  4. # If run as a non-root user, this setting has no effect.
  5. # Note that on Windows this has no effect and so mosquitto should
  6. # be started by the user you wish it to run as.
  7. user mosquitto
  8. # Boolean value that determines whether clients that connect
  9. # without providing a username are allowed to connect. If set to
  10. # false then a password file should be created (see the
  11. # password_file option) to control authenticated client access.
  12. # Defaults to true.
  13. allow_anonymous false
  14. # See the TLS client require_certificate and use_identity_as_username options
  15. # for alternative authentication options.
  16. password_file /etc/mosquitto/password_file

配置文件说明

# =================================================================

# General configuration

# =================================================================

  1. # 客户端心跳的间隔时间
  2. #retry_interval 20
  3. # 系统状态的刷新时间
  4. #sys_interval 10
  5. # 系统资源的回收时间,0表示尽快处理
  6. #store_clean_interval 10
  7. # 服务进程的PID
  8. #pid_file /var/run/mosquitto.pid
  9. # 服务进程的系统用户
  10. #user mosquitto
  11. # 客户端心跳消息的最大并发数
  12. #max_inflight_messages 10
  13. # 客户端心跳消息缓存队列
  14. #max_queued_messages 100
  15. # 用于设置客户端长连接的过期时间,默认永不过期
  16. #persistent_client_expiration
  17. # =================================================================
  18. # Default listener
  19. # =================================================================
  20. # 服务绑定的IP地址
  21. #bind_address
  22. # 服务绑定的端口号
  23. #port 1883
  24. # 允许的最大连接数,-1表示没有限制
  25. #max_connections -1
  26. # cafile:CA证书文件
  27. # capath:CA证书目录
  28. # certfile:PEM证书文件
  29. # keyfile:PEM密钥文件
  30. #cafile
  31. #capath
  32. #certfile
  33. #keyfile
  34. # 必须提供证书以保证数据安全性
  35. #require_certificate false
  36. # 若require_certificate值为true,use_identity_as_username也必须为true
  37. #use_identity_as_username false
  38. # 启用PSK(Pre-shared-key)支持
  39. #psk_hint
  40. # SSL/TSL加密算法,可以使用“openssl ciphers”命令获取
  41. # as the output of that command.
  42. #ciphers
  43. # =================================================================
  44. # Persistence
  45. # =================================================================
  46. # 消息自动保存的间隔时间
  47. #autosave_interval 1800
  48. # 消息自动保存功能的开关
  49. #autosave_on_changes false
  50. # 持久化功能的开关
  51. persistence true
  52. # 持久化DB文件
  53. #persistence_file mosquitto.db
  54. # 持久化DB文件目录
  55. #persistence_location /var/lib/mosquitto/
  56. # =================================================================
  57. # Logging
  58. # =================================================================
  59. # 4种日志模式:stdout、stderr、syslog、topic
  60. # none 则表示不记日志,此配置可以提升些许性能
  61. log_dest none
  62. # 选择日志的级别(可设置多项)
  63. #log_type error
  64. #log_type warning
  65. #log_type notice
  66. #log_type information
  67. # 是否记录客户端连接信息
  68. #connection_messages true
  69. # 是否记录日志时间
  70. #log_timestamp true
  71. # =================================================================
  72. # Security
  73. # =================================================================
  74. # 客户端ID的前缀限制,可用于保证安全性
  75. #clientid_prefixes
  76. # 允许匿名用户
  77. #allow_anonymous true
  78. # 用户/密码文件,默认格式:username:password
  79. #password_file
  80. # PSK格式密码文件,默认格式:identity:key
  81. #psk_file
  82. # pattern write sensor/%u/data
  83. # ACL权限配置,常用语法如下:
  84. # 用户限制:user <username>
  85. # 话题限制:topic [read|write] <topic>
  86. # 正则限制:pattern write sensor/%u/data
  87. #acl_file
  88. # =================================================================
  89. # Bridges
  90. # =================================================================
  91. # 允许服务之间使用“桥接”模式(可用于分布式部署)
  92. #connection <name>
  93. #address <host>[:<port>]
  94. #topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]
  95. # 设置桥接的客户端ID
  96. #clientid
  97. # 桥接断开时,是否清除远程服务器中的消息
  98. #cleansession false
  99. # 是否发布桥接的状态信息
  100. #notifications true
  101. # 设置桥接模式下,消息将会发布到的话题地址
  102. # $SYS/broker/connection/<clientid>/state
  103. #notification_topic
  104. # 设置桥接的keepalive数值
  105. #keepalive_interval 60
  106. # 桥接模式,目前有三种:automatic、lazy、once
  107. #start_type automatic
  108. # 桥接模式automatic的超时时间
  109. #restart_timeout 30
  110. # 桥接模式lazy的超时时间
  111. #idle_timeout 60
  112. # 桥接客户端的用户名
  113. #username
  114. # 桥接客户端的密码
  115. #password
  116. # bridge_cafile:桥接客户端的CA证书文件
  117. # bridge_capath:桥接客户端的CA证书目录
  118. # bridge_certfile:桥接客户端的PEM证书文件
  119. # bridge_keyfile:桥接客户端的PEM密钥文件
  120. #bridge_cafile
  121. #bridge_capath
  122. #bridge_certfile
  123. #bridge_keyfile

创建用户/密码

  1. mosquitto.conf中指明了,用户密码文件路径,在路径下创建密码文件;
  2. 格式: username:password

使用配置文件启动服务

  1. [nick@XQLY mosquitto]$ mosquitto -c /etc/mosquitto/mosquitto.conf

查看服务启动状态

  1. [nick@XQLY ~]$ ps -A | grep mosquitto

查看服务启动状态

  1. [nick@XQLY ~]$ ss -tanl

补充问题:

1、在启动过程中报错:

Error: Invalid user 'mosquitto'.

解决方法:

2、重启Mosquitto

使用Liunx终止进程命令,强行退出。 先kill掉,再重启:

3、编译过程中问题:

  1. 1. ssh.h找不到。我之前安装了openssl,为什么还会报这个错误呢,因为我安装好了以后并没有配置环境变量 下面的命令是安装开发环境,执行此命令,上面的openssl就可以不安装了
  2. yum install openssl-devel
  3. 2.ares.h找不到
  4. yum install c-ares-devel
  5. 3.#include <uuid/uuid.h> 找不到文件解决方法:
  6. yum install e2fsprogs-devel
  7. yum install uuid-devel
  8. yum install libuuid-devel

4、 调测问题:

  1. 1、报错./mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
  2. 解决:执行以下命令:
  3. 建立软链接
  4. sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
  5. 刷新链接
  6. ldconfig

调试

开启服务

  1. [nick@XQLY mosquitto]$ mosquitto -c /etc/mosquitto/mosquitto.conf
  2. 1530344877: mosquitto version 1.4.15 (build date 2018-06-30 14:03:02+0800) starting
  3. 1530344877: Config loaded from /etc/mosquitto/mosquitto.conf.
  4. 1530344877: Opening ipv4 listen socket on port 1883.
  5. 1530344877: Opening ipv6 listen socket on port 1883.

建立一个客户端,订阅主题(用户名nick 密码zhangshan)

  1. [nick@XQLY mosquitto]$ mosquitto_sub -u nick -P zhangshan -t mass

建立一个客户端,向mass主题发送一个消息

  1. [nick@XQLY ~]$ mosquitto_pub -t mass -u nick -P zhangshan -m "this is a messeage"

服务器显示

  1. [nick@XQLY mosquitto]$ mosquitto -c /etc/mosquitto/mosquitto.conf
  2. 1530344877: mosquitto version 1.4.15 (build date 2018-06-30 14:03:02+0800) starting
  3. 1530344877: Config loaded from /etc/mosquitto/mosquitto.conf.
  4. 1530344877: Opening ipv4 listen socket on port 1883.
  5. 1530344877: Opening ipv6 listen socket on port 1883.
  6. 1530344892: New connection from ::1 on port 1883.
  7. 1530344892: New client connected from ::1 as mosqsub|9252-XQLY (c1, k60, u'nick').
  8. 1530344900: New connection from ::1 on port 1883.
  9. 1530344900: New client connected from ::1 as mosqpub|9254-XQLY (c1, k60, u'nick').
  10. 1530344900: Client mosqpub|9254-XQLY disconnected.

订阅者显示

  1. [nick@XQLY mosquitto]$ mosquitto_sub -u nick -P zhangshan -t mass
  2. this is a messeage

本文参考:

https://blog.csdn.net/qq_29350001/article/details/76680646

https://www.cnblogs.com/chen1-kerr/p/7258487.html

MQTT入门1 -- mosquitto 安装的更多相关文章

  1. 【转载】MQTT学习笔记——MQTT协议体验 Mosquitto安装和使用

    http://blog.csdn.net/xukai871105/article/details/39252653 0 前言     MQTT是IBM开发的一个即时通讯协议.MQTT是面向M2M和物联 ...

  2. MQTT的学习之Mosquitto安装&使用(1)

    Mosquitto是一个实现了MQTT3.1协议的代理服务器,由MQTT协议创始人之一的Andy Stanford-Clark开发,它为我们提供了非常棒的轻量级数据交换的解决方案.本文的主旨在于记录M ...

  3. MQTT的学习之Mosquitto安装和使用

    Mosquitto是一个实现了MQTT3.1协议的代理服务器,由MQTT协议创始人之一的Andy Stanford-Clark开发,它为我们提供了非常棒的轻量级数据交换的解决方案.本文的主旨在于记录M ...

  4. 在阿里云服务器上(centos 8) 安装自己的MQTT服务器 (mosquitto)

    layout: post title: 在阿里云服务器上(centos 8) 安装自己的MQTT服务器 (mosquitto) subtitle: date: 2020-3-2 author: Dap ...

  5. Mac 下 Mosquitto 安装和配置 (Mosquitto为开源的mqtt服务器)

    官网:http://mosquitto.org/download/ 官网的介绍简单明了 Mac 下一个命令“brew install mosquitto” 安装成功了,还学会了brew 安装目录:/u ...

  6. Mosquitto安装_Ubuntu/Debian上安装消息队列Mosquitto

    Mosquitto安装_Ubuntu/Debian上安装消息队列Mosquitto MQTT是IBM开发的一个即时通讯协议.MQTT是面向M2M和物联网的连接协议,采用轻量级发布和订阅消息传输机制.M ...

  7. MQTT 2——服务端安装与客户端测试

    本篇记录一下MQTT服务器,客户端,JAVA客户端的选择开发与测试 MQTT服务端选择与安装过程:MQTT客户端测试工具安装与测试:MQTT JAVA客户端的选择与开发,测试 MQTT服务器选择与安装 ...

  8. jmeter安装教程与新手入门(附jdk安装教程)

    一.前言 最近要对网站做性能测试,提到了并发数测试,查了下,还是决定使用jmeter来完成这项测试,这里总结了jmeter完整的安装教程,附上新手使用教程. 二.jmeter安装 1.jdk安装(jm ...

  9. [转] Spark快速入门指南 – Spark安装与基础使用

    [From] https://blog.csdn.net/w405722907/article/details/77943331 Spark快速入门指南 – Spark安装与基础使用 2017年09月 ...

随机推荐

  1. [翻译] HTKDynamicResizingCell

    HTKDynamicResizingCell https://github.com/henrytkirk/HTKDynamicResizingCell Subclassed UITableView/U ...

  2. 用POP动画模拟真实秒钟摆动效果

    用POP动画模拟真实秒钟摆动效果 静态图: 动画图: 此处用到了POP中的Spring系列动画,现提供源码如下: SecondClockView.h 与 SecondClockView.m // // ...

  3. Java实例---简单的上课管理系统

    源码分析 Course.java package com.ftl.many2many; import java.util.*; public class Course { private int cr ...

  4. December 09th 2016 Week 50th Friday

    In books lies the soul of the whole past time. 书中有所有先贤的全部灵魂. I must know that if I run my business i ...

  5. codeforces 388D Fox and Perfect Sets(线性基+数位dp)

    #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define mp mak ...

  6. 一次xss的黑盒挖掘和利用过程

    挖掘过程一: 自从上一次投稿,已经好久好久没写文章了.今天就着吃饭的时间,写篇文章,记录下自己学习xss这么久的心得.在我看来.Xss就是javascript注入,你可以在js语法规定的范畴内做任何事 ...

  7. Alpha Scrum5

    Alpha Scrum5 牛肉面不要牛肉不要面 Alpha项目冲刺(团队作业5) 各个成员在 Alpha 阶段认领的任务 林志松:督促和监督团队进度,前端页面编写 林书浩.陈远军:界面设计.美化 吴沂 ...

  8. 【RabbitMQ】4、三种Exchange模式——订阅、路由、通配符模式

    前两篇博客介绍了两种队列模式,这篇博客介绍订阅.路由和通配符模式,之所以放在一起介绍,是因为这三种模式都是用了Exchange交换机,消息没有直接发送到队列,而是发送到了交换机,经过队列绑定交换机到达 ...

  9. 如何高效的写出markdown笔记

    重置用户名和密码 安利一个小工具donet-cnblog可以同步图片到cnblog中,同时生成对应的Markdown笔记.写博客的时候我们可以本地写,用这个工具同步到cnblog上能够大大节省我们的时 ...

  10. 【jQuery mobile】启程跨平台开发之旅

    APICloud创建跨平台应用有两种方法,一种在云端直接创建,一种是在APICloud Studio中创建. 创建一个应用 1.注册账号 2.创建HelloApp应用 3.留意应用的ID . 4.下载 ...