分片(Sharding)

分片就是将数据进行拆分,并将其分别存储在不同的服务器上 MongoDB支持自动分片能够自动处理数据在分片上的分布

MongoDB分片有三种角色

  • 配置服务器:一个单独的mongod进程,主要记录了哪个分片服务器包含了哪些数据的信息,保存的只是数据的分布表,如果配置服务器不可用时,将变为只读,不能进行分片和数据迁移,
    配置服务器的1KB空间相当于真实数据的200MB,所以配置服务器不需要太多的资源和配置。但是每个配置服务器都建议部署在不同的物理机上,
    配置服务器相当于整个集群的大脑保存了集群和分片的元数据,并且mongos路由服务器需要从配置服务器获取配置信息,
    因此应该首先建立配置服务器,并且启用日志功能。自3.2开始配置服务器可以部署为副本集。
    使用副本集作为配置服务器时要满足以下条件:
    • 使用的副本集的配置服务器分片集群要超过 3 配置服务器,因为副本集可以有最多 50 个成员
    • 要将配置服务器部署为副本集,配置服务器必须运行 WiredTiger 存储引擎
    • 配置服务器部署为副本集时必须要没有仲裁者
    • 配置服务器为副本集时不能存在延迟节点slaveDelay不能为0
    • 必须建立的索引
      (即没有成员应该有 buildIndexes 设置为 false)
  • 路由服务器:mongos进程,起到一个前端路由的功能,供客户端进行接入,本身不会保存数据,在启动时从配置服务器加载集群信息,所以配置路由服务器时,不需要指定数据目录,开启mongos进程需要知道配置服务器的地址,指定configdb选项。
    当客户端连接到路由服务器时,会询问配置服务器需要到哪个分片服务器上查询或保存记录,然后再链接相应的Shard进行操作,最后将结果整合给返回给客户端
  • 分片服务器:可以是一个副本集或单独的mongod进程,保存分片后的集合数据

块(chunk)

MongoDB将数据拆分为chunk,每个chunk都是collection中的一段连续的数据记录,为防止一个chunk变的越来越大,当一个chunk增加到特定大小时,会被自动拆分为两个较小的chunk。默认块大小为64M 新的分片集合刚开始只有一个chunk,所有文档都位于这个chunk中,块的范围是$minKey至$maxKey。随着数据的增加会被拆分为多个块,块的范围也会逐步被调整,多使用[a,b)表示区间范围

均衡器(balancer)

均衡器周期性的检查每个分片之间的数据是否存在不均衡情况,如果存在,就会进行块的迁移。

片键

对集合进行分片时,需要选择一个或多个字段用于数据拆分。
拆分数据最常用的数据分发方式有三种,升序片键,随机分发的片键,小基数片键。

  • 升序片键:
    升序片键会导致所有数据总是被添加到最后一个数据块中,导致存在一个单一不可分散的人点,用一个分片承担了所有的读写。
  • 随机分发片键:
    这种分片虽然可以得到一组均匀分布于各分片的数据块。但是考虑到数据序列的随机性,一般情况下这个数据不会被加载到内存中,所以此时的MongoDB会引发大量的磁盘IO 给RAM带来更大压力,并且由于片键必须有索引,所以如果选择了不依据它进行查询的随机键,基本浪费了一个索引,导致MongoDB写操作变慢。
  • 小基数片键:
    即片键值个数有限的键,这种片键容易导致块的个数有限,并且导致块的体积越来越大。

建议使用准升序键加搜索键的组合片键
升序片键最好能够对应N多个数据块,搜索键则应当是通常用来查询的字段,搜索键不能是一个升序字段,这样会把片键降级为一个升序片键,应该具有非升序,分布随即,且基数适当的特点

注意:

  • 片键上必须有索引,因此如果选择了从不依据索引查询的随机键,基本上可以说浪费了一个索引,另一方面索引的增加会降低写操作的速度,所以降低索引量也是非常必要的。
  • 片键不可以是数组
  • 应该选择不会被改变或很少发生变动的字段作为片键,因为文档一旦插入,正则片键值就无法在进行修改了,如果要修改则必须先删除在进行修改

