Mongodb集群配置(sharding with replica set)
转自:http://blog.csdn.net/zhangzhaokun/article/details/6269514
前言
最近在研习MongoDB集群,找到一个不错的例子,加了几句,按照自己的理解画了一个图,做个笔记。
该实验是在一台Windows机器下搭建了一个 Replica Sets + Sharding 测试集群环境,以此作为后续对于Mongodb更进一步调研的实验平台。
正文
只有一台windows机器,配置方案:
1、3个分片sharding 2、每一个分片由3个节点构成1主2备的Replica Sets 3、3个配置节点Configsever 4、1个路由节点Mongos 分片复制集A(三个分片节点构成一个复制集): 127.0.0.1:10000 127.0.0.1:10001 127.0.0.1:10002 分片复制集B(三个分片节点构成一个复制集): 127.0.0.1:20000 127.0.0.1:20001 127.0.0.1:20002 分片复制集C(三个分片节点构成一个复制集): 127.0.0.1:30000 127.0.0.1:30001 127.0.0.1:30002 Configsvc(三个配置服务器节点): 127.0.0.1:40000 127.0.0.1:40001 127.0.0.1:4002 mongos(一个路由节点): 127.0.0.1:50000
详细操作步骤:
(1)下载mongodb-window 安装文件,将bin文件整个放置在D:/MongoDB目录下。
(2)创建数据和日志文件目录
进入D:/MongoDB目录下创建如下目录:
创建数据文件目录: data/a/r0 data/a/r1 data/a/r2 data/b/r0 data/b/r1 data/b/r2 data/c/r0 data/c/r1 data/c/r2 data/configsvr/r0 data/configsvr/r1 data/configsvr/r2 创建日志文件目录: logs/a logs/b logs/c logs/configsvr
(3)创建分片和复制集
第一组:
mongod --logpath D:/MongoDB/logs/a/r0.log --logappend --dbpath D:/MongoDB/data/a/r0 --port 10000 --shardsvr --replSet setA --rest --oplogSize 64 mongod --logpath D:/MongoDB/logs/a/r1.log --logappend --dbpath D:/MongoDB/data/a/r1 --port 10001 --shardsvr --replSet setA --rest --oplogSize 64 mongod --logpath D:/MongoDB/logs/a/r2.log --logappend --dbpath D:/MongoDB/data/a/r2 --port 10002 --shardsvr --replSet setA --rest --oplogSize 64
启动上述分片节点之后,再使用mongo的命令行来初始化复制集:
D:/MongoDB/bin>call mongo 127.0.0.1:10000/admin MongoDB shell version: 1.8.0 connecting to: 127.0.0.1:10000/admin > config={ _id:'setA', members:[{_id: 0,host:'127.0.0.1:10000'}, {_id:1,host:'127.0.0.1:10001'}, {_id:2,host:'127.0.0.1:10002'}] } { "_id" : "setA", "members" : [ { "_id" : 0, "host" : "127.0.0.1:10000" },{ "_id" : 1, "host" : "127.0.0.1:10001" },{ "_id" : 2, "host" : "127.0.0.1:10002" }] } setA> rs.initiate(config);
第二组:
mongod --logpath D:/MongoDB/logs/b/r0.log --logappend --dbpath D:/MongoDB/data/b/r0 --port 20000 --shardsvr --replSet setB --rest --oplogSize 64 mongod --logpath D:/MongoDB/logs/b/r1.log --logappend --dbpath D:/MongoDB/data/b/r1 --port 20001 --shardsvr --replSet setB --rest --oplogSize 64 mongod --logpath D:/MongoDB/logs/b/r2.log --logappend --dbpath D:/MongoDB/data/b/r2 --port 20002 --shardsvr --replSet setB --rest --oplogSize 64
启动上述分片节点之后,再使用mongo的命令行来初始化复制集
D:/MongoDB/bin>call mongo 127.0.0.1:20000/admin MongoDB shell version: 1.8.0 connecting to: 127.0.0.1:20000/admin > config={ _id: 'setB', members:[ {_id:0,host:'127.0.0.1:20000'}, {_id:1,host:'127.0.0.1:20001'}, {_id:2,host:'127.0.0.1:20002'} ]} { "_id" : "setB", "members" : [ { "_id" : 0, "host" : "127.0.0.1:20000" },{ "_id" : 1, "host" : "127.0.0.1:20001" },{ "_id" : 2, "host" : "127.0.0.1:20002" }] } > rs.initiate(config);
第三组:
从命令行进入D:/MongoDB/bin目录,分别执行如下命令:
mongod --logpath D:/MongoDB/logs/c/r0.log --logappend --dbpath D:/MongoDB/data/c/r0 --port 30000 --shardsvr --replSet setC --rest --oplogSize 64 mongod --logpath D:/MongoDB/logs/c/r1.log --logappend --dbpath D:/MongoDB/data/c/r1 --port 30001 --shardsvr --replSet setC --rest --oplogSize 64 mongod --logpath D:/MongoDB/logs/c/r2.log --logappend --dbpath D:/MongoDB/data/c/r2 --port 30002 --shardsvr --replSet setC --rest --oplogSize 64
启动上述分片节点之后,再使用mongo的命令行来初始化复制集
D:/MongoDB/bin>call mongo 127.0.0.1:30000/admin MongoDB shell version: 1.8.0 connecting to: 127.0.0.1:30000/admin > config={ _id: 'setC', members:[ {_id: 0,host:'127.0.0.1:30000'}, {_id:1,host:'127.0.0.1:30001'}, {_id:2,host:'127.0.0.1:30002'} ]} { "_id" : "setC", "members" : [ { "_id" : 0, "host" : "127.0.0.1:30000" },{ "_id" : 1, "host" : "127.0.0.1:30001" },{ "_id" : 2, "host" : "127.0.0.1:30002" } ] } > rs.initiate(config);
(4)启动三个配置服务节点Configsvr:(从命令行分别执行如下命令,配置三个Configsvr)
cd D:/MongoDB/bin call mongod --configsvr --logpath D:/MongoDB/logs/configsvr/r0.log --logappend --dbpath D:/MongoDB/data/configsvr/r0 --port 40000 --shardsvr cd D:/MongoDB/bin call mongod --configsvr --logpath D:/MongoDB/logs/configsvr/r1.log --logappend --dbpath D:/MongoDB/data/configsvr/r1 --port 40001 --shardsvr cd D:/MongoDB/bin call mongod --configsvr --logpath D:/MongoDB/logs/configsvr/r2.log --logappend --dbpath D:/MongoDB/data/configsvr/r2 --port 40002 --shardsvr
(5)启动一个路由节点mongos
cd D:/MongoDB/bin call mongos --configdb 127.0.0.1:40000,127.0.0.1:40001,127.0.0.1:40002 --logpath D:/MongoDB/logs/mongos.log --logappend --port 50000
(6)配置分片
>cd D:/MongoDB/bin D:/MongoDB/bin>call mongo 127.0.0.1:50000 MongoDB shell version: 1.8.0 connecting to: 127.0.0.1:50000/test >use admin switched to db admin > db.runCommand({addshard:"setA/127.0.0.1:10000,127.0.0.1:10001,127.0.0.1:10002",name:"ShardSetA"}) { "shardAdded" : "ShardSetA", "ok" : 1 } > db.runCommand({addshard:"setB/127.0.0.1:20000,127.0.0.1:20001,127.0.0.1:20002",name:"ShardSetB"}) { "shardAdded" : "ShardSetB", "ok" : 1 } > db.runCommand({addshard:"setC/127.0.0.1:30000,127.0.0.1:30001,127.0.0.1:30002",name:"ShardSetC"}) { "shardAdded" : "ShardSetC", "ok" : 1 } >printShardingStatus() --- Sharding Status --- sharding version: { "_id" : 1, "version" : 3 } shards: { "_id" : "ShardSetA", "host" : "setA/127.0.0.1:10000,127.0.0.1:10001,127.0.0.1:10002" } { "_id" : "ShardSetB", "host" : "setB/127.0.0.1:20000,127.0.0.1:20001,127.0.0.1:20002" } { "_id" : "ShardSetC", "host" : "setC/127.0.0.1:30000,127.0.0.1:30001,127.0.0.1:30002" } databases: { "_id" : "admin", "partitioned" : false, "primary" : "config" }
配置到这里,集群搭建完毕!
在完成了集群的搭建工作之后,需要做的就是建立一个数据库,建立表,设置分片主键来初始化数据了:
查看分片:
db.runCommand( { listshards : 1 } )
数据库分片:
db.runCommand( { enablesharding : "test"} )
Collecton分片:
db.runCommand( { shardcollection : "test.person", key : {name : 1} } )
添加数据测试:
for (var i = 1; i <= 100000; i++){ db.person.insert({"name":"jack"+i, "age":i}); } db.printShardingStatus() (查看mongodb的数据分片情况)
读者:
撒花,恭喜你兄弟,如果没出意外的话,你已经完成了一个sample,吃枣吃枣哈........