{
? ? ? ? "info" : "Config now saved locally. ?Should come online in about a minute.",
? ? ? ? "ok" : 1
}
八、添加分片
在三个节点上任一个节点都可以操作
192.168.1.30
[root@orcl sd3]# mongo --port 27017
MongoDB shell version: 2.6.4
connecting to: 127.0.0.1:27017/test
mongos> use admin
switched to db admin
mongos> db.runCommand({addshard:"set1/192.168.1.30:27018,192.168.1.52:27018,192.168.1.108:27018"})
{ "shardAdded" : "set1", "ok" : 1 }
mongos> db.runCommand({addshard:"set2/192.168.1.30:27019,192.168.1.52:27019,192.168.1.108:27019"})
{ "shardAdded" : "set2", "ok" : 1 }
mongos> db.runCommand({addshard:"set3/192.168.1.30:27020,192.168.1.52:27020,192.168.1.108:27020"})
{ "shardAdded" : "set3", "ok" : 1 }
九、查看分片信息
mongos> db.runCommand({listshards : 1})
{
? ? ? ? "shards" : [
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? "_id" : "set1",
? ? ? ? ? ? ? ? ? ? ? ? "host" : "set1/192.168.1.30:27018,192.168.1.52:27018"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? "_id" : "set2",
? ? ? ? ? ? ? ? ? ? ? ? "host" : "set2/192.168.1.108:27019,192.168.1.52:27019"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? "_id" : "set3",
? ? ? ? ? ? ? ? ? ? ? ? "host" : "set3/192.168.1.108:27020,192.168.1.30:27020"
? ? ? ? ? ? ? ? }
? ? ? ? ],
? ? ? ? "ok" : 1
}
十、删除分片
mongos> db.runCommand({removeshard:"set3"})
{
? ? ? ? "msg" : "draining started successfully",
? ? ? ? "state" : "started",
? ? ? ? "shard" : "set3",
? ? ? ? "ok" : 1
}
十一、管理分片
mongos> use config
switched to db config
mongos> db.shards.find();
{ "_id" : "set1", "host" : "set1/192.168.1.30:27018,192.168.1.52:27018" }
{ "_id" : "set2", "host" : "set2/192.168.1.108:27019,192.168.1.52:27019" }
{ "_id" : "set3", "host" : "set3/192.168.1.108:27020,192.168.1.30:27020" }
十二、对要分片的库和表声明
切换到admin库
mongos> use admin ? ? ?
声明test库允许分片
mongos> db.runCommand({enablesharding:"test"})
{ "ok" : 1 }?
声明users表要分片 ?
mongos> db.runCommand({shardcollection:"test.lineqi",key:{id:"hashed"}})
{ "collectionsharded" : "test.lineqi", "ok" : 1 }
十???、测试脚本
切换到test
mongos>use test
mongos> for (var i = 1; i <= 100000; i++) db.lineqi.save({id:i,name:"12345678",sex:"male",age:27,value:"test"});?
WriteResult({ "nInserted" : 1 })
十四、测试结果
查看分片信息
mongos> use config
switched to db config
mongos> db.chunks.find();
{ "_id" : "test.users-id_MinKey", "lastmod" : Timestamp(2, 0), "lastmodEpoch" : ObjectId("55ddb3a70f613da70e8ce303"), "ns" : "test.users", "min" : { "id" : { "$minKey" : 1 } }, "max" : { "id" : 1 }, "shard" : "set1" }
{ "_id" : "test.users-id_1.0", "lastmod" : Timestamp(3, 1), "lastmodEpoch" : ObjectId("55ddb3a70f613da70e8ce303"), "ns" : "test.users", "min" : { "id" : 1 }, "max" : { "id" : 4752 }, "shard" : "set2" }
{ "_id" : "test.users-id_4752.0", "lastmod" : Timestamp(3, 0), "lastmodEpoch" : ObjectId("55ddb3a70f613da70e8ce303"), "ns" : "test.users", "min" : { "id" : 4752 }, "max" : { "id" : { "$maxKey" : 1 } }, "shard" : "set3" }
{ "_id" : "test.lineqi-id_MinKey", "lastmod" : Timestamp(3, 2), "lastmodEpoch" : ObjectId("55ddb7460f613da70e8ce380"), "ns" : "test.lineqi", "min" : { "id" : { "$minKey" : 1 } }, "max" : { "id" : NumberLong("-6148914691236517204") }, "shard" : "set2" }
{ "_id" : "test.lineqi-id_-3074457345618258602", "lastmod" : Timestamp(3, 4), "lastmodEpoch" : ObjectId("55ddb7460f613da70e8ce380"), "ns" : "test.lineqi", "min" : { "id" : NumberLong("-3074457345618258602") }, "max" : { "id" : NumberLong(0) }, "shard" : "set3" }
{ "_id" : "test.lineqi-id_3074457345618258602", "lastmod" : Timestamp(3, 6), "lastmodEpoch" : ObjectId("55ddb7460f613da70e8ce380"), "ns" : "test.lineqi", "min" : { "id" : NumberLong("3074457345618258602") }, "max" : { "id" : NumberLong("614891469123