记得关注一下博主,博主每天都会更新IT技术,让你有意想不到的小收获哦^_^


文章目录

  • *`记得关注一下博主,博主每天都会更新IT技术,让你有意想不到的小收获哦^_^`*
      • 一、MongDB集群启停止
          • 1、MonogoDB分片集群服务启动顺序(三台主机都要操作,主机启动顺序 mongodb01 --- mongodb02 --- mongodb03)
          • 2、MonogoDB分片集群服务停止顺序(三台主机都要操作,主机停止顺序 mongodb03 --- mongodb02 --- mongodb01)
      • 二、查看并调整MonogDB分片集群
          • 1、查看分片集群状态信息
          • 2、查看分片数据库信息
          • 3、开启balancer功能
          • 4、设置balance进程activeWindows运行窗口时间(本教程暂时不设置,以免影响后面模拟操作)
      • 三、MongoDB分片集群模拟非分片集合转分片集合
          • 1、分片集群创建非分片集合并插入数据
          • 2、查看school数据库分片情况
          • 3、school数据库开启分片功能
          • 4、school数据库student集合开启分片功能
          • 5、调整MongoDB分片集群 Chunk大小
          • 6、开启student集合balancer功能和设置autosplit属性为true
          • 7、设置student集合balancer功能autosplit属性为true
          • 8、查看school数据库student集合Chunk数据块在每个shard节点分配使用情况
          • 9、模拟school数据库student集合插入数据
          • 10、查看school数据库student集合Chunk数据块在每个shard节点分配使用情况




MongoDB分片集群由以下组件组成

shard 分片服务: 提供以复制集为单位横向扩展,最大支持分片节点1024个,分片之间的数据不重复,所有分片在一起才可以完整工作。

mongos 路由服务:提供客户端应用程序和分片群集之间接口,选择合适数据shard节点进行读写,不持久化数据,只缓存config配置服务器的元数据。

config 配置服务: 提供MongoDB分片集群数据存储分片shard节点的信息和分片功能的配置信息, 从MongoDB 3.4开始,配置服务器必须部署为副本集。




主机规划

主机名称主机IPCPU内存mongos端口config端口share1端口share2端口share3端口操作系统及软件版本
mongodb01192.168.91.612*416GB2701727019271012710227103CentOS 7.9 mongo 4.4.29 mongos 4.4.29 mongod 4.4.29 mongosh 2.2.29
mongodb02192.168.91.622*416GB2701727019271012710227103CentOS 7.9 mongo 4.4.29 mongos 4.4.29 mongod 4.4.29 mongosh 2.2.29
mongodb03192.168.91.632*416GB2701727019271012710227103CentOS 7.9 mongo 4.4.29 mongos 4.4.29 mongod 4.4.29 mongosh 2.2.29



MongoDB分片集群架构图

在这里插入图片描述




提示:MongoDB 分片集群安装与部署本教程不讲解,如有疑问请自行上网查阅关于MongDB分片集群安装与部署。




一、MongDB集群启停止


1、MonogoDB分片集群服务启动顺序(三台主机都要操作,主机启动顺序 mongodb01 — mongodb02 — mongodb03)
systemctl start mongod
systemctl start shard1
systemctl start shard2
systemctl start shard3
systemctl start mongossystemctl status mongod
systemctl status shard1
systemctl status shard2
systemctl start shard3
systemctl status mongos

2、MonogoDB分片集群服务停止顺序(三台主机都要操作,主机停止顺序 mongodb03 — mongodb02 — mongodb01)
systemctl stop mongos
systemctl stop shard3
systemctl stop shard2
systemctl stop shard1
systemctl stop mongodsystemctl status mongos
systemctl status shard3
systemctl status shard2
systemctl status shard1
systemctl status mongod



二、查看并调整MonogDB分片集群


1、查看分片集群状态信息
mongosh --host 192.168.91.61 --port 27017 -u root -p 123456### 显示分片集群的分片节点信息
use admin
db.runCommand({ listshards : 1})### 查看分片集群状态信息
use admin
sh.status()
### 查看更多分片集群状态信息,包括分片键和范围信息等
sh.status(true)

2、查看分片数据库信息
mongosh --host 192.168.91.61 --port 27017 -u root -p 123456### 查看已开启分片的数据库
use config
db.databases.find( { "partitioned": true } )
db.databases.find()### 查看所有分片的片键
db.collections.find()

3、开启balancer功能

