Practical MongoDB Aggregations Book

概述

分片(sharding)是一种跨多台机器分布数据的方法, MongoDB使用分片来支持具有非常大的数据集和高吞吐量操作的部署。

MongoDB分片群集包含以下组件:

  • 分片(存储):每个分片包含分片数据的子集。 每个分片都可以部署为副本集
  • mongos(路由):mongos充当查询路由器,在客户端应用程序和分片集群之间提供接口
  • config servers(“调度”的配置):配置服务器存储群集的元数据和配置设置。
    从MongoDB 3.4开始,必须将配置服务器部署为副本集(CSRS)。
  • 分片键:分片键可以是单个索引字段,也可以是复合索引所涵盖的多个字段,该复合索引确定集合文档在集群分片之间的分布。
  • 对冲读取:从版本4.4开始,实例可以对冲使用非读取首选项的读取。使用对冲读取实例将读取操作路由到每个查询分片的两个副本集成员,并返回每个分片的第一个响应者的结果。

官方给出的分片集群中组件交互:

而我看来,从数据和控制的角度,下面的拓扑更为恰当:

LAB

Sharding的配置参考可以查看官方文档:https://docs.mongodb.com/manual/tutorial/deploy-shard-cluster/

拓扑图

环境准备

所属副本集 主机:Port 存储文件夹
srs01 db1:27017 /mongodb/sharded_cluster/srs01_27017
srs01 db2:27017 /mongodb/sharded_cluster/srs01_27017
srs01 db3:27017 /mongodb/sharded_cluster/srs01_27017
srs02 db1:27018 /mongodb/sharded_cluster/srs02_27018
srs02 db2:27018 /mongodb/sharded_cluster/srs02_27018
srs02 db3:27018 /mongodb/sharded_cluster/srs02_27018
confrs db1:27019 /mongodb/sharded_cluster/confrs_27019
confrs db2:27019 /mongodb/sharded_cluster/confrs_27019
confrs db3:27019 /mongodb/sharded_cluster/confrs_27019

1.创建分片副本集

副本集的具体配置参看:Mongodb-ReplicaSet Install

创建副本集srs01和srs02,如db1,同样的命令用于db2 和 db3

[zyi@db1 ~]$ sudo mongod --port 27017 --dbpath /mongodb/sharded_cluster/srs01_27017/data/db --bind_ip 0.0.0.0 --shardsvr --replSet srs01  --logpath /mongodb/sharded_cluster/srs01_27017/log/srs01.log --fork
about to fork child process, waiting until server is ready for connections.
forked process: 27400
child process started successfully, parent exiting

和正常的副本集的区别在于使用参数:–shardsvr 设置了sharding. clusterRole: shardsvr,如果用conf文件:

sharding:clusterRole: shardsvr
replication:replSetName: <replSetName>
net:bindIp: localhost,<ip address>

2.创建配置服务器副本集

创建副本集confrs,如db1,同样的命令用于db2 和 db3

[zyi@db1 ~]$ sudo mongod --port 27019 --dbpath /mongodb/sharded_cluster/confrs_27019/data/db --bind_ip 0.0.0.0 --configsvr --replSet confrs --logpath /mongodb/sharded_cluster/confrs_27019/log/confrs.log --fork
about to fork child process, waiting until server is ready for connections.
forked process: 27790
child process started successfully, parent exiting

和正常的副本集的区别在于使用参数:–configsvr 设置了sharding. clusterRole: configsvr,如果用conf文件:

sharding:clusterRole: configsvr
replication:replSetName: <replica set name>
net:bindIp: localhost,<hostname(s)|ip address(es)>

3.创建Mongos Router

在另外一台服务器上面启动两个mongos服务,分别使用27018和27019端口。
先启动一个:

[zyi@mongos ~]$ sudo /usr/local/bin/mongos --port 27018 --configdb config/db1:27019,db2:27019,db3:27019 --bind_ip 0.0.0.0 --fork --logpath /mongodb/mongos/mongos.log
about to fork child process, waiting until server is ready for connections.
forked process: 5588
child process started successfully, parent exiting

在启动的时候指定了配置副本集的位置:

--configdb config/db1:27019,db2:27019,db3:27019

也可以用conf文件的方式:

sharding:configDB: <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019
net:bindIp: localhost,<hostname(s)|ip address(es)>

再增加一台router