部署分片集群

1.创建配置服务器:

// config 1

systemLog:

path: D:\mongodb\sharding\config\c0\logs\mongodb.log

logAppend: true

destination: file

storage:

dbPath: D:\mongodb\sharding\config\c0\data

net:

port: 27020

bindIp: 127.0.0.1

security:

keyFile: D:\mongodb\sharding\config\c0\key

sharding:

clusterRole: configsvr

// config 2

systemLog:

path: D:\mongodb\sharding\config\c1\logs\mongodb.log

logAppend: true

destination: file

storage:

dbPath: D:\mongodb\sharding\config\c1\data

net:

port: 27021

bindIp: 127.0.0.1

security:

keyFile: D:\mongodb\sharding\config\c1\key

sharding:

clusterRole: configsvr

//config 3

systemLog:

path: D:\mongodb\sharding\config\c2\logs\mongodb.log

logAppend: true

destination: file

storage:

dbPath: D:\mongodb\sharding\config\c2\data

net:

port: 27022

bindIp: 127.0.0.1

security:

keyFile: D:\mongodb\sharding\config\c2\key

sharding:

clusterRole: configsvr

启动以上三个配置服务器

mongod -config D:\mongodb\sharding\config\c0\mongodb.conf

mongod -config D:\mongodb\sharding\config\c1\mongodb.conf

mongod -config D:\mongodb\sharding\config\c2\mongodb.conf

配置路由服务器

//route 1

systemLog:

path: D:\mongodb\sharding\route\r0\logs\mongodb.log

logAppend: true

destination: file

net:

port: 27020

bindIp: 127.0.0.1

security:

keyFile: D:\mongodb\sharding\route\r0\key

sharding:

autoSplit: true

configDB: 127.0.0.1:27010,127.0.0.1:27011,127.0.0.1:27012

chunkSize: 64

//route 2

systemLog:

path: D:\mongodb\sharding\route\r1\logs\mongodb.log

logAppend: true

destination: file

net:

port: 27021

bindIp: 127.0.0.1

security:

keyFile: D:\mongodb\sharding\route\r1\key

sharding:

autoSplit: true

configDB: 127.0.0.1:27010,127.0.0.1:27011,127.0.0.1:27012

chunkSize: 64

启动以上两个路由服务器

mongos -config D:\mongodb\sharding\route\r1\mongodb.conf

mongos -config D:\mongodb\sharding\route\r1\mongodb.conf

配置分片服务器

//shard 1

systemLog:

path: D:\mongodb\sharding\shards\s0\logs\mongodb.log

logAppend: true

destination: file

storage:

dbPath: D:\mongodb\sharding\shards\s0\data

net:

port: 27030

bindIp: 127.0.0.1

security:

keyFile: D:\mongodb\sharding\shards\s0\key

sharding:

clusterRole: shardsvr

//shard 2

systemLog:

path: D:\mongodb\sharding\shards\s1\logs\mongodb.log

logAppend: true

destination: file

storage:

dbPath: D:\mongodb\sharding\shards\s1\data

net:

port: 27031

bindIp: 127.0.0.1

security:

keyFile: D:\mongodb\sharding\shards\s1\key

sharding:

clusterRole: shardsvr

启动分片服务器

 

mongod -config D:\mongodb\sharding\shards\s0\mongodb.conf

mongod -config D:\mongodb\sharding\shards\s1\mongodb.conf

登录到mongos ,添加shard分片

mongo --port 27021

添加分片服务器

db.runCommand({addshard:"127.0.0.1:27030"})

sh.addShard("127.0.0.1:27031")

配置分片存储的数据库 sh.enableSharding(dbname) dbname-数据库名称

 

sh.enableSharding("testSharding")

设置分片集合的名称及指定片键 sh.shardCollection(fullName,key,unique) fullname-dbname.collectionname 数据库名称+集合名称;key-片键;unique-默认为true,为true时在基础索引上创建唯一约束