提示:balancer组件是MongoDB分片集群中的一个进程,它会定期检查分片集群中的分片数量,并自动迁移分片之间数据不均衡的数据块,以此来实现数据分布均衡。

mongosh --host 192.168.91.61 --port 27017 -u root -p 123456### 检查balancer是否开启,true为开启,false为关闭,默认开启
sh.getBalancerState()### 启动balancer
sh.startBalancer()### 关闭balancer
sh.stopBalancer()

4、设置balance进程activeWindows运行窗口时间(本教程暂时不设置,以免影响后面模拟操作)

提示:如果MongDB分片集群在执行备份期间,需要确保balance处于停止状态,以免MongDB分片集群在执行备份期间还有数据块在迁移,影响备份数据的一致性。可以设置一个备份窗口时间用于自动化备份操作,在这个时间段内,balancer进程一直处于停止状态。

mongosh --host 192.168.91.61 --port 27017 -u root -p 123456### 检查balancer是否开启,true为开启,false为关闭,默认开启
use config
sh.getBalancerState()### 如果未开balancer功能可以执行以下命令开启
sh.setBalancerState( true )### 设置balancer进程activeWindows运行窗口从6点开始运行,晚上23点停止,其余的时间段可以用来备份分片集群数据库操作
db.settings.update( { _id : "balancer" }, { $set : { activeWindow : { start : "6:00", stop : "23:00" } } }, true )### 删除balancer进程activeWindows运行窗口
db.settings.update({ "_id" : "balancer" }, { $unset : { activeWindow : 1 }});



三、MongoDB分片集群模拟非分片集合转分片集合

模拟说明:暂时先不要给模拟数据库和集合启动分片功能,等创建好数据库并插入数据后,再启动分片功能。


1、分片集群创建非分片集合并插入数据
mongosh --host 192.168.91.61  --port 27017  -u root -p 123456### 创建school数据库
use school### 创建student集合并插入数据
for (var i = 1; i <= 10000; i++){db.student.insert({number:i,"001":"xiaoming"});
}### 查看school数据库student集合数据
use school
show collections;
db.student.find().limit(5)
db.student.find().count()

2、查看school数据库分片情况

提示:MongoDB分片集群school数据库student集合如果要开启分片功能,需要先开启school数据库分片功能才能接着开启student集合的分片功能。

mongosh --host 192.168.91.61  --port 27017  -u root -p 123456### 当前参数partitioned:为false说明school数据库并没有开启分片功能,student集合也没有开启分片功能,当前参数primary: 'shard1'说明school数据库只是分配在shard1节点上
[direct: mongos] config> sh.status()
…………………………………………database: {_id: 'school',primary: 'shard1',partitioned: false,version: {uuid: UUID('9e42a412-f191-40b5-9804-9f225cd5abb4'),lastMod: 1},lastMovedTimestamp: Timestamp({ t: 1718954817, i: 1 })},collections: {}}
]

3、school数据库开启分片功能
mongosh --host 192.168.91.61  --port 27017  -u root -p 123456### school数据库开启分片功能
use admin
sh.enableSharding("school")### 当前参数partitioned:为true说明school数据库开启分片功能
[direct: mongos] admin> sh.status()
…………………………………………database: {_id: 'school',primary: 'shard1',partitioned: true,version: {uuid: UUID('9e42a412-f191-40b5-9804-9f225cd5abb4'),lastMod: 1},lastMovedTimestamp: Timestamp({ t: 1718954817, i: 1 })},collections: {}}
]

