这里先介绍sharding的架构和几个基本概念术语。
shard server :shard server可以使一个mongod实例,也可以是replica set。
config sever:为了将指定collection存储在多个shard中,那么就需要个key来进行分割,config server存储各个节点的配置信息。shard key的范围,以及分布情况。
route process:由此介入客户端,通过询问config server,确定到那个shard上面查询,在连接相应的shard操作,不保存数据和配置信息。
由于资源限制,在一台机子上做一下实验
Shard Server 1:30000
Shard Server 2:30001
Config Server :40000
Route Process:50000
步骤:
启动shard server 1和2
[mongo@172_16_3_216 mongo]$ mkdir -p /mongo/shard/data0
[mongo@172_16_3_216 mongo]$ mkdir -p /mongo/shard/data1
[mongo@172_16_3_216 mongo]$ touch shard.log
[mongo@172_16_3_216 mongo]$ mongod --shardsvr --port 30000 --dbpath /mongo/shard/data0 --fork --logpath shard.log --directoryperdb
[mongo@172_16_3_216 mongo]$ touch shard1.log
[mongo@172_16_3_216 mongo]$ mongod --shardsvr --port 30001 --dbpath /mongo/shard/data1 --fork --logpath shard1.log --directoryperdb
启动config server
[mongo@172_16_3_216 mongo]$ mkdir -p /mongo/shard/config
[mongo@172_16_3_216 mongo]$ touch config.log
[mongo@172_16_3_216 mongo]$ mongod --configsvr --port 40000 --dbpath /mongo/shard/config --fork --logpath config.log --directoryperdb
启动route process
[mongo@172_16_3_216 mongo]$ touch route.log
[mongo@172_16_3_216 mongo]$ mongos --port 50000 --configdb localhost:40000 --fork --logpath route.log --chunkSize 2
初始化sharding
mongo admin --port 50000
MongoDB shell version: 1.8.4
connecting to: 127.0.0.1:50000/admin
> db.runCommand({addshard:"localhost:30000"}) ----添加shard1
{ "shardAdded" : "shard0000", "ok" : 1 }
> db.runCommand({addshard:"localhost:30001"}) -----添加shard2
{ "shardAdded" : "shard0001", "ok" : 1 }
> db.runCommand({enablesharding:"test"}) ---对数据库test分片
{ "ok" : 1 }
> db.runCommand({shardcollection:"test.tb1",key:{_id:1}}) ---对数据库test中tb1按_id作为key
{ "collectionsharded" : "test.tb1", "ok" : 1 }
验证sharding
> for (var i=1;i<=50000;i++) db.tb1.insert({age:i,name:"hank",addr:"HangZhou"})
> db.tb1.stats()
{
"sharded" : true,
"ns" : "test.tb1",
"count" : 50000,
"size" : 3600016,
"avgObjSize" : 72.00032,
"storageSize" : 13975552,
"nindexes" : 1,
"nchunks" : 4,
"shards" : {
"shard0000" : {
"ns" : "test.tb1",
"count" : 17888,
"size" : 1287944,
"avgObjSize" : 72.00044722719142,
"storageSize" : 2793472,
"numExtents" : 5,
"nindexes" : 1,
"lastExtentSize" : 2097152,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 753664,
"indexSizes" : {
"_id_" : 753664
},
"ok" : 1
},
"shard0001" : {
"ns" : "test.tb1",
"count" : 32112,
"size" : 2312072,
"avgObjSize" : 72.00024912805182,
"storageSize" : 11182080,
"numExtents" : 6,
"nindexes" : 1,
"lastExtentSize" : 8388608,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 1343488,
"indexSizes" : {
"_id_" : 1343488
},
"ok" : 1
}
},
"ok" : 1
}
查看sharding信息:
> db.runCommand({listshards:1})
{
"shards" : [
{
"_id" : "shard0000",
"host" : "localhost:30000"
},
{
"_id" : "shard0001",
"host" : "localhost:30001"
}
],
"ok" : 1
}
新增shard server:
[mongo@172_16_3_216 mongo]$ mkdir -p /mongo/shard/data2
[mongo@172_16_3_216 mongo]$ touch shard2.log
[mongo@172_16_3_216 mongo]$ mongod --shardsvr --port 30002 --dbpath /mongo/shard/data2 --fork --logpath shard2.log --directoryperdb
> db.runCommand({ addshard:"localhost:30002" })
{ "shardAdded" : "shard0002", "ok" : 1 }
> db.runCommand({listshards:1})
{
"shards" : [
{
"_id" : "shard0000",
"host" : "localhost:30000"
},
{
"_id" : "shard0001",
"host" : "localhost:30001"
},
{
"_id" : "shard0002",
"host" : "localhost:30002"
}
],
"ok" : 1
}
如果分片的表继续有插入数据,那么数据就会分配到新加的片上,而且会根据sharding key进行数据的迁移,和重新分布。
所以建议在添加删除节点的时候,建议避开高峰期,在业务最低谷的时候操作。
删除shard server:
> use admin
switched to db admin
> db.runCommand({"removeshard" : "localhost:30002"});
{
"msg" : "draining started successfully",
"state" : "started",
"shard" : "shard0002",
"ok" : 1
}
很简单,remove就可以了,原来的数据会按照key分配到剩下的shard server上。
最后> db.printShardingStatus()可以查看sharding的信息。注意:操作都是在route process上面,不要登录到shard server操作。