http://www.cnblogs.com/yuechaotian/archive/2012/10/31/2747951.html html
1. 啓動一對副本集(增長分片參數)mongodb
D:\mongodb>mongod --dbpath=d:\mongodb\db --port 2222 --replSet neu/127.0.0.1:3333 --shardsvrshell
E:\mongodb>mongod --dbpath=e:\mongodb\db --port 3333 --replSet neu/127.0.0.1:2222 --shardsvr數據庫
2. 啓動仲裁測試
F:\mongodb>mongod --dbpath=f:\mongodb\db --port 4444 --replSet neu/127.0.0.1:2222 spa
3. 啓動配置庫code
G:\mongodb>mongod --dbpath=g:\mongodb\configdb --port 30000 --configsvr htm
4. 啓動mongosblog
C:\>mongos --port 20000 --configdb 127.0.0.1:30000 get
5. 鏈接到mongos,配置分片信息
C:\>mongo 127.0.0.1:20000/admin
MongoDB shell version: 1.8.3
connecting to: 127.0.0.1:20000/admin
#(1)將這一對副本集做爲一個片
> db.runCommand({addshard:"neu/127.0.0.1:2222,127.0.0.1:3333",name:"neu"})
{ "shardAdded" : "neu", "ok" : 1 }
#(2)查看分片信息
> db.runCommand({listshards:1})
{
"shards" : [
{
"_id" : "neu",
"host" : "neu/127.0.0.1:2222,127.0.0.1:3333"
}
],
"ok" : 1
}
#(3)將test數據庫設置爲分片
> db.runCommand({"enablesharding":"test"})
{ "ok" : 1 }
#(4)將test庫的表yctshard設置爲分片,片鍵爲name
> db.runCommand({"shardcollection":"test.yctshard","key":{"name":1}})
{ "collectionsharded" : "test.yctshard", "ok" : 1 }
#(5)在yctshard中插入數據,並查看分片狀況
> use test
switched to db test
> for (var i=1;i<300000; i++) db.test.yctshard.save({"name":"yct"+i,age:30+i})
#(6)查看數據庫分片信息
> 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" : 1000, "i" : 0 }
#(7)查看錶yctshard分片信息
> 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)在primary節點上能夠查詢集合yctshard中的數據,但在secondary節點上不能查詢,setSlaveOK便可
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" : "not master 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 }
has more
neu:SECONDARY>