分片(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. struts2学习笔记--OGNL表达式1

    struts2标签库主要使用的是OGNL语言,类似于El表达式,但是强大得多,它是一种操作对象属性的表达式语言,OGNL有自己的优点: 能够访问对象的方法,如list.size(); 能够访问静态属性 ...

  2. 资源等待类型sys.dm_os_wait_stats

    动态管理视图  sys.dm_os_wait_stats 返回执行的线程所遇到的所有等待的相关信息.可以使用该聚合视图来诊断 SQL Server 以及特定查询和批处理的性能问题. 列名 数据类型 说 ...

  3. sqlite - java 初学

    进来准备使用一种embedded database,即嵌入式数据库,方便随项目本地存储.目前学习打算是sqlite和H2. document:http://www.runoob.com/sqlite/ ...

  4. 小白Linux入门 三

    环境变量 shell 变量: 内存空间 ,命名的内存空间 echo $SHELL 其中SHELL是变量 里面是/bin/bash sudo su  进入root printenv 命令 命令: 内部命 ...

  5. 分享在winform下实现左右布局多窗口界面-续篇

    之前的这篇文章<分享在winform下实现左右布局多窗口界面>已经实现了左右布局多窗口界面,今天本来是研究基于winform的插件编程,没想到顺便又找到了另一种实现方案,这种实现方案更简单 ...

  6. .Net语言 APP开发平台——Smobiler学习日志:如何在手机上显示类似EXCEL表格

    最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 样式一 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的&qu ...

  7. 【夯实PHP基础系列】JQuery easyUI的使用

    最近在做一个公司的后台项目中,接触到 JQuery easyUI前端框架,被她简洁的代码和简单有效的ajax交互所深深吸引. 体会有以下3个方面: 1)快速创建表格的能力: 后端程序,比如PHP只需要 ...

  8. IT基础架构规划方案之实际网络设计案例

    根据某集团总部新办公大楼.厂房和分支机构(店面)的情况,以及IT部门对网络节点数.网络应用和分支机构(店面)的初步规划,对企业的总体网络拓扑结构进行设计,如下图. 设备选型和部署参考: 类型 设备选型 ...

  9. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(22)-为用户设置角色

    ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框架搭建    (2):数据库访问层的设计Demo    (3):面向接口编程   (4 ):业务逻辑层的封装    ...

  10. Group-buy项目总结

    这是我做的第一个移动端项目,和传统PC端网站不同的是,做移动端的网站要适配各种尺寸的设备. 在默认情况下,移动设备上的viewport都是要大于浏览器可视区域的,这是因为考虑到移动设备的分辨率相对于桌 ...