MongoDB整理笔记のSharding分片
这是一种将海量的数据水平扩展的数据库集群系统,数据分表存储在sharding 的各个节点上,使用者通过简单的配置就可以很方便地构建一个分布式MongoDB 集群。
MongoDB 的数据分块称为 chunk。每个 chunk 都是 Collection 中一段连续的数据记录,通常最大尺寸是 200MB,超出则生成新的数据块。要构建一个 MongoDB Sharding Cluster,需要三种角色:
Shard Server
即存储实际数据的分片,每个Shard 可以是一个mongod 实例,也可以是一组mongod 实例构成的Replica Set。为了实现每个Shard 内部的auto-failover,MongoDB 官方建议每个Shard为一组Replica Set。
Config Server
为了将一个特定的collection 存储在多个shard 中,需要为该collection 指定一个shard key,例如{age: 1} ,shard key 可以决定该条记录属于哪个chunk。Config Servers 就是用来存储:所有shard 节点的配置信息、每个chunk 的shard key 范围、chunk 在各shard 的分布情况、该集群中所有DB 和collection 的sharding 配置信息。
Route Process
这是一个前端路由,客户端由此接入,然后询问Config Servers 需要到哪个Shard 上查询或保存记录,再连接相应的Shard 进行操作,最后将结果返回给客户端。客户端只需要将原本发给mongod 的查询或更新请求原封不动地发给Routing Process,而不必关心所操作的记录存储在哪个Shard 上。
下面我们在同一台物理机器上构建一个简单的 Sharding Cluster:
架构图如下:
Shard Server 1:20000
Shard Server 2:20001
Config Server :30000
Route Process:40000
(1)启动三服务
启动Shard Server
mkdir -p /data/shard/s0 --创建数据目录
mkdir -p /data/shard/s1
mkdir -p /data/shard/log --创建日志目录
/Apps/mongo/bin/mongod --shardsvr --port 20000 --dbpath /data/shard/s0 --fork --logpath
/data/shard/log/s0.log --directoryperdb --启动Shard Server 实例1
/Apps/mongo/bin/mongod --shardsvr --port 20001 --dbpath /data/shard/s1 --fork --logpath
/data/shard/log/s1.log --directoryperdb --启动Shard Server 实例2
启动Config Server
mkdir -p /data/shard/config --创建数据目录
/Apps/mongo/bin/mongod --configsvr --port 30000 --dbpath /data/shard/config --fork --logpath
/data/shard/log/config.log --directoryperdb --启动Config Server 实例
启动Route Process
/Apps/mongo/bin/mongos --port 40000 --configdb localhost:30000 --fork --logpath
/data/shard/log/route.log --chunkSize 1 --启动Route Server 实例
mongos 启动参数中,chunkSize 这一项是用来指定chunk 的大小的,单位是MB,默认大小为200MB,为了方便测试Sharding 效果,我们把chunkSize 指定为 1MB。
(2)配置Sharding
接下来,我们使用MongoDB Shell 登录到mongos,添加Shard 节点
[root@localhost ~]# /Apps/mongo/bin/mongo admin --port 40000 --此操作需要连接admin 库
MongoDB shell version: 1.8.1
connecting to: 127.0.0.1:40000/admin
> db.runCommand({ addshard:"localhost:20000" }) --添加 Shard Server
{ "shardAdded" : "shard0000", "ok" : 1 }
> db.runCommand({ addshard:"localhost:20001" })
{ "shardAdded" : "shard0001", "ok" : 1 }
> db.runCommand({ enablesharding:"test" }) --设置分片存储的数据库
{ "ok" : 1 }
> db.runCommand({ shardcollection: "test.users", key: { _id:1 }}) --设置分片的集合名称,且必
须指定Shard Key,系统会自动创建索引
{ "collectionsharded" : "test.users", "ok" : 1 }
>
(3)验证Sharding正常工作
我们已经对test.users 表进行了分片的设置,下面我们们插入一些数据看一下结果
> use test
switched to db test
> for (var i = 1; i <= 500000; i++) db.users.insert({age:i, name:"wangwenlong", addr:"Beijing",
country:"China"})
> db.users.stats()
{
"sharded" : true, --说明此表已被shard
"ns" : "test.users",
"count" : 500000,
"size" : 48000000,
"avgObjSize" : 96,
"storageSize" : 66655232,
"nindexes" : 1,
"nchunks" : 43,
"shards" : {
"shard0000" : { --在此分片实例上约有24.5M 数据
"ns" : "test.users",
"count" : 254889,
"size" : 24469344,
"avgObjSize" : 96,
"storageSize" : 33327616,
"numExtents" : 8,
"nindexes" : 1,
"lastExtentSize" : 12079360,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 11468800,
"indexSizes" : {
"_id_" : 11468800
},
"ok" : 1
},
"shard0001" : { --在此分片实例上约有23.5M 数据
"ns" : "test.users",
"count" : 245111,
"size" : 23530656,
"avgObjSize" : 96,
"storageSize" : 33327616,
"numExtents" : 8,
"nindexes" : 1,
"lastExtentSize" : 12079360,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 10649600,
"indexSizes" : {
"_id_" : 10649600
},
"ok" : 1
}
},
"ok" : 1
}
>
我们看一下磁盘上的物理文件情况
[root@localhost bin]# ll /data/shard/s0/test --此分片实例上有数据产生
总计 262420
-rw------- 1 root root 16777216 06-03 15:21 test.0
-rw------- 1 root root 33554432 06-03 15:21 test.1
-rw------- 1 root root 67108864 06-03 15:22 test.2
-rw------- 1 root root 134217728 06-03 15:24 test.3
-rw------- 1 root root 16777216 06-03 15:21 test.ns
[root@localhost bin]# ll /data/shard/s1/test --此分片实例上有数据产生
总计 262420
-rw------- 1 root root 16777216 06-03 15:21 test.0
-rw------- 1 root root 33554432 06-03 15:21 test.1
-rw------- 1 root root 67108864 06-03 15:22 test.2
-rw------- 1 root root 134217728 06-03 15:23 test.3
-rw------- 1 root root 16777216 06-03 15:21 test.ns
[root@localhost bin]#
看上述结果,表明test.users 集合已经被分片处理了,但是通过mongos 路由,我们并感觉不到是数据存放在哪个shard 的chunk 上的,这就是MongoDB 用户体验上的一个优势,即对用户是透明的。
MongoDB整理笔记のSharding分片的更多相关文章
- MongoDB整理笔记のReplica Sets + Sharding
MongoDB Auto-Sharding 解决了海量存储和动态扩容的问题,但离实际生产环境所需的高可靠.高可用还有些距离,所以有了"Replica Sets + Sharding" ...
- MongoDB整理笔记の管理Sharding
1.列出所有的Shard Server > db.runCommand({ listshards: 1 }) --列出所有的Shard Server { "shards" : ...
- MongoDB整理笔记の新增Shard Server
1.启动一个新Shard Server 进程 [root@localhost ~]# mkdir /data/shard/s2 [root@localhost ~]# /Apps/mongo/bin/ ...
- MongoDB 学习笔记之 分片和副本集混合运用
分片和副本集混合运用: 基本架构图: 搭建详细配置: 3个shard + 3个replicat set + 3个configserver + 3个Mongos shardrsname Primary ...
- MongoDB整理笔记のMapReduce
MongDB的MapReduce相当于MySQL中的“group by”,所以在MongoDB上使用Map/Reduce进行并行“统计”很容易. 使用MapReduce要实现两个函数Map函数和Red ...
- MongoDB整理笔记のCapped Collection
1.简单介绍 capped collections 是性能出色的有着固定大小的集合,以LRU(Least Recently Used 最近最少使用)规则和插入顺序进行age-out(老化移出)处理,自 ...
- MongoDB整理笔记の走进MongoDB世界
本人学习mongodb时间不长,但是鉴于工作的需要以及未来发展的趋势,本人想更深层的认识mongodb底层的原理以及更灵活的应用mongodb,边学边工作实践. mongodb属于nosql中算是最 ...
- MongoDB整理笔记のjava MongoDB分页优化
最近项目在做网站用户数据新访客统计,数据存储在MongoDB中,统计的数据其实也并不是很大,1000W上下,但是公司只配给我4G内存的电脑,让我程序跑起来气喘吁吁...很是疲惫不堪. 最常见的问题莫过 ...
- MongoDB整理笔记のID自增长
以下是官网原文地址: http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/ 概要 MongoDB 的_i ...
随机推荐
- HTTP重要概念
connection连接 一个传输层的实际环流,它是建立在两个相互通讯的应用程序之间. 在http1.1,request和reponse头中都有可能出现一个connection的头,此header的含 ...
- PL/SQL 训练12--动态sql和绑定变量
--什么是动态SQL?动态PL/SQL--动态SQL是指在运行时刻才构建执行的SQL语句--动态PL/SQL是指整个PL/SQL代码块都是动态构建,然后再编译执行 --动态SQL来可以用来干什么? - ...
- Spring MVC启动时初始化的几个常用方法
Spring-MVC的应用中,要实现类似的功能,主要是通过实现下面这些接口(任选一,至少一个即可) 一.ApplicationContextAware接口 +? 1 2 3 4 5 6 7 8 9 p ...
- vs2017 android demo
vs2017自安装以后就没怎么打开过,虽然12出的时候用10,15出的时候用13,17出的时候用15,但我依然坚持不用也装上再说的理念. 1.vs2017开发IOS和Android安装所必不可少的,u ...
- BurpSuite系列(九)----Comparer模块(比较器)
一.简介 Burp Comparer在Burp Suite中主要提供一个可视化的差异比对功能,来对比分析两次数据之间的区别.使用中的场景可能是: 1.枚举用户名过程中,对比分析登陆成功和失败时,服务器 ...
- Cause: java.sql.SQLException: 无效的列索引
今天调试代码发现“Cause: java.sql.SQLException: 无效的列索引”,查资料得出结论如下: 1.sql串的?号用''括了起来. 例如:select* from user t ...
- Python基础学习四 文件操作(二)
####读取文件#### with open('goods_info.txt', 'r', encoding='utf-8') as f: f.seek(0) # 注意指针位置 goods_info ...
- 【279】◀▶ Python 运算符说明
参考:Python 运算符说明 目录: 一.算术运算符 二.比较(关系)运算符 三.赋值运算符 四.位运算符 五.逻辑运算符 六.成员运算符 七.身份运算符 八.运算符优先级 一.Python 算术运 ...
- angular与avalon对复杂对象的修改
angular的实现 <!doctype html> <html ng-app> <head> <script src="http://files. ...
- ubuntu搭建定时任务管理器
一.安装golang 1.apt-get安装golang $ sudo apt-get update $ sudo apt-get install -y golang 2.创建Go语言的工作文件夹,并 ...