读书人

【转】Mongodb分片示范

发布时间: 2013-03-14 10:33:15 作者: rapoo

【转】Mongodb分片示例

Mongodb分片实现

?

1、定义

ConfigServer ???localhost:10000,

MongosServer localhost:20000,

Shard1Server ??localhost:30001,

Shard2Server ??localhost:30002,

Shard3Server ??localhost:30003(分片完成以后再添加)

2、测试数据结构

{

??_id:1,

??name:'zhangsan',

??age:21,

??birthday:randomdate

}

3、开始

先看一张结构图:

【转】Mongodb分片示范

从上图中可以看出Shard server、Mongos server以及Config server之间的关系,Mongos起路由的作用,配置服务器保存各个shard服务器的配置信息,客户端不会直接同shard打交道,而是连接mongos服务器,下面开始启动各个服务器:

先启动配置服务器

每个配置服务器都是一个mongod实例,启动一个Mongodb实例作为配置服务器

bin/mongod dbpath ../data/config -port 10000

然后启动mongos服务器

需要指定配置服务器的地址

bin/mongod configdb localhost:20000

启动两个mongod实例作为shard

bin/mongod dbpath ../data/shard1 -port 30001

bin/mongod dbpath ../data/shard2 -port 30002

都启动以后,就要开始配置了mongo连接刚才的mongos服务器的admin数据库

????????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语言:

【转】Mongodb分片示范

这是30001的数据

【转】Mongodb分片示范

这是30002的数据

【转】Mongodb分片示范

因为按照birthday分片,所以数据库将当前日期之前的数据放在了30001,大于当前时间的放在了30002

加我QQ,一起交流:240035336

?

转自?http://gjdrift.diandian.com/post/2012-10-04/40039646455

读书人网 >其他数据库

热点推荐