分别开启一个Config和两个Shard



D:\mongodb1\bin\mongod --configsvr --dbpath D:\mongodb1\db\ --port 27020



D:\mongodb2\bin\mongod --dbpath D:\mongodb2\db\ --port 27021



D:\mongodb3\bin\mongod --dbpath D:\mongodb3\db\ --port 27022



启动mongos(Sharding controller分片控制器)



D:\mongodb1\bin\mongos --configdb 192.168.0.109:27020





配置



D:\mongodb1\bin\mongo



use admin



db.runCommand( { addshard : "192.168.0.109:27021", allowLocal : 1, maxSize:2 , minKey:1, maxKey:10} )



--添加分片,maxsize单位是M,此处设置比较小的数值只为演示分片效果



--或者使用sh.addShard("192.168.0.109:27021")



db.runCommand( { addshard : "192.168.0.109:27022", allowLocal : 1, minKey:1000} )



--移除分片可以用 db.runCommand( { removeshard : "localhost:10000" } );



db.runCommand({listshards:1}); 



--查看分片节点列表



sh.status()



--查看状态 db.printShardingStatus()



sh.enableSharding("records")



--允许数据库records分片



db.runCommand( { shardcollection : "records.posts", key : {_id : 1}, unique: true } ) 



--records.posts数据集分片存储(默认一个数据集在一个分片上)



用程序向records.posts数据集中插入100000条数据



查看效果



use records



db.posts.stats()



观察结果中的shards.shard0000.count和shards.shard0001.count即可得出两个分片服务器的存储量