[zyi@centos ~]$ sudo /usr/local/bin/mongos --port 27019 --configdb config/db1:27019,db2:27019,db3:27019 --bind_ip 0.0.0.0 --fork --logpath /mongodb/mongos/mongos.log
about to fork child process, waiting until server is ready for connections.
forked process: 7490
child process started successfully, parent exiting

此时查看sharding状态:

mongos> sh.status()
--- Sharding Status --- sharding version: {"_id" : 1,"minCompatibleVersion" : 5,"currentVersion" : 6,"clusterId" : ObjectId("61f7c76011fc11df29b8cf4b")}shards:{  "_id" : "srs01",  "host" : "srs01/db1:27017,db2:27017,db3:27017",  "state" : 1,  "topologyTime" : Timestamp(1643628963, 2) }{  "_id" : "srs02",  "host" : "srs02/db1:27018,db2:27018,db3:27018",  "state" : 1,  "topologyTime" : Timestamp(1643628995, 1) }active mongoses:"5.0.5" : 2autosplit:Currently enabled: yesbalancer:Currently enabled: yesCurrently running: noFailed balancer rounds in last 5 attempts: 0Migration results for the last 24 hours: 512 : Success
...

可用的router已经变成了2

active mongoses:"5.0.5" : 2

4.访问分片

和对单机mongodb的访问方式一致,对sharding的访问可以用mongo shell方式:

[zyi@centos ~]$ sudo /usr/local/bin/mongo --port 27018
MongoDB shell version v5.0.5
connecting to: mongodb://127.0.0.1:27018/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b5aec9bd-145f-4348-bff2-d07aed46f13a") }
MongoDB server version: 5.0.5
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting: 2022-01-31T11:32:54.563+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted2022-01-31T11:32:54.563+00:00: You are running this process as the root user, which is not recommended
---
mongos>

如使用图形化终端如compass,访问mongodb://ip of mongos:port即可

增加分片副本集

进入mongos后使用命令添加sharding副本集
使用命令:sh.addShard( “/s1-mongo1.example.net:27018,s1-mongo2.example.net:27018,s1-mongo3.example.net:27018”)

mongos> show dbs
admin   0.000GB
config  0.000GB
mongos> sh.addShard('srs01/db1:27017,db2:27017,db3:27017')
{"shardAdded" : "srs01","ok" : 1,"$clusterTime" : {"clusterTime" : Timestamp(1643628964, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}},"operationTime" : Timestamp(1643628964, 1)
}

添加完成以后可以查看分片状态

mongos> sh.status()
--- Sharding Status --- sharding version: {"_id" : 1,"minCompatibleVersion" : 5,"currentVersion" : 6,"clusterId" : ObjectId("61f7c76011fc11df29b8cf4b")}shards:{  "_id" : "srs01",  "host" : "srs01/db1:27017,db2:27017,db3:27017",  "state" : 1,  "topologyTime" : Timestamp(1643628963, 2) }{  "_id" : "srs02",  "host" : "srs02/db1:27018,db2:27018,db3:27018",  "state" : 1,  "topologyTime" : Timestamp(1643628995, 1) }active mongoses:"5.0.5" : 1autosplit:Currently enabled: yesbalancer:Currently enabled: yesCurrently running: noFailed balancer rounds in last 5 attempts: 0Migration results for the last 24 hours: No recent migrationsdatabases:{  "_id" : "config",  "primary" : "config",  "partitioned" : true }

已经加入了shards

shards:{  "_id" : "srs01",  "host" : "srs01/db1:27017,db2:27017,db3:27017",  "state" : 1,  "topologyTime" : Timestamp(1643628963, 2) }{  "_id" : "srs02",  "host" : "srs02/db1:27018,db2:27018,db3:27018",  "state" : 1,  "topologyTime" : Timestamp(1643628995, 1) }

分片的数据操作

分片键
分片键可以是单个索引字段,也可以是复合索引所涵盖的多个字段,复合索引确定集合文档在集群分片之间的分布。

MongoDB将分片键值(或散列分片键值)的范围划分为分片键值(或散列分片键值)的非重叠范围。每个范围都与一个区块相关联,MongoDB尝试在集群中的分片之间均匀分布区块。

