1 减小单机请求数,将单机负载,提升总负载 mongodb
2 减小单机的存储空间,提升总存空间。 shell
下图一目了然: 数据库
简单注解: 服务器
1 mongos 路由进程, 应用程序接入mongos再查询到具体分片。 架构
2 config server 路由表服务。 每一台都具备所有chunk的路由信息。 app
3 shard为数据存储分片。 每一片均可以是复制集(replica set)。 ui
step 1 启动config server this
mkdir /data/configdb mongod --configsvr --dbpath /data/configdb --port 27019正式生产环境通常启动3个config server。 启动3个是为了作热备。
step 2 启动mongos spa
mongos --configdb cfg0.example.net:27019,cfg1.example.net:27019,cfg2.example.net:27019step3 启动分片mongod
分片就是普通的mongod .net
mongod --dbpath <path> --port <port>
用mongo 链接上mongos, 而后经过Mongo命令行输入:
添加非replica set做为分片:
sh.addShard( "mongodb0.example.net:27017" )
添加replica set做为分片:
sh.addShard( "rs1/mongodb0.example.net:27017" )step5 对某个数据库启用分片
sh.enableSharding("<database>")这里只是标识这个数据库能够启用分片,但实际上并无进行分片。
step6 对collection进行分片
分片时须要指定分片的key, 语法为
sh.shardCollection("<database>.<collection>", shard-key-pattern)例子为:
sh.shardCollection("records.people", { "zipcode": 1, "name": 1 } )
sh.shardCollection("people.addresses", { "state": 1, "_id": 1 } )
sh.shardCollection("assets.chairs", { "type": 1, "_id": 1 } )
db.alerts.ensureIndex( { _id : "hashed" } ) sh.shardCollection("events.alerts", { "_id": "hashed" } )
1 shard key须要有高的cardinality 。 也就是shard key须要拥有不少不一样的值。 便于数据的切分和迁移。
2 尽可能与应用程序融合。让mongos面对查询时能够直接定位到某个shard。
3 具备随机性。这是为了避免会让某段时间内的insert请求所有集中到某个单独的分片上,形成单片的写速度成为整个集群的瓶颈。用objectId做为shard key时会发生随机性差状况。 ObjectId实际上由进程ID+TIMESTAMP + 其余因素组成, 因此一段时间内的timestamp会相对集中。
不过随机性高会有一个反作用,就是query isolation性比较差。
可用hash key增长随机性。
登上mongos
sh.status()或者须要看详细一点
sh.status({verbose:true})
Sharding Status --- sharding version: { "_id" : 1, "version" : 3 } shards: { "_id" : "shard0000", "host" : "m0.example.net:30001" } { "_id" : "shard0001", "host" : "m3.example2.net:50000" } databases: { "_id" : "admin", "partitioned" : false, "primary" : "config" } { "_id" : "contacts", "partitioned" : true, "primary" : "shard0000" } foo.contacts shard key: { "zip" : 1 } chunks: shard0001 2 shard0002 3 shard0000 2 { "zip" : { "$minKey" : 1 } } -->> { "zip" : 56000 } on : shard0001 { "t" : 2, "i" : 0 } { "zip" : 56000 } -->> { "zip" : 56800 } on : shard0002 { "t" : 3, "i" : 4 } { "zip" : 56800 } -->> { "zip" : 57088 } on : shard0002 { "t" : 4, "i" : 2 } { "zip" : 57088 } -->> { "zip" : 57500 } on : shard0002 { "t" : 4, "i" : 3 } { "zip" : 57500 } -->> { "zip" : 58140 } on : shard0001 { "t" : 4, "i" : 0 } { "zip" : 58140 } -->> { "zip" : 59000 } on : shard0000 { "t" : 4, "i" : 1 } { "zip" : 59000 } -->> { "zip" : { "$maxKey" : 1 } } on : shard0000 { "t" : 3, "i" : 3 } { "_id" : "test", "partitioned" : false, "primary" : "shard0000" }
Step1 disable balance process. 链接上Mongos
sh.setBalancerState(false)Step2 关闭config server
Step3 备份数据文件夹
Step4 重启config server
Step5 enable balance process.
sh.setBalancerState(false)
能够经过下面的命令来查看当前的balance进程状态。先链接到任意一台mongos
use config db.locks.find( { _id : "balancer" } ).pretty() { "_id" : "balancer", "process" : "mongos0.example.net:1292810611:1804289383", "state" : 2, "ts" : ObjectId("4d0f872630c42d1978be8a2e"), "when" : "Mon Dec 20 2010 11:41:10 GMT-0500 (EST)", "who" : "mongos0.example.net:1292810611:1804289383:Balancer:846930886", "why" : "doing balance round" }
能够经过balance时间窗口指定在一天以内的某段时间以内能够进行balance, 其余时间不得进行balance。
先链接到任意一台mongos
use config db.settings.update({ _id : "balancer" }, { $set : { activeWindow : { start : "23:00", stop : "6:00" } } }, true )
也能够取消时间窗口设置:
use config db.settings.update({ _id : "balancer" }, { $unset : { activeWindow : true } })
这是一个全局的参数。 默认是64MB。
小的chunk会让不一样的shard数据量更均衡。 但会致使更多的Migration。
大的chunk会减小migration。不一样的shard数据量不均衡。
这样修改chunk size。先链接上任意mongos
db.settings.save( { _id:"chunksize", value: <size> } )
单位是MB
每一个mongos进程均可能发动balance。
一次只会有一个balance跑。 这是由于须要竞争这个锁:
db.locks.find( { _id : "balancer" } )
balance一次只会迁移一个chunk。
只有chunk最多的shard的chunk数目减去chunk最少的shard的chunk数目超过treshhold时才开始migration。
Number of Chunks |
Migration Threshold |
Fewer than 20 |
2 |
21-80 |
4 |
Greater than 80 |
8 |
一旦balancer开始行动起来,只有当任意两个shard的chunk数量小于2或者是migration失败才会中止。
有两种方式,第一种在添加分片时候用maxSize参数指定:
db.runCommand( { addshard : "example.net:34008", maxSize : 125 } )第二种方式能够在运行中修改设定:
use config db.shards.update( { _id : "shard0000" }, { $set : { maxSize : 250 } } )
链接上任意一台mongos
STEP1 确认balancer已经打开。
STEP2 运行命令:
db.runCommand( { removeShard: "mongodb0" } )mongodb0是须要删除的分片的名字。这时balancer进程会开始把要删除掉的分片上的数据往别的分片上迁移。
STEP3 查看是否删除完
仍是运行上面那条removeShard命令
若是还未删除完数据则返回:
{ msg: "draining ongoing" , state: "ongoing" , remaining: { chunks: NumberLong(42), dbs : NumberLong(1) }, ok: 1 }STEP4 删除unsharded data
有一些分片上保存上一些unsharded data, 须要迁移到其余分片上:
能够用sh.status()查看分片上是否有unsharded data。
若是有则显示:
{ "_id" : "products", "partitioned" : true, "primary" : "mongodb0" }用下面的命令迁移:
db.runCommand( { movePrimary: "products", to: "mongodb1" })只有所有迁移完上面的命令才会返回:
{ "primary" : "mongodb1", "ok" : 1 }STEP5 最后运行命令
db.runCommand( { removeShard: "mongodb0" } )
通常状况下你不须要这么作,只有当一些特殊状况发生时,好比:
1 预分配空的集合时
2 在balancing时间窗以外
手动迁移的方法:
chunks: shard0000 2 shard0001 2 { "zipcode" : { "$minKey" : 1 } } -->> { "zipcode" : 10001 } on : shard0000 Timestamp(6, 0) { "zipcode" : 10001 } -->> { "zipcode" : 23772 } on : shard0001 Timestamp(6, 1) { "zipcode" : 23772 } -->> { "zipcode" : 588377 } on : shard0001 Timestamp(3, 2) { "zipcode" : 588377 } -->> { "zipcode" : { "$maxKey" : 1 } } on : shard0000 Timestamp(5, 1) mongos> db.adminCommand({moveChunk: "contact.people", find:{zipcode:10003}, to:"192.168.1.135:20002"}) { "millis" : 2207, "ok" : 1 } mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "version" : 3, "minCompatibleVersion" : 3, "currentVersion" : 4, "clusterId" : ObjectId("52ece49ae6ab22400d937891") } shards: { "_id" : "shard0000", "host" : "192.168.1.135:20002" } { "_id" : "shard0001", "host" : "192.168.1.135:20003" } databases: { "_id" : "admin", "partitioned" : false, "primary" : "config" } { "_id" : "test", "partitioned" : false, "primary" : "shard0000" } { "_id" : "contact", "partitioned" : true, "primary" : "shard0000" } contact.people shard key: { "zipcode" : 1 } chunks: shard0000 3 shard0001 1 { "zipcode" : { "$minKey" : 1 } } -->> { "zipcode" : 10001 } on : shard0000 Timestamp(6, 0) { "zipcode" : 10001 } -->> { "zipcode" : 23772 } on : shard0000 Timestamp(7, 0) { "zipcode" : 23772 } -->> { "zipcode" : 588377 } on : shard0001 Timestamp(7, 1) { "zipcode" : 588377 } -->> { "zipcode" : { "$maxKey" : 1 } } on : shard0000 Timestamp(5, 1) mongos>
这是一种提升写效率的方法。至关于在写入真实数据以前,就分配好了数据桶,而后再对号入座。省去了建立chunk和split的时间。
实际上使用的是split命令:
db.runCommand( { split : "myapp.users" , middle : { email : prefix } } );myapp.users 是 collection的名字。
middle参数是split的点。
split命令以下:
db.adminCommand( { split: <database>.<collection>, <find|middle|bounds> } )find 表示查找到的记录进行分裂
bounds是指定[low, up]分裂
middle是指定分裂的点。
一个预分配chunk的例子以下:
for ( var x=97; x<97+26; x++ ){ for( var y=97; y<97+26; y+=6 ) { var prefix = String.fromCharCode(x) + String.fromCharCode(y); db.runCommand( { split : "myapp.users" , middle : { email : prefix } } ); } }
这个预分配的目的是字母顺序有必定间隔的email, 分配到不一样的chunk里。
例如aa-ag到一个chunk
ag-am到一个chunk
预分配的结果以下:{ "email" : { "$minKey" : 1 } } -->> { "email" : "aa" } on : shard0001 Timestamp(2, 0) { "email" : "aa" } -->> { "email" : "ag" } on : shard0001 Timestamp(3, 0) { "email" : "ag" } -->> { "email" : "am" } on : shard0001 Timestamp(4, 0) { "email" : "am" } -->> { "email" : "as" } on : shard0001 Timestamp(5, 0) { "email" : "as" } -->> { "email" : "ay" } on : shard0001 Timestamp(6, 0) ...
{ "email" : "zm" } -->> { "email" : "zs" } on : shard0000 Timestamp(1, 257) { "email" : "zs" } -->> { "email" : "zy" } on : shard0000 Timestamp(1, 259) { "email" : "zy" } -->> { "email" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 260)
假设sharding的分片是复制集,须要删除某个复制集的某个成员。
只要在复制集的设置中删除该成员便可,不须要在mongos中删除。mongos会自动同步这个配置。
例如 sharding cluster中有这个分片:
{ "_id" : "rs3", "host" : "rs3/192.168.1.5:30003,192.168.1.6:30003" }
须要删除192.168.1.6:30003这个成员。
只须要:
step 1: 在192.168.1.6:30003上运行db.shutdownServer()关闭mongod
step 2:在rs3的primary的成员192.168.1.5:30003上执行
rs.remove("192.168.1.6:30003")
关闭
sh.setBalancerState(false)打开
sh.setBalancerState(true)查看是否打开:
sh.getBalancerState()
migrate commit waiting for 2 slaves for
则须要重启该分片的mongod进程。
特别须要注意的是,若是某mongod进程是一个replica set的primary, 而且该replica set上只有一个mongod, 那么不能用db.shutdownServer()的方法关闭。 会报下面的错误:
no secondary is within 10 seconds of the primary,
须要用下面的命令关闭:
db.adminCommand({shutdown : 1, force : true})
日志里出现这样的错误:
secondaryThrottle on, but doc insert timed out after 60 seconds, continuing
经过1 将全部分片的secondary和arbitary删除掉,2 重启同步的分片解决。
找到这个问题的解决方法是看到mongo/s/d_migration.cpp里有这样一段代码
if ( secondaryThrottle && thisTime > 0 ) { if ( ! waitForReplication( cc().getLastOp(), 2, 60 /* seconds to wait */ ) ) { warning() << "secondaryThrottle on, but doc insert timed out after 60 seconds, continuing" << endl; } }这段代码含义是,要进行同步的chunk所在的分片的从服务的secondary的optime和主分片不一致,因此须要等待60秒钟的时间。
因此将要进行同步的chunk所在分片的复制集secondary和arbiter都删除掉,再重启新分片的mongod以后解决。
解决方法,在mongos上运行
mongos> use admin switched to db admin mongos> db.runCommand("flushRouterConfig"); { "flushed" : true, "ok" : 1 }