mongodb sharding基本概念(二)

2014-11-24 13:33:14 · 作者: · 浏览: 1
},
"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操作。