分片规则
对集合进行分片时,你需要选择一个分片键(Shard Key) , shard key 是每条记录都必须包含的,且建立了
索引的单个字段或复合字段,MongoDB按照片键将数据划分到不同的 数据块 中,并将 数据块均衡地分布到所有分片中.为了按照片键划分数据块,MongoDB使用 基于哈希的分片方式(随机平均分配)或者基于范围的分片方式(数值大小分配) 。

  1. 哈希策略
    对于 基于哈希的分片 ,MongoDB计算一个字段的哈希值,并用这个哈希值来创建数据块.
    在使用基于哈希分片的系统中,拥有”相近”片键的文档 很可能不会 存储在同一个数据块中,因此数据的分离性更好一些。
  2. 范围策略
    对 基于范围的分片 ,MongoDB按照片键的范围把数据分成不同部分。
    假设有一个数字的片键:想象一个从负无穷到正无穷的直线,每一个片键的值都在直线上画了一个点.MongoDB把这条直线划分为更短的不重叠的片段,并称之为 数据块 ,每个数据块包含了片键在一定范围内的数据。
    在使用片键做范围划分的系统中,拥有”相近”片键的文档很可能存储在同一个数据块中,因此也会存储在同一个分片中。

我们测试使用哈希策略:
需要使用分片的数据分布,需要先指定数据库和集合的分片键方法

mongos> sh.enableSharding('etaondb')
{"ok" : 1,"$clusterTime" : {"clusterTime" : Timestamp(1643629251, 29),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}},"operationTime" : Timestamp(1643629251, 28)
}
mongos> sh.shardCollection('etaondb.comment',{'nickname':'hashed'})
{"collectionsharded" : "etaondb.comment","ok" : 1,"$clusterTime" : {"clusterTime" : Timestamp(1643629362, 42),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}},"operationTime" : Timestamp(1643629362, 38)
}

此时查看分片状态

mongos> sh.status()
--- Sharding Status --- sharding version: {"_id" : 1,"minCompatibleVersion" : 5,"currentVersion" : 6,"clusterId" : ObjectId("61f7c76011fc11df29b8cf4b")}shards:{  "_id" : "srs01",  "host" : "srs01/db1:27017,db2:27017,db3:27017",  "state" : 1,  "topologyTime" : Timestamp(1643628963, 2) }{  "_id" : "srs02",  "host" : "srs02/db1:27018,db2:27018,db3:27018",  "state" : 1,  "topologyTime" : Timestamp(1643628995, 1) }active mongoses:"5.0.5" : 2autosplit:Currently enabled: yesbalancer:Currently enabled: yesCurrently running: noFailed balancer rounds in last 5 attempts: 0Migration results for the last 24 hours: 512 : Successdatabases:{  "_id" : "config",  "primary" : "config",  "partitioned" : true }config.system.sessionsshard key: { "_id" : 1 }unique: falsebalancing: truechunks:srs01   512srs02   512too many chunks to print, use verbose if you want to force print{  "_id" : "etaondb",  "primary" : "srs02",  "partitioned" : true,  "version" : {  "uuid" : UUID("4bd8a393-94dd-442c-8b4d-8327dd5bb7b0"),  "timestamp" : Timestamp(1643629251, 26),  "lastMod" : 1 } }etaondb.commentshard key: { "nickname" : "hashed" }unique: falsebalancing: truechunks:srs01   2srs02   2{ "nickname" : { "$minKey" : 1 } } -->> { "nickname" : NumberLong("-4611686018427387902") } on : srs01 Timestamp(1, 0) { "nickname" : NumberLong("-4611686018427387902") } -->> { "nickname" : NumberLong(0) } on : srs01 Timestamp(1, 1) { "nickname" : NumberLong(0) } -->> { "nickname" : NumberLong("4611686018427387902") } on : srs02 Timestamp(1, 2) { "nickname" : NumberLong("4611686018427387902") } -->> { "nickname" : { "$maxKey" : 1 } } on : srs02 Timestamp(1, 3)
mongos>

在最后的部分,已经可以看到数据库etaondb的集合comment使用哈希策略。

增加数据
使用*for(var i=1;i<=1000;i++) {db.comment.insert({_id:i+"",nickname:“BoBo”+i})}*增加1000个数据

mongos> for(var i=1;i<=1000;i++) {db.comment.insert({_id:i+"",nickname:"BoBo"+i})}
WriteResult({ "nInserted" : 1 })
mongos> db.comment.count()
1000

在查看分片存储数据情况:

srs01:PRIMARY> use etaondb
switched to db etaondb
srs01:PRIMARY> db.comment.count()
507
......
srs02:PRIMARY> use etaondb
switched to db etaondb
srs02:PRIMARY> db.comment.count()
493

两个分片的数据数分别是507和493,基本上平衡分配。