sh.shardCollection("testSharding.users",{userName:1})

创建测试数据

use testSharding

 

for (i = 5000; i < 100000; i++) {

db.users.insert({

"i": i,

"userName": "user" + i,

"age": Math.floor(Math.random() * 120),

"created": new Date(),

total: Math.floor(Math.random() * 100) * i

})

}

为副本集

//shardRepliSet1

 

systemLog:

path: D:\mongodb\sharding\shards\s2\rs0\logs\mongodb.log

logAppend: true

destination: file

storage:

dbPath: D:\mongodb\sharding\shards\s2\rs0\data

net:

port: 27035

bindIp: 127.0.0.1

security:

keyFile: D:\mongodb\sharding\shards\s2\rs0\key

replication:

replSetName: replcaSetTest

secondaryIndexPrefetch: all

sharding:

clusterRole: shardsvr

 

//shardRepliSet2

 

systemLog:

path: D:\mongodb\sharding\shards\s2\rs1\logs\mongodb.log

logAppend: true

destination: file

storage:

dbPath: D:\mongodb\sharding\shards\s2\rs1\data

net:

port: 27032

bindIp: 127.0.0.1

security:

keyFile: D:\mongodb\sharding\shards\s2\rs1\key

replication:

replSetName: replcaSetTest

secondaryIndexPrefetch: all

sharding:

clusterRole: shardsvr

 

 

//shardRepliSet3

 

systemLog:

path: D:\mongodb\sharding\shards\s2\rs2\logs\mongodb.log

logAppend: true

destination: file

storage:

dbPath: D:\mongodb\sharding\shards\s2\rs2\data

net:

port: 27033

bindIp: 127.0.0.1

security:

keyFile: D:\mongodb\sharding\shards\s2\rs2\key

replication:

replSetName: replcaSetTest

secondaryIndexPrefetch: all

sharding:

clusterRole: shardsvr

 

 

//shardRepliSet4

 

systemLog:

path: D:\mongodb\sharding\shards\s2\rs3\logs\mongodb.log

logAppend: true

destination: file

storage:

dbPath: D:\mongodb\sharding\shards\s2\rs3\data

net:

port: 27038

bindIp: 127.0.0.1

security:

keyFile: D:\mongodb\sharding\shards\s2\rs3\key

replication:

replSetName: replcaSetTest

secondaryIndexPrefetch: all

sharding:

clusterRole: shardsvr

启动副本集实例

mongod -config D:\mongodb\sharding\shards\s2\rs0\mongodb.conf

mongod -config D:\mongodb\sharding\shards\s2\rs1\mongodb.conf

mongod -config D:\mongodb\sharding\shards\s2\rs2\mongodb.conf

mongod -config D:\mongodb\sharding\shards\s2\rs3\mongodb.conf

配置及初始化副本集

rsConfig = {

_id: "replcaSetTest",

members: [{ _id: 0, host: "127.0.0.1:27032" },

{ _id: 1, host: "127.0.0.1:27033" },

{ _id: 2, host: "127.0.0.1:27035" },

{ _id: 3, host: "127.0.0.1:27038" }

]

};

rs.initiate(rsConfig);

添加副本集分片至分片配置

sh.addShard("replcaSetTest/127.0.0.1:27032,127.0.0.1:27033,127.0.0.1:27035,127.0.0.1:27038")

设置配置服务器为副本集
配置文件中添加节点clusterRole: configsvr 即可,其它请参考副本集配置。

db.users.stats()查看集合users分片信息

管理维护分片集群

列出所有的分片服务器

use admin

db.runCommand({listshards:1})

查看集群摘要信息

sh.status();

查看分片集群信息

printShardingStatus()

