当前位置:   article > 正文

linux基于mosquitto搭建MQTT服务器集群_mosquitto 集群

mosquitto 集群

一、安装相关软件
1、安装 gcc-c++

	yum install gcc-c++
  • 1
2、安装cmake
  • 1
	yum install cmake
  • 1
3、安装openssl-devel
  • 1
	yum install openssl-devel
  • 1

二、安装mosquitto
1、下载并解压mosquitto

	wget http://mosquitto.org/files/source/mosquitto-1.4.10.tar.gz --no-check-certificate
	tar -xzvf mosquitto-1.4.10.tar.gz
  • 1
  • 2
2、修改mosquitto的配置
	打开mosquitto-1.4.10文件夹下的config.mk文件将里面的WITH_SRV:=yes和WITH_UUID:=yes都用#号注释掉
3、安装
  • 1
  • 2
  • 3
  make
  sudo make install
  • 1
  • 2

三、配置mosquitto
1、创建用户

sudo groupadd mosquitto
sudo useradd -g mosquitto mosquitto
  • 1
  • 2

2、创建配置文件

mv  /etc/mosquitto/mosquitto.conf.example  /etc/mosquitto/mosquitto.conf
  • 1

3、关闭匿名用户登录
打开mosquitto.conf文件( /etc/mosquitto/mosquitto.conf ),找到allow_anonymous节点,这个节点作用是,是否开启匿名用户登录。去掉前面的#,改为false。
4、设置用户密码文件路径
找到#password_file节点,这个节点是告诉服务器你要配置的用户将存放在哪里。打开此配置并指定pwfile.example文件路径(注意是绝对路径),在669行左右。
password_file /etc/mosquitto/pwfile 或者 /pwfile.example
5、配置topic和用户
找到 #acl_file节点进行配置 acl_file /etc/mosquitto/aclfile.example
6、添加用户
终端输入,最后面的是用户名,之后自动弹出密码和确认密码,输入即可。(注意第二次创建用户时不用加 -c 如果加 -c 会把第一次创建的用户覆盖。)

mosquitto_passwd -c /etc/mosquitto/pwfile admin203
  • 1

然后进入到/etc/mosquitto/mosquitto.conf 的# Authentication and topic access plugin options 下将下面两个用户名和密码加上。

# -----------------------------------------------------------------
# Authentication and topic access plugin options
# -----------------------------------------------------------------

# If the auth_plugin option above is used, define options to pass to the
# plugin here as described by the plugin instructions. All options named
# using the format auth_opt_* will be passed to the plugin, for example:
#
# auth_opt_db_host
# auth_opt_db_port 
 auth_opt_db_username admin203
 auth_opt_db_password admin203
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

7、添加Topic和用户的关系
编辑/etc/mosquitto/aclfile.example文件在末尾添加以下配置
user admin203
topic write saleAuction/#

user admin203
topic read saleAuction/#
四、启动服务器

mosquitto -c /etc/mosquitto/mosquitto.conf -d
  • 1

五、接收消息

mosquitto_sub -h localhost -t saleAuction/448180 -u admin203 -P admin203
  • 1

如果报以下错误
mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
依次在mosquitto安装文件夹所有目录中依次执行以下命令

sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
sudo ldconfig
  • 1
  • 2

如果报以下错误
Connection Refused: not authorised.
可进行以下操作

mosquitto_passwd /etc/mosquitto/pwfile.example admin203 #重置用户admin203的密码
  • 1

并确保配置文件mosquitto.conf中password_file /etc/mosquitto/pwfile.example与重置名字时的密码文件路径名称一致。
六、发送信息

mosquitto_pub -h localhost -t saleAuction/448180 -u admin203 -P admin203 -m "testjkhjkh"
  • 1

七、客户端收到以下消息

[root@yuanq1 ~]# mosquitto_sub -h localhost -t saleAuction/448180 -u admin203 -P admin203
testjkhjkh
  • 1
  • 2

八、集群搭建
在安装多台mqtt服务器,从服务器配置不用动,只修改主服务器的Bridges节点

