天天看点

Mqtt服务器配置

作者:纳秒智控

概述

Mosquitto是一个轻量级的开源消息代理,实现了MQTT版本3.1.0,3.1.1和版本5.0。它由Roger Light用C语言编写,可在Windows和Linux上运行,是一个Eclipse项目。官网的地址下载链接(http://mosquitto.org/download/),选择需要的版本下载安装即可。

Mqtt服务器配置

配置和启动服务器

  • 安装过程比较简单,傻瓜式的下一步即可,安装完成以后,找到安装目录,如下图所示:
Mqtt服务器配置
  • 打开Windows的超级终端或者命令提示框,进入到安装目录,输入下面命令,即可启动服务器
.\mosquitto.exe -c .\mosquitto.conf -v           

出现下面的界面,说明启动成功了,通讯和运行日志都能监视到,如果不想看日志,可以去掉-v

Mqtt服务器配置

注意:上述默认配置,只能本机客户端访问,无法远程访问

默认的配置文件为安装目录下的:mosquitto.conf,通过修改这个配置文件就能实现远程访问

  • 修改监听端口号,将1883修改为2005
# listener port-number [ip address/host name/unix socket path]
listener 2005           
  • 配置访问的用户名密码,关闭匿名访问
# Defaults to false, unless there are no listeners defined in the configuration
# file, in which case it is set to true, but connections are only allowed from
# the local machine.
allow_anonymous false
           
  • 设置用户名密码存储的文件,这里采用默认的文件pwfile.example
# See the TLS client require_certificate and use_identity_as_username options
# for alternative authentication options. If an auth_plugin is used as well as
# password_file, the auth_plugin check will be made first.
password_file pwfile.example           
  • 设置用户名密码,打开Windows超级终端power shell,进入到安装目录。通过mosquitto_passwd.exe来配置用户名密码,分别指定存储文件以及用户名,回车以后设置两次密码。打开安装目录下的pwfile.example可以看到刚才添加的用户名
.\mosquitto_passwd.exe -c .\pwfile.example zhangsan           
  • 启动服务器,就可以外网访问服务器了
.\mosquitto.exe -c .\mosquitto.conf -v           

继续阅读