MongoDB学习笔记——分片(Sharding)的更多相关文章

  1. MongoDB学习笔记(六)--复制集+sharding分片 && 总结

    复制集+sharding分片                                                               背景 主机 IP 服务及端口 Server A ...

  2. MongoDB学习笔记(五)--复制集 && sharding分片

    主从复制                                                                                       主从节点开启 主节 ...

  3. mongoDB 学习笔记纯干货(mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等)

    最后更新时间:2017-07-13 11:10:49 原始文章链接:http://www.lovebxm.com/2017/07/13/mongodb_primer/ MongoDB - 简介 官网: ...

  4. MongoDB学习笔记(四)--索引 && 性能优化

    索引                                                                                             基础索引 ...

  5. MongoDB学习笔记:快速入门

    MongoDB学习笔记:快速入门   一.MongoDB 简介 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情况下,添加更多的节点,可以保证服务器性能.M ...

  6. MongoDB学习笔记(1):MongoDB的安装和说明

    MongoDB学习笔记(1):MongoDB的安装和说明 快速开始 下载地址 官网下载: https://www.mongodb.com/download-center?jmp=nav#communi ...

  7. MongoDB学习笔记:MongoDB 数据库的命名、设计规范

    MongoDB学习笔记:MongoDB 数据库的命名.设计规范     第一部分,我们先说命名规范. 文档 设计约束 UTF-8 字符 不能包含 \0 字符(空字符),这个字符标识建的结尾 . 和 $ ...

  8. MongoDB学习笔记系列

    回到占占推荐博客索引 该来的总会来的,Ef,Redis,MVC甚至Sqlserver都有了自己的系列,MongoDB没有理由不去整理一下,这个系列都是平时在项目开发时总结出来的,希望可以为各位一些帮助 ...

  9. PHP操作MongoDB学习笔记

    <?php/*** PHP操作MongoDB学习笔记*///*************************//**   连接MongoDB数据库  **////*************** ...

随机推荐

  1. iOS开发多线程篇—多线程简单介绍

    iOS开发多线程篇—多线程简单介绍 一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcod ...

  2. Jackson的简单用法

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1简介 Jackson具有比较高的序列化和反序列化效率,据测试,无论是 ...

  3. 简述ASP.NET MVC原理

    1.为什么ASP.NET需要MVC? 因为随着网站的的数量级越来越大,原始的网站方式,这里指的是WebForm,在运行速度和维护性方面,以及代码量上面,越来越难以满足日益庞大的网站维护成本.代码的重构 ...

  4. c++中的重名问题

    看C++视频教程-范磊主讲(2.91G)视频学习笔记整理. 1.c++为什么要引入名字空间(namespace)? c++引入名字空间这个概念,是为了避免重名问题,比如说避免重复的函数名或者变量名引起 ...

  5. .net提交HTML元素到后台,遇到Request报错 解决方案

    对于.NET MVC 项目来说,在Controller中对应的Action方法上打上标签: [ValidateInput(false)] 在MSDN上:HttpRequest 类使用输入验证标志来跟踪 ...

  6. DirectShow+VS2010+Win7配置说明

    不得不说,使用windows的东西确实很麻烦,DirectShow这个东西不断更新换代,书本上的知识完全跟不上时代,只能去博客上查资料.百度之后,看了大量的文章,终于完成了自己的DirectShow安 ...

  7. DirectShow Filter的开发实践

    一.介绍 摄像头图像采集处理在业界有着多种成熟的方案.从老的DirectShow.Grabber技术,到新的Windows Media Foundation框架,网络上都有着丰富的参考资料.OpenC ...

  8. LocalDB在IIS中的运行失败

    Using LocalDB with Full IIS, Part 1: User Profile http://blogs.msdn.com/b/sqlexpress/archive/2011/12 ...

  9. "System.Security.Cryptography.CryptographicException: 拒绝访问" 问题的解决方法

    .net web程序使用rsa算法进行加解密时,程序报告“System.Security.Cryptography.CryptographicException: 拒绝访问”错.按网上搜的解决方法做了 ...

  10. touchstart,touchmove,touchend触摸事件的小小实践心得

    近段时间使用html5开发一个公司内部应用,而触摸事件必然是移动应用中所必须的,刚开始以为移动设备上或许也会支持鼠标事件,原来是不支持的,好在webkit内核的移动浏览器支持touch事件,并且打包成 ...