1. 启动一对副本集(增加分片参数)
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. 启动仲裁
F:\mongodb>mongod --dbpath=f:\mongodb\db --port 4444 --replSet neu/127.0.0.1:2222
3. 启动配置库
G:\mongodb>mongod --dbpath=g:\mongodb\configdb --port 30000 --configsvr
4. 启动mongos
C:\>mongos --port 20000 --configdb 127.0.0.1:30000
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>