Mongodb-sharding相关推荐

  1. MongoDB sharding迁移那些事(一)

    如果不了解 MongoDB Sharded Cluster 原理,请先阅读 MongoDB Sharded cluster架构原理 关于MongoDB Sharding,你应该知道的 关于 shard ...

  2. MongoDB Sharding分片配置

    Ps:mongod是mongodb实例,mongos被默认为为mongodb sharding的路由实例. 本文使用的mongodb版本为3.2.9,因此参考网址为:https://docs.mong ...

  3. MongoDB Sharding 请勿复用已删除的 namespace

    SERVER-17397: Dropping a Database or Collection in a Sharded Cluster may not fully succeed 是 MongoDB ...

  4. MongoDB Sharding 机制分析

    MongoDB 是一种流行的非关系型数据库.作为一种文档型数据库,除了有无 schema 的灵活的数据结构,支持复杂.丰富的查询功能外,MongoDB 还自带了相当强大的 sharding 功能. 要 ...

  5. MongoDB Sharding

    MongoDB Sharding Sharding结构 replica set mmap Chunk的分裂和迁移 Chunk的分裂 Chunk的迁移 Sharding结构 转载自https://www ...

  6. MongoDB sharding模式实现(http://blog.chinaunix.net/xmlrpc.php?r=blog/articleuid=28266791id=5758139 )

    环境准备 1.本例使用3台Linux主机,IP地址如下: 一. 点击(此处)折叠或打开 Server A: 192.168.106.101 Server B: 192.168.106.102 Serv ...

  7. MongoDB Sharding使用总结

    Shard Keys 切片字段的选择 切片方式的选择 查询效果的验证 Balancer Others MongoDB的Sharding机制能够让数据库系统以接近线性的方式进行扩展,非常适合具备大数据集 ...

  8. MongoDB sharding 集合不分片性能更高?

    最近云上用户用户遇到一个 sharding 集群性能问题的疑惑,比较有代表性,简单分享一下 测试配置 mongos x 2.shard x 3 测试1:集合不开启分片,批量 insert 导入数据,每 ...

  9. Mongodb sharding转换一个副本集为分片集群

    2019独角兽企业重金招聘Python工程师标准>>> 1. 部署一个测试副本集 创建第一个副本集实例,名称为firstset: 1.1 创建副本集并且插入数据如下:/data/ex ...

  10. 故障分析 | MongoDB Sharding QPS 分布不均案例一则

    作者:任坤 现居珠海,先后担任专职 Oracle 和 MySQL DBA,现在主要负责 MySQL.mongoDB 和 Redis 维护工作. 本文来源:原创投稿 *爱可生开源社区出品,原创内容未经授 ...

最新文章

  1. 迷失在小镇上的日记(16)
  2. Windows域内的时间同步
  3. java中Mark接口_JVM源码分析之Java对象头实现
  4. LR逻辑斯蒂回归 — 机器学习面试
  5. 自己的 并查集 模板
  6. 设计心理学-产品设计的几个原则
  7. Unity3d美颜滤镜
  8. 深度学习:基本概要:监督,无监督,半监督,弱监督,多示例,迁移学习
  9. docker 查看容器日志命令
  10. ADS1255/6 使用
  11. ConvNext模型复现--CVPR2022
  12. Python 位操作符(Bitwise)
  13. Unity3D游戏开发入门学习笔记
  14. js中 ‘ ‘==0 为什么等于true?
  15. 替换MP9486A 替代MP4689 替换LM5007 LM5017 国产芯片内置150V做降压恒压4.2V GPS防盗器专用芯片
  16. 【华为OD机试真题 JS】德州扑克
  17. Spfa算法总结(C/C++)
  18. 问题 A: C语言11.1——完成一个对候选人得票的统计程序。
  19. 光通讯元件的程序烧录
  20. 图木舒克市谷歌高清卫星地图下载

热门文章

  1. LeetCode随缘刷题之回文数
  2. python解析jmeter.jtl文件_jmeter jtl文件解析
  3. 如果你也准备踏入网络安全行业,收藏这一篇就够了(内含282G网络安全资料)
  4. [Code Jam] Millionaire
  5. 道路车辆先进驾驶辅助系统(ADAS)术语及定义
  6. 1996亚特兰大奥运会歌曲《Reach》铃声 1996亚特兰大奥运会歌曲...
  7. 内存和硬盘、磁盘的区别
  8. 计算机恢复出厂设置xp,教你几个步骤就可以学会xp系统恢复出厂设置
  9. 【草稿】程序学习学纲领-计算机时代下对批判的批判
  10. 吴恩达最新采访!以数据为中心的原因