分片(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. JS去除空格方法记录

    JS中去掉空格 //去除空格  String.prototype.Trim = function() {      return this.replace(/\s+/g, ""); ...

  2. 优化JS加载时间过长的一种思路

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 去年公司在漳州的一个项目中,现场工程人员反映地图部分出图有点 ...

  3. 基于Angularjs实现分页

    前言 学习任何一门语言前肯定是有业务需求来驱动你去学习它,当然ng也不例外,在学习ng前我第一个想做的demo就是基于ng实现分页,除去基本的计算思路外就是使用指令封装成一个插件,在需要分页的列表页面 ...

  4. TCP三次握手,四次挥手

    前言 在面试的过程中,TCP的传输协议经常会出现.以前我参加面试的过程中就被问到过,现在轮到我面试其他人的时候,我也会问一些相关的问题.作为一名开发者,无论使用什么样的开发语言,最基本的网络知识一定要 ...

  5. idea 自动提示生成 serialVersionUID

    from: http://tonycody.blog.51cto.com/8421818/1401422 Intellij IDEA 默认没启用这个功能. Setting->Inspection ...

  6. SQL Server 2016 SP1 标准版等同企业版?!

    上周微软发布了SQL Server的历史性公告:SQL Server 标准版的SP1提供你和企业版一样得功能.你不信的话?可以点击这里. 这改变了整个关系数据库市场,重重打击了Oracle.在今天的文 ...

  7. SQL常见的系统存储过程

    1.sp_datebases 列出服务器上的所有数据库信息,包括数据库名称和数据库大小 例:exec sp_datebases 2.sp_helpdb 报告有关指定数据库或所有数据库的信息 例:exe ...

  8. Rafy 领域实体框架设计 - 重构 ORM 中的 Sql 生成

    前言 Rafy 领域实体框架作为一个使用领域驱动设计作为指导思想的开发框架,必然要处理领域实体到数据库表之间的映射,即包含了 ORM 的功能.由于在 09 年最初设计时,ORM 部分的设计并不是最重要 ...

  9. 迷惑很久,仅以个人想法谈谈MVC架构,希望大家多给点意见

    博主是非科班出身,所以和大部分新手有着一样的困惑,究竟什么才能算是MVC框架,总是在谈Model,View,Controller分离,可是究竟什么才能算是分离,而他们又是负责什么样的分工呢. 大二的时 ...

  10. csharp: MySQL Stored Procedure using DAL

    # 建表 塗聚文 20160907 drop table attendrecord; create table attendrecord ( seq INT NOT NULL PRIMARY KEY ...