長(zhǎng)期以來(lái)我就對(duì)分布式系統(tǒng)原理與實(shí)踐非常感興趣,對(duì)于Mongodb我一直很好奇,最近終于有時(shí)間能動(dòng)手實(shí)驗(yàn)一把!
我在一臺(tái)Windows機(jī)器下搭建了一個(gè) Replica Sets + Sharding 測(cè)試集群環(huán)境,以此作為我后續(xù)對(duì)于Mongodb更進(jìn)一步學(xué)習(xí)的實(shí)驗(yàn)平臺(tái)。
只有一臺(tái)windows機(jī)器,配置方案:
1、3個(gè)分片sharding
2、每一個(gè)分片由3個(gè)節(jié)點(diǎn)構(gòu)成1主2備的Replica Sets
3、3個(gè)配置節(jié)點(diǎn)Configsever
4、1個(gè)路由節(jié)點(diǎn)Mongos
分片復(fù)制集A(三個(gè)分片節(jié)點(diǎn)構(gòu)成一個(gè)復(fù)制集):
127.0.0.1:10000 127.0.0.1:10001 127.0.0.1:10002
分片復(fù)制集B(三個(gè)分片節(jié)點(diǎn)構(gòu)成一個(gè)復(fù)制集):
127.0.0.1:20000 127.0.0.1:20001 127.0.0.1:20002
分片復(fù)制集C(三個(gè)分片節(jié)點(diǎn)構(gòu)成一個(gè)復(fù)制集):
127.0.0.1:30000 127.0.0.1:30001 127.0.0.1:30002
Configsvc(三個(gè)配置服務(wù)器節(jié)點(diǎn)):
127.0.0.1:40000 127.0.0.1:40001 127.0.0.1:4002
mongos(一個(gè)路由節(jié)點(diǎn)):
127.0.0.1:50000
詳細(xì)操作步驟如下:
(1)下載mongodb-win32-i386-1.8.0
下載mongodb-win32-i386-1.8.0版本的Mongodb,放置在d:/mongodb-win32-i386-1.8.0目錄下。
(2)創(chuàng)建數(shù)據(jù)和日志文件目錄
進(jìn)入d:/mongodb-win32-i386-1.8.0目錄下創(chuàng)建如下目錄
創(chuàng)建數(shù)據(jù)文件目錄:
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
創(chuàng)建日志文件目錄:
logs/a
logs/b
logs/c
logs/configsvr
(3)創(chuàng)建分片和復(fù)制集
配置第一組:
從命令行進(jìn)入d:/mongodb-win32-i386-1.8.0/bin目錄,分別執(zhí)行如下命令!
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
啟動(dòng)上述分片節(jié)點(diǎn)之后,再使用mongo的命令行來(lái)初始化復(fù)制集
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);
配置第二組:
從命令行進(jìn)入d:/mongodb-win32-i386-1.8.0/bin目錄,分別執(zhí)行如下命令!
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
啟動(dòng)上述分片節(jié)點(diǎn)之后,再使用mongo的命令行來(lái)初始化復(fù)制集
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);
配置第三組:
從命令行進(jìn)入d:/mongodb-win32-i386-1.8.0/bin目錄,分別執(zhí)行如下命令!
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)啟動(dòng)三個(gè)配置服務(wù)節(jié)點(diǎn)Configsvr
從命令行分別執(zhí)行如下命令,配置三個(gè)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)啟動(dòng)一個(gè)路由節(jié)點(diǎn)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" }
配置到這里,集群搭建完畢了!
在完成了集群的搭建工作之后,需要做的就是建立一個(gè)數(shù)據(jù)庫(kù),建立表,設(shè)置分片主鍵來(lái)初始化數(shù)據(jù)了!