4、school数据库student集合开启分片功能
mongosh --host 192.168.91.61  --port 27017  -u root -p 123456### school数据库student集合开启分片功能,提示以下错误原因在于当前school数据库student集合已经有数据在里面(属于非分片集合),并且插入的数据并没有分片索引,所以,需要进入到school数据库先创建student集合的分片索引。
[direct: mongos] admin> use admin
[direct: mongos] admin> sh.shardCollection( "school.student", { _id: "hashed" } )
MongoServerError[InvalidOptions]: Please create an index that starts with the proposed shard key before sharding the collection### 进入school数据库创建student集合分片索引(”_id”索引名称自定义)
[direct: mongos] school> use school
[direct: mongos] school> db.student.createIndex( { _id: "hashed" } )
[direct: mongos] school> db.student.getIndexes()
[{ v: 2, key: { _id: 1 }, name: '_id_' },{ v: 2, key: { _id: 'hashed' }, name: '_id_hashed' }
]### 开启school数据库student集合分片功能
use admin
sh.shardCollection( "school.student", { _id: "hashed" } )### 当前参数balancing值为true说明student集合开启了balancer功能。当前chunkMetadata参数只分配在shard1节点上,但没有分配在shard2节点和shard3节点,是否student没有开启分片功能?其实不是这样子,原因在于当前student集合并没有数据写入,而且数据也很小,分片集群的Chunk数据块默认是64MB,其实只要一个shard节点的一个Chunk数据块就够存储了。如果我们把Chunk数据块调整成1MB,然后我们在往student集合插入大量数据,那balancer进程就会检查到有数据写入到student集合就会触发自动平衡student集合Chunk数据块到shard1节点、shard2节点、shard3节点。
[direct: mongos] admin> sh.status()
…………………………………………database: {_id: 'school',primary: 'shard1',partitioned: true,version: {uuid: UUID('9e42a412-f191-40b5-9804-9f225cd5abb4'),lastMod: 1},lastMovedTimestamp: Timestamp({ t: 1718954817, i: 1 })},collections: {'school.student': {shardKey: { _id: 'hashed' },unique: false,balancing: true,chunkMetadata: [ { shard: 'shard1', nChunks: 1 } ],chunks: [{ min: { _id: MinKey() }, max: { _id: MaxKey() }, 'on shard': 'shard1', 'last modified': Timestamp({ t: 1, i: 0 }) }],tags: []}}}
]

5、调整MongoDB分片集群 Chunk大小

提示:本教程调整Chunk大小为1MB只是为了方便模拟测试,如果是生产环境推荐使用默认值或根据实际环境需求调整。

mongosh --host 192.168.91.61  --port 27017  -u root -p 123456### 调整chunk大小,默认64MB,chunk取值范围 1MB~1024MB(2的整数倍取值),"value:1"代表调整每个chunk大小为1MB
use config
db.settings.updateOne( { _id: "chunksize" },{ $set: { _id: "chunksize", value: 1 } },{ upsert: true } )### 查看chunk大小,并且要确保当前参数autosplit为true状态,才能让Chunk自动分裂,autosplit属性也是靠insert和update来触发Chunk自动分裂的
[direct: mongos] config> db.settings.find()
[{ _id: 'balancer', mode: 'full', stopped: false },{ _id: 'autosplit', enabled: true },{ _id: 'chunksize', value: 1 }
]

6、开启student集合balancer功能和设置autosplit属性为true
mongosh --host 192.168.91.61  --port 27017  -u root -p 123456### 开启student集合balancer功能
use config
sh.enableBalancing("school.student") ### 关闭student集合balancer功能
use config
sh.disableBalancing("school.student")### 查看school数据库student集合是否开启了balancer功能,如果参数balancing值为true说明开启,false说明关闭
sh.status()

7、设置student集合balancer功能autosplit属性为true
mongosh --host 192.168.91.61  --port 27017  -u root -p 123456### 设置autosplit属性为true
use config
sh.disableAutoSplit()### 设置autosplit属性为false
use config
sh.enableAutoSplit()### 查看student集合balancer功能autosplit属性值
use config
db.settings.find()

8、查看school数据库student集合Chunk数据块在每个shard节点分配使用情况
mongosh --host 192.168.91.61  --port 27017  -u root -p 123456[direct: mongos] config> use school[direct: mongos] school> db.student.getShardDistribution()
Shard shard1 at shard1/192.168.91.61:27101,192.168.91.62:27101,192.168.91.63:27101
{data: '507KiB',docs: 10000,chunks: 1,'estimated data per chunk': '507KiB','estimated docs per chunk': 10000
}
---
Totals
{data: '507KiB',docs: 10000,chunks: 1,'Shard shard1': ['100 % data','100 % docs in cluster','52B avg obj size on shard']
}

9、模拟school数据库student集合插入数据
mongosh --host 192.168.91.61  --port 27017  -u root -p 123456use school### student集合在原来1万条数据再插入多5万条数据
for (var i = 10001; i <= 60000; i++){db.student.insert({id:i,"003":"xiaoming"});
}### 查看school数据库student集合数据
use school
show collections;
db.student.find().limit(5)
db.student.find().count()