# =================================================================
# Bridges
# =================================================================

# A bridge is a way of connecting multiple MQTT brokers together.
# Create a new bridge using the "connection" option as described below. Set
# options for the bridges using the remaining parameters. You must specify the
# address and at least one topic to subscribe to.
# Each connection must have a unique name.
# The address line may have multiple host address and ports specified. See
# below in the round_robin description for more details on bridge behaviour if
# multiple addresses are used.
# The direction that the topic will be shared can be chosen by 
# specifying out, in or both, where the default value is out. 
# The QoS level of the bridged communication can be specified with the next
# topic option. The default QoS level is 0, to change the QoS the topic
# direction must also be given.
# The local and remote prefix options allow a topic to be remapped when it is
# bridged to/from the remote broker. This provides the ability to place a topic
# tree in an appropriate location. 
# For more details see the mosquitto.conf man page.
# Multiple topics can be specified per connection, but be careful 
# not to create any loops.
# If you are using bridges with cleansession set to false (the default), then
# you may get unexpected behaviour from incoming topics if you change what
# topics you are subscribing to. This is because the remote broker keeps the
# subscription for the old topic. If you have this problem, connect your bridge
# with cleansession set to true, then reconnect with cleansession set to false
# as normal.
#connection <name>
#address <host>[:<port>] [<host>[:<port>]]
#topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]

connection slave1 slave2
address 192.168.5.116:1883 192.168.5.119:1883
topic # both 0

remote_username admin203
remote_password admin203
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

connect:节上名称,自定义
adddress:ip加端口
topic:消息主题#指所有主题
remote_username 远程账号
emote_password 远程密码
如果节点服务器密码不一样,可以分开设置

connection slave1
address 192.168.5.116:1883
topic # both 0
remote_username admin203
remote_password admin203

connection slave2
address 192.168.5.119:1883
topic # both 0
remote_username admin203
remote_password admin203
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

九、如果客户端连接不上mqtt服务器可关闭防火墙

systemctl stop firewalld
  • 1

或用以下命令配置防火墙

iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 1883 -j ACCEPT
firewall-cmd --zone=public --add-port=1883/tcp --permanent
firewall-cmd --reload 
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/42877
推荐阅读
  • 文章浏览阅读2.2k次,点赞98次,收藏98次。expect是由DonLibes基于Tcl(ToolCommandLanguage)语言开发的,是一种脚本语言,主要应用于自动化交互式操作的场景,借助Expect处理交互的命令,可以将交互过程... [详细]

  • 文章浏览阅读1w次,点赞141次,收藏140次。探索LinuxDocker管理神器Portainer,解锁远程访问技巧,轻松图形化管理你的容器。LinuxDocker图形化工具Portainer远程访问文章目录前言1.部署Portainer... [详细]

  • 文章浏览阅读4.6k次,点赞126次,收藏147次。操作系统进程状态,以及Linux中的进程状态,僵尸进程,孤儿进程【Linux】探索Linux进程状态|僵尸进程|孤儿进程最近,我发现了一个超级强大的人工智能学习网站。它以通俗易懂的方式呈现... [详细]

  • 文章浏览阅读5.1k次,点赞151次,收藏135次。LinuxAMH服务器管理面板远程访问LinuxAMH服务器管理面板远程访问文章目录1.前言2.Linux安装AMH面板3.本地访问AMH面板4.Linux安装Cpolar5.配置AMH面... [详细]

  • 文章浏览阅读1.1w次,点赞32次,收藏103次。Linux安装MySQL【超详细版】_linux安装mysqllinux安装mysql一、安装MySQL的准备工作1.查看系统版本cat/etc/redhat-release12.查看系统是... [详细]

  • 文章浏览阅读723次。拿到dotnet-sdk-8.0.100-linux-x64.tar.gz文件。1.有些环境比较老的服务器,会出现如下的错误提示,需要更新c++库。执行命令检测是否安装成功,执行dotnet--version。2.把文... [详细]

  • 文章浏览阅读1.6k次,点赞77次,收藏70次。ssh操作需要免密附上本机免密脚本ssh_keygen.sh。执行完脚本成功登录。【Linux】Centos7shell实现MySQL5.7tar一键安装... [详细]

  • 文章浏览阅读987次,点赞43次,收藏32次。使用上面的Cpolarhttps公网地址,在任意设备的浏览器进行访问,即可成功看到我们AMH面板界面,这样一个公网地址且可以远程访问就创建好了,使用了cpolar的公网域名,无需自己购买云服务器... [详细]

  • 文章浏览阅读950次,点赞49次,收藏46次。本篇博客作为Linux常见指令的总结,主要是方便个人进行回顾使用。以上就是我对于Linux常见指令的总结。!!Linux:常见指令个人主页:个人主页个人专栏:《数据结构》《C语言》《C++》文章... [详细]

  • 文章浏览阅读2.8w次,点赞73次,收藏317次。ps命令用来查看系统进程,ps-ef:查看所有进程ps-aux:查看所有进程ps-ef|greptomcat:查看指定进程_psefgrep命令看进程psefgrep命令看进程「作者主页」:... [详细]

  • 文章浏览阅读1.2w次,点赞64次,收藏74次。Crontab是一个在Unix和Linux操作系统上用于定时执行任务的工具。它允许用户创建和管理计划任务,以便在特定的时间间隔或时间点自动运行命令或脚本。Crontab是crontable的缩... [详细]

  • 文章浏览阅读1.1k次,点赞76次,收藏65次。Linux|创建|删除|查看|基本命名详解Linux|创建|删除|查看|基本命名详解Linux|创建|删除|查看|基本命名详解文章目录Linux|创建|删除|查看|基本命名详解前言一、安装Li... [详细]

  • DataEase是开源的数据可视化分析工具,帮助用户快速分析数据并洞察业务趋势,从而实现业务的改进与优化。是开源的数据可视化分析工具,帮助用户快速分析数据并洞察业务趋势,从而实现业务的改进与优化。在本地搭建后,借助cpolar内网穿透实现远... [详细]

  • 介绍socket编程,简单实现一个UDP服务器✨✨【Linux】套接字编程目录套接字IP+PORTTCP和UDP的介绍TCPUDP网络字节序转换接口UDP服务器的编写服务器的初始化 socketbindsockaddr结构服务器的... [详细]

  • 本文是对Linux中文件和目录权限的总结。以上就是我对于Linux中文件和目录权限的总结。!!Linux:权限个人主页:个人主页个人专栏:《数据结构》《C语言》《C++》《Linux》文章目录前言一、Linux权限的管理文件访问者的分类(身... [详细]

  • 看了这篇文章你将学到软硬链接的使用,以及制作自己的动静态库,并使用他们✨✨_动态库的软连接动态库的软连接系列文章收录于【Linux】文件系统 专栏关于文件描述符与文件重定向的相关内容可以移步 文件描述符与重定向操作。可以... [详细]

  • 讲述Linux的基本指令:man、cp、mv、cat、more、less、head、tail等指令。【走进Linux的世界】Linux---基本指令(2)个人主页:平行线也会相交欢迎点赞... [详细]

  • 上一篇文章中我们讲到了很多的网络名词以及相关知识,下面我们就直接进入udp服务器的实现。一、udp服务器的实现cc=g++.PHONY:allclean:我们通过all就可以创建多个可执行程序了,对于cc这个变量我们设置为g++,以后如果想... [详细]

  • Linux防火墙服务和区的不同配置操作,放行端口或服务等_linux防火墙配置linux防火墙配置目录防火墙介绍zones预定义服务firewalld启动与停止查看firewalld当前状态和设置使⽤CLI查看firewalld设置fire... [详细]

  • Linux MeterSphere一站式开源持续测试平台远程访问
    MeterSphere是一站式开源持续测试平台,涵盖测试跟踪、接口测试、UI测试和性能测试等功能,全面兼容JMeter、Selenium等主流开源标准,有效助力开发和测试团队充分利用云弹性进行高度可扩展的自动化测试,加速高质量的软件交付,推... [详细]

相关标签
  

闽ICP备14008679号