MongoDB学习笔记——分片(Sharding)
分片(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)的更多相关文章
- MongoDB学习笔记(六)--复制集+sharding分片 && 总结
复制集+sharding分片 背景 主机 IP 服务及端口 Server A ...
- MongoDB学习笔记(五)--复制集 && sharding分片
主从复制 主从节点开启 主节 ...
- mongoDB 学习笔记纯干货(mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等)
最后更新时间:2017-07-13 11:10:49 原始文章链接:http://www.lovebxm.com/2017/07/13/mongodb_primer/ MongoDB - 简介 官网: ...
- MongoDB学习笔记(四)--索引 && 性能优化
索引 基础索引 ...
- MongoDB学习笔记:快速入门
MongoDB学习笔记:快速入门 一.MongoDB 简介 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情况下,添加更多的节点,可以保证服务器性能.M ...
- MongoDB学习笔记(1):MongoDB的安装和说明
MongoDB学习笔记(1):MongoDB的安装和说明 快速开始 下载地址 官网下载: https://www.mongodb.com/download-center?jmp=nav#communi ...
- MongoDB学习笔记:MongoDB 数据库的命名、设计规范
MongoDB学习笔记:MongoDB 数据库的命名.设计规范 第一部分,我们先说命名规范. 文档 设计约束 UTF-8 字符 不能包含 \0 字符(空字符),这个字符标识建的结尾 . 和 $ ...
- MongoDB学习笔记系列
回到占占推荐博客索引 该来的总会来的,Ef,Redis,MVC甚至Sqlserver都有了自己的系列,MongoDB没有理由不去整理一下,这个系列都是平时在项目开发时总结出来的,希望可以为各位一些帮助 ...
- PHP操作MongoDB学习笔记
<?php/*** PHP操作MongoDB学习笔记*///*************************//** 连接MongoDB数据库 **////*************** ...
随机推荐
- 【集合框架】JDK1.8源码分析之Comparable && Comparator(九)
一.前言 在Java集合框架里面,各种集合的操作很大程度上都离不开Comparable和Comparator,虽然它们与集合没有显示的关系,但是它们只有在集合里面的时候才能发挥最大的威力.下面是开始我 ...
- Android之仿ele地图定位效果
PS:最近项目要求,希望在选择地址的时候能够仿ele来实现定位效果.因此就去做了一下.不过ele使用高德地图实现的,我是用百度地图实现的.没办法,公司说用百度那就用百度的吧.个人觉得高德应该更加的精准 ...
- 让你分分钟学会Javascript中的闭包
Javascript中的闭包 前面的话: 闭包,是 javascript 中重要的一个概念,对于初学者来讲,闭包是一个特别抽象的概念,特别是ECMA规范给的定义,如果没有实战经验,你很难从定义去理解它 ...
- MySQL一个语句查出各种整形占用字节数及最大最小值
直接上码: as min_num union , union , union , union , union ,) union ,) union ,) union ,) union ,); +---- ...
- 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing
[源码下载] 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing 作者:webabcd 介绍背水一 ...
- eclipse报错:Failed to load the JNI shared library
Eclipse运行时提示“Failed to load the JNI shared library /Java/jre6/bin/client/jvm.dll”的一个解决方案 因为 Eclipse ...
- 【nodejs笔记3】Express基本用法,和路由控制,和模板渲染ejs
1. 路由控制的工作原理 //routes/index.js中的代码//访问主页时,调用ejs模板引擎,渲染index.ejs模板文件,生成静态页面,并显示在浏览器中.router.get('/', ...
- Java--JDK动态代理核心源码解析
1.首先我们了解一下JDK动态代理的使用方法: public static void main(String[] args) { /** * 创建一个Bean对象,该对象实现BeanInterFace ...
- php 实现设计模式之 享元模式
<?php class Book{ public $title; public $author; public function __construct($title,$author){ $th ...
- cessss
[文字] 关注1啊啊啊啊啊 点击关注微信 点击关注微信2 点击关注微信3 关注2啊啊啊啊啊啊啊啊啊啊啊 关注3啊啊啊啊啊啊啊啊 关注4啊啊啊啊啊啊啊啊 关注5啊啊啊啊啊啊啊啊 关注6啊啊啊啊啊啊啊啊啊 ...