10、查看school数据库student集合Chunk数据块在每个shard节点分配使用情况
mongosh --host 192.168.91.61  --port 27017  -u root -p 123456[direct: mongos] school> use school[direct: mongos] school> db.student.getShardDistribution()
Shard shard1 at shard1/192.168.91.61:27101,192.168.91.62:27101,192.168.91.63:27101
{data: '2.16MiB',docs: 46552,chunks: 1,'estimated data per chunk': '2.16MiB','estimated docs per chunk': 46552
}
---
Shard shard2 at shard2/192.168.91.61:27102,192.168.91.62:27102,192.168.91.63:27102
{data: '1.38MiB',docs: 29859,chunks: 1,'estimated data per chunk': '1.38MiB','estimated docs per chunk': 29859
}
---
Shard shard3 at shard3/192.168.91.61:27103,192.168.91.62:27103,192.168.91.63:27103
{data: '709KiB',docs: 14929,chunks: 1,'estimated data per chunk': '709KiB','estimated docs per chunk': 14929
}
---
Totals
{data: '4.24MiB',docs: 91340,chunks: 3,'Shard shard1': ['51.06 % data','50.96 % docs in cluster','48B avg obj size on shard'],'Shard shard2': ['32.62 % data','32.68 % docs in cluster','48B avg obj size on shard'],'Shard shard3': ['16.31 % data','16.34 % docs in cluster','48B avg obj size on shard']
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/94201.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/94201.shtml
英文地址,请注明出处:http://en.pswp.cn/pingmian/94201.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mybatis过渡到mybatis-plus过程中需要注意的地方

将 MyBatis 升级为 MyBatis-Plus&#xff08;简称 MP&#xff09;是一个平滑过渡的过程&#xff0c;因为 MP 是 MyBatis 的增强工具&#xff08;而非替代&#xff09;&#xff0c;但仍有一些关键注意事项需要关注&#xff0c;以确保升级后功能兼容且能充分利用 MP 的特性&#…

openEuler系统中如何将docker安装在指定目录

在 openEuler 中&#xff0c;Docker 的默认数据存储目录为 /var/lib/docker&#xff08;程序文件通常安装在系统默认路径&#xff0c;一般无需修改&#xff09;。若需将 Docker 数据&#xff08;镜像、容器、卷等&#xff09;存储到指定目录&#xff0c;可通过修改 Docker 配置…

2.4 我国金融市场的监管体制

1、国务院金融发展委员会职责 2、中国人民银行职责

PHP - 实例属性访问与静态方法调用的性能差异解析

观察到了一个看似矛盾的现象&#xff1a;实例属性访问更快&#xff0c;但静态方法调用更快。这实际上是两种不同的操作&#xff0c;下面我将详细解释其中的原理和差异。1. 实例属性访问为什么快访问机制class MyClass {public $instanceProp 1; }$obj new MyClass(); $value …

音视频面试题集锦第 31 期

音视频面试题集锦第 31 期&#xff1a; 1、I 帧、P 帧和 B 帧的概念及区别&#xff1f;2、视频编码中的码率控制技术有哪些&#xff1f;3、音频采样参数有哪些&#xff1f;4、RTMP 和 HLS 协议各有什么特点&#xff1f;如何选择&#xff1f;5、WebRTC 中的 ICE、STUN、TURN 各…

企业视频库管理高效策略

内容概要本文全面探讨企业视频库管理的高效策略&#xff0c;旨在帮助组织优化视频资源处理。首先&#xff0c;我们将介绍企业视频库管理的基本概念和核心价值。接着&#xff0c;深入分析智能分类核心技术如何通过AI算法实现视频自动归类。之后&#xff0c;阐述云集成实现路径&a…

WebSocket和跨域问题

WebSocket 特点 WebSocket 是一种在单个 TCP 连接上进行全双工通信的协议。简单来说&#xff0c;它就像是在客户端和服务器之间建立了一条"专用通道"&#xff0c;双方可以随时主动发送消息给对方&#xff0c;而不需要像HTTP那样总是由客户端发起请求。 同一时间、双向…

微服务-19.什么是网关

一.网关曾经我们的项目是单体项目&#xff0c;前端只需要请求8080端口&#xff0c;就可以获取所有需要的数据和服务并进行渲染。但是拆分成微服务后&#xff0c;会面临几大问题&#xff1a;1.但是现在我们将该单体项目拆分成了微服务项目&#xff0c;每个项目都有自己独立的端口…

从字节码层面剖析以太坊智能合约创建原理

1. 引言 阅读完本文之后&#xff0c;将能理解一下字节码含义&#xff1a; 608060405260405160893803806089833981016040819052601e916025565b600055603d565b600060208284031215603657600080fd5b5051919050565b603f80604a6000396000f3fe6080604052600080fdfea2646970667358221…

typora无需激活版及最新激活版方法!双击安装就能用

介绍 Typora 是一款Markdown编辑器&#xff0c;支持实时预览&#xff0c;所见即所得。跨平台&#xff0c;支持Windows、macOS、Linux。适合写作、笔记、技术文档等。本教程将提供合法安全的安装方案&#xff0c;并解决常见问题&#xff0c;助你高效完成部署&#xff01; 直接…

基于Java、GeoTools与PostGIS的对跖点求解研究

目录 前言 一、对跖点简介 1、地理学定义 2、人生哲学含义 二、对跖点求解 1、Java求解 2、Geotools求解 3、PostGIS求解 4、三种计算方法的对比 5、Leaflet展示对跖点 三、总结 前言 在地理信息系统&#xff08;GIS&#xff09;领域&#xff0c;对跖点&#xff08;A…

Linux-函数的使用-编写监控脚本

Linux-函数的使用-编写监控脚本前言一、监控cpu二、采集内存的使用信息三、采集磁盘和分区的使用信息四、显示进程的信息前言 编写监控脚本实现以下功能 监控cpu&#xff0c;内存&#xff0c;磁盘&#xff0c;进程等信息&#xff0c;每隔5分钟记录这些信息到日志文件里perform…

Authelia:开源双因素认证与单点登录解决方案

项目标题与描述 Authelia是一个开源的认证和授权服务器&#xff0c;专注于为应用程序提供双因素认证&#xff08;2FA&#xff09;和单点登录&#xff08;SSO&#xff09;功能。通过Web门户&#xff0c;Authelia能够作为身份和访问管理&#xff08;IAM&#xff09;系统&#xff…

Apache Ozone 介绍与部署使用(最新版2.0.0)

目录 一、软件介绍 二、软件架构 Ozone Manager&#xff08;OM&#xff09; Storage Container Manager&#xff08;SCM&#xff09; Containers Datanodes Storage Containers Recon Recon 和 Ozone Manager Recon 和 Storage Container Manager 三、安装部署 准备…

Review --- Linux

Review — Linux Linux 是一种开源的类 Unix 操作系统内核&#xff0c;广泛应用于服务器、嵌入式设备和个人计算机中。其核心特点是开源、稳定、安全和高度的可定制性。对于大学毕业生而言&#xff0c;掌握 Linux 的基本操作和原理是进入 IT 行业的重要技能之一。 Linux 的基本…

【msyql 】占用硬盘太大 ,那些文件可以清理

从目录内容来看&#xff0c;这台 MySQL 服务器上主要是 xxl-job 调度平台的数据库。占用空间最大的是&#xff1a;24G xxl_job_log.ibd这个文件是 xxl-job 的任务执行日志表&#xff0c;随着时间推移&#xff0c;日志量会非常大。可以清理的文件和方法1. 清理 xxl_job_log 表数…

58 C++ 现代C++编程艺术7-模板友元

C 现代C编程艺术7-模板友元 文章目录C 现代C编程艺术7-模板友元一、基础应用场景 &#x1f9e9;1. 模板类声明友元函数2. 普通类声明模板函数为友元二、模板类互访场景 ⚙️1. 同类模板互访&#xff08;一对一&#xff09;2. 异类模板互访&#xff08;多对多&#xff09;三、高…

Undertow —— JBOSS 的社区版,redhat 下场维护的开源项目,顶顶好用的 Java web server

Undertow JBoss Community Undertow Undertow is a flexible performant web server written in java, providing both blocking and non-blocking API’s based on NIO. Undertow 是一个用 Java 编写的灵活高性能 Web 服务器&#xff0c;提供基于 NIO 的阻塞和非阻塞 API。…

【AI智能体】Dify 搭建业务单据差异核对助手实战详解

目录 一、前言 二、Dify介绍 2.1 Dify 是什么 2.2 Dify 核心特性 2.2.1 Dify特点 2.2.2 Dify 多模型支持 2.2.3 Dify 适应场景 2.2.4 基于Dify 搭建发票识别应用优势 三、Dify 搭建业务单据核对助手实战过程 3.1 前置准备 3.1.1 安装必要的插件 3.2 完整操作步骤 3…

Centos编译安装Python3.10

gcc编译源码包 下载python源码包并解压 wget https://www.python.org/ftp/python/3.10.18/Python-3.10.18.tgz tar -xf Python-3.10.18.tgz cd Python-3.10.18系统编译依赖环境安装 sudo yum install zlib-devel ncurses-devel gdbm-devel nss-devel openssl-devel readline-de…