长期以来我就对分布式系统原理与实践非常感兴趣,对于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-win32-i386-1.8.0
下载mongodb-win32-i386-1.8.0版本的Mongodb,放置在d:/mongodb-win32-i386-1.8.0目录下。
(2)创建数据和日志文件目录
进入d:/mongodb-win32-i386-1.8.0目录下创建如下目录
创建数据文件目录:
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)创建分片和复制集
配置第一组:
从命令行进入d:/mongodb-win32-i386-1.8.0/bin目录,分别执行如下命令!
mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/a/r0.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/a/r0 --port 10000 --shardsvr --replSet setA --rest --oplogSize 64
mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/a/r1.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/a/r1 --port 10001 --shardsvr --replSet setA --rest --oplogSize 64
mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/a/r2.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/a/r2 --port 10002 --shardsvr --replSet setA --rest --oplogSize 64
启动上述分片节点之后,再使用mongo的命令行来初始化复制集
D:/mongodb-win32-i386-1.8.0/bin>call mongo.exe 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);
配置第二组:
从命令行进入d:/mongodb-win32-i386-1.8.0/bin目录,分别执行如下命令!
mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/b/r0.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/b/r0 --port 20000 --shardsvr --replSet setB --rest --oplogSize 64
mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/b/r1.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/b/r1 --port 20001 --shardsvr --replSet setB --rest --oplogSize 64
mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/b/r2.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/b/r2 --port 20002 --shardsvr --replSet setB --rest --oplogSize 64
启动上述分片节点之后,再使用mongo的命令行来初始化复制集
D:/mongodb-win32-i386-1.8.0/bin>call mongo.exe 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-win32-i386-1.8.0/bin目录,分别执行如下命令!
mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/c/r0.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/c/r0 --port 30000 --shardsvr --replSet setC --rest --oplogSize 64
mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/c/r1.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/c/r1 --port 30001 --shardsvr --replSet setC --rest --oplogSize 64
mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/c/r2.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/c/r2 --port 30002 --shardsvr --replSet setC --rest --oplogSize 64
D:/mongodb-win32-i386-1.8.0/bin>call mongo.exe 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-win32-i386-1.8.0/bin
call mongod.exe --configsvr --logpath d:/mongodb-win32-i386-1.8.0/logs/configsvr/r0.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/configsvr/r0 --port 40000 --shardsvr
cd d:/mongodb-win32-i386-1.8.0/bin
call mongod.exe --configsvr --logpath d:/mongodb-win32-i386-1.8.0/logs/configsvr/r1.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/configsvr/r1 --port 40001 --shardsvr
cd d:/mongodb-win32-i386-1.8.0/bin
call mongod.exe --configsvr --logpath d:/mongodb-win32-i386-1.8.0/logs/configsvr/r2.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/configsvr/r2 --port 40002 --shardsvr
(5)启动一个路由节点mongos
cd d:/mongodb-win32-i386-1.8.0/bin
call mongos.exe --configdb 127.0.0.1:40000,127.0.0.1:40001,127.0.0.1:40002 --logpath d:/mongodb-win32-i386-1.8.0/logs/mongos.log --logappend --port 50000
(6)配置分片
D:/mongodb-win32-i386-1.8.0/cmd>cd d:/mongodb-win32-i386-1.8.0/bin
D:/mongodb-win32-i386-1.8.0/bin>call mongo.exe 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" }
配置到这里,集群搭建完毕了!
在完成了集群的搭建工作之后,需要做的就是建立一个数据库,建立表,设置分片主键来初始化数据了!
详细请参见:Mongodb集群JavaAPI插入数据