Http://www.cnblogs.com/yuechaotian/archive/2012/10/31/2747951.html
mongo-Replica set Shard test
1. Start a pair of replica sets (Increase shard parameters)
D:\mongodb>mongod--dbpath=d:\mongodb\db--port 2222--replset neu/127.0.0.1:3333 --shardsvr
E:\mongodb>mongod--dbpath=e:\mongodb\db--port 3333--replset neu/127.0.0.1:2222 --shardsvr
2. Commencement of arbitration
F:\mongodb>mongod--dbpath=f:\mongodb\db--port 4444--replset neu/127.0.0.1:2222
3. Start the Configuration library
G:\mongodb>mongod--dbpath=g:\mongodb\configdb--port 30000 --configsvr
4. Start MONGOs
c:\>mongos --port 20000--configdb 127.0.0.1:30000
5. Connect to MONGOs, configure Shard Information
C:\>mongo 127.0.0.1:20000/admin
MongoDB Shell version:1.8.3
Connecting To:127.0.0.1:20000/admin
# (1) Use this pair of replica sets as a slice
> Db.runcommand ({addshard: "neu/127.0.0.1:2222,127.0.0.1:3333", Name: "Neu"})
{"shardadded": "Neu", "OK": 1}
# (2) View shard information
> Db.runcommand ({listshards:1})
{
"Shards": [
{
"_id": "Neu",
"Host": "neu/127.0.0.1:2222,127.0.0.1:3333"
}
],
"OK": 1
}
# (3) Set the test database to Shard
> Db.runcommand ({"enablesharding": "Test"})
{"OK": 1}
# (4) Set the test Library's table Yctshard to Shard, the tablet key is name
> Db.runcommand ({"Shardcollection": "test.yctshard", "key": {"name": 1}})
{"collectionsharded": "Test.yctshard", "OK": 1}
# (5) Insert data in Yctshard and view shard status
> Use test
Switched to DB test
> for (var i=1;i<300000; i++) db.test.yctshard.save({"Name": "YCT" +i,age:30+i})
# (6) View Database shard information
> Db.printshardingstatus ()
---sharding Status---
Sharding version: {"_id": 1, "Version": 3}
Shards:
{"_id": "Neu", "host": "neu/127.0.0.1:2222,127.0.0.1:3333"}
Databases
{"_id": "admin", "partitioned": false, "PRIMARY": "Config"}
{"_id": "Tage", "partitioned": false, "primary": "Neu"}
{"_id": "Test", "partitioned": true, "primary": "Neu"}
Test.yctshard chunks:
Neu 1
{"Name": {$minKey: 1}}-->> {"name": {$maxKey: 1}} on:neu {"T": +, "I": 0}
# (7) View table Yctshard Shard Information
> Use test
Switched to DB test
> Db.yctshard.stats ()
{
"sharded": true,
"NS": "Test.yctshard",
"Count": 299999,
"Size": 16795968,
"Avgobjsize": 55.986746622488745,
"Storagesize": 33327616,
"Nindexes": 2,
"Nchunks": 1,
"Shards": {
"Neu": {
"NS": "Test.yctshard",
"Count": 299999,
"Size": 16795968,
"Avgobjsize": 55.986746622488745,
"Storagesize": 33327616,
"Numextents": 8,
"Nindexes": 2,
"Lastextentsize": 12079360,
"Paddingfactor": 1,
"Flags": 1,
"Totalindexsize": 35160064,
"Indexsizes": {
"_id_": 12492800,
"Name_1": 22667264
},
"OK": 1
}
},
"OK": 1
}
>
# (8) The data in the collection Yctshard can be queried on the primary node, but cannot be queried on secondary nodes, Setslaveok can
C:\>mongo 127.0.0.1:2222/admin
MongoDB Shell version:1.8.3
Connecting To:127.0.0.1:2222/admin
Neu:secondary> Db.yctshard.find ()
Error: {"$err": "NotMaster and Slaveok=false", "Code": 13435}
Neu:secondary> Db.getmongo (). Setslaveok ()
Not Master and Slaveok=false
Neu:secondary> Db.yctshard.find ()
neu:secondary> Use test
Switched to DB test
Neu:secondary> Db.yctshard.find ()
{"_id": ObjectId ("5090bed6605f7a295d61f4ef"), "name": "Yct1", "Age": 31}
{"_id": ObjectId ("5090bed6605f7a295d61f4f0"), "name": "Yct2", "Age": 32}
{"_id": ObjectId ("5090bed6605f7a295d61f4f1"), "name": "Yct3", "Age": 33}
...
{"_id": ObjectId ("5090bed6605f7a295d61f501"), "name": "yct19", "Age": 49}
{"_id": ObjectId ("5090bed6605f7a295d61f502"), "name": "Yct20", "Age": 50}
Have more
Neu:secondary>
mongo-Replica set Shard test