【转】Mongodb分片示例
?
ConfigServer ???localhost:10000,
MongosServer localhost:20000,
Shard1Server ??localhost:30001,
Shard2Server ??localhost:30002,
Shard3Server ??localhost:30003(分片完成以后再添加)
{
??_id:1,
??name:'zhangsan',
??age:21,
??birthday:randomdate
}
先看一张结构图:

从上图中可以看出Shard server、Mongos server以及Config server之间的关系,Mongos起路由的作用,配置服务器保存各个shard服务器的配置信息,客户端不会直接同shard打交道,而是连接mongos服务器,下面开始启动各个服务器:
每个配置服务器都是一个mongod实例,启动一个Mongodb实例作为配置服务器
bin/mongod –dbpath ../data/config –-port 10000
需要指定配置服务器的地址
bin/mongod –configdb localhost:20000
bin/mongod –dbpath ../data/shard1 –-port 30001
bin/mongod –dbpath ../data/shard2 –-port 30002
????????bin/mongolocalhost:2000/admin
????????然后执行addshard命令,因为是在一台电脑上,所以要指定allowLocal属性为true
????????db.runCommand({addshard:”localhost:30001”,allowLocal:true})
????????db.runCommand({addshard:”localhost:30002”,allowLocal:true})
还是在刚刚的shell里面,切换到admin数据库,执行以下命令:
db.runCommand({“enablesharding”:”ShardDemo”});
db.runCommand({“shardcollection”:”ShardDemo.users”,”key”:”birthday”})
?
到这里,分片就算完成了,使用客户端插入数据试试,这里我用java语言:

这是30001的数据

这是30002的数据

因为按照birthday分片,所以数据库将当前日期之前的数据放在了30001,大于当前时间的放在了30002
加我QQ,一起交流:240035336
?
转自?http://gjdrift.diandian.com/post/2012-10-04/40039646455