MongoDB Windows 集群配置
Posted shw0009
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB Windows 集群配置相关的知识,希望对你有一定的参考价值。
MongoDB Windows 集群配置
一、实验环境 :
Win7(X64),MongoDB 3.0.3
二、一台Win7电脑,配置方案如下:
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 3.0.3 ,存放路径:D:\\MongoDB\\mongodb3.0.3
2.创建数据和日志文件目录
创建数据文件目录:
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
创建日志文件目录:
log/a
log/b
log/c
log/configsvr
log/route
如图:
3.创建分片和复制集
3.1 配置第一组setA:
配置文件shareA_r0.config:
dbpath=../data/a/r0
logpath=../log/a/r0.log
logappend=true
replSet=setA
port=10000
shardsvr=true
journal=true
配置文件shareA_r1.config
dbpath=../data/a/r1
logpath=../log/a/r1.log
logappend=true
replSet=setA
port=10001
shardsvr=true
journal=true
配置文件shareA_r2.config
dbpath=../data/a/r2
logpath=../log/a/r2.log
logappend=true
replSet=setA
port=10002
shardsvr=true
journal=true
分别启动shareA_r0,shareA_r1,shareA_r2
D:\\MongoDB\\mongodb3.0.3\\bin>mongod -f ../config/shareA_r0.config
D:\\MongoDB\\mongodb3.0.3\\bin>mongod -f ../config/shareA_r1.config
D:\\MongoDB\\mongodb3.0.3\\bin>mongod -f ../config/shareA_r2.config
启动上述分片节点之后,再使用mongo的命令行来初始化复制集
D:\\MongoDB\\mongodb3.0.3\\bin>call mongo.exe 127.0.0.1:10000/admin
2016-10-13T10:56:09.898+0800 I CONTROL Hotfix KB2731284 or later update is not
installed, will zero-out data files
MongoDB shell version: 3.0.3
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"
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
3.2 配置第二组setB:
配置文件shareB_r0.config:
dbpath=../data/b/r0
logpath=../log/b/r0.log
logappend=true
replSet=setB
port=20000
shardsvr=true
journal=true
配置文件shareB_r1.config:
dbpath=../data/b/r1
logpath=../log/b/r1.log
logappend=true
replSet=setB
port=20001
shardsvr=true
journal=true
配置文件shareB_r2.config:
dbpath=../data/b/r2
logpath=../log/b/r2.log
logappend=true
replSet=setB
port=20002
shardsvr=true
journal=true
分别启动shareB_r0,shareB_r1,shareB_r2
D:\\MongoDB\\mongodb3.0.3\\bin>mongod -f ../config/shareB_r0.config
D:\\MongoDB\\mongodb3.0.3\\bin>mongod -f ../config/shareB_r1.config
D:\\MongoDB\\mongodb3.0.3\\bin>mongod -f ../config/shareB_r2.config
启动上述分片节点之后,再使用mongo的命令行来初始化复制集
D:\\MongoDB\\mongodb3.0.3\\bin>call mongo.exe 127.0.0.1:20000/admin
2016-10-13T11:17:41.626+0800 I CONTROL Hotfix KB2731284 or later update is not
installed, will zero-out data files
MongoDB shell version: 3.0.3
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)
{ "ok" : 1 }
3.3 配置第三组setC:
配置文件shareC_r0.config:
dbpath=../data/c/r0
logpath=../log/c/r0.log
logappend=true
replSet=setC
port=30000
shardsvr=true
journal=true
配置文件shareC_r1.config:
dbpath=../data/c/r1
logpath=../log/c/r1.log
logappend=true
replSet=setC
port=30001
shardsvr=true
journal=true
配置文件shareC_r2.config:
dbpath=../data/c/r2
logpath=../log/c/r2.log
logappend=true
replSet=setC
port=30002
shardsvr=true
journal=true
分别启动shareC_r0,shareC_r1,shareC_r2
D:\\MongoDB\\mongodb3.0.3\\bin>mongod -f ../config/shareC_r0.config
D:\\MongoDB\\mongodb3.0.3\\bin>mongod -f ../config/shareC_r1.config
D:\\MongoDB\\mongodb3.0.3\\bin>mongod -f ../config/shareC_r2.config
启动上述分片节点之后,再使用mongo的命令行来初始化复制集
D:\\MongoDB\\mongodb3.0.3\\bin>call mongo.exe 127.0.0.1:30000/admin
2016-10-13T11:17:41.626+0800 I CONTROL Hotfix KB2731284 or later update is not
installed, will zero-out data files
MongoDB shell version: 3.0.3
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)
{ "ok" : 1 }
3.4 启动三个配置服务节点Configsvr
配置文件configsvr_r0.config:
dbpath=../data/configsvr/r0
logpath=../log/configsvr/r0.log
logappend=true
port=40000
configsvr=true
journal=true
配置文件configsvr_r1.config:
dbpath=../data/configsvr/r1
logpath=../log/configsvr/r1.log
logappend=true
port=40001
configsvr=true
journal=true
配置文件configsvr_r2.config:
dbpath=../data/configsvr/r2
logpath=../log/configsvr/r2.log
logappend=true
port=40002
configsvr=true
journal=true
分别启动configsvr_r0,configsvr_r1,configsvr_r2
D:\\MongoDB\\mongodb3.0.3\\bin>mongod -f ../config/configsvr/configsvr_r0.config
D:\\MongoDB\\mongodb3.0.3\\bin>mongod -f ../config/configsvr/configsvr_r1.config
D:\\MongoDB\\mongodb3.0.3\\bin>mongod -f ../config/configsvr/configsvr_r2.config
3.5 启动路由节点
配置文件route.config:
configdb=127.0.0.1:40000,127.0.0.1:40001,127.0.0.1:40002
port=50000
chunkSize=1
logpath=../log/route/mongos.log
logappend=true
启动路由节点:
D:\\MongoDB\\mongodb3.0.3\\bin>mongos -f ../config/routeconfig/route.config
3.6 配置分片
分片命令:
db.runCommand({addshard:"setA/127.0.0.1:10000,127.0.0.1:10001,127.0.0.1
:10002",name:"ShardSetA"})
db.runCommand({addshard:"setB/127.0.0.1:20000,127.0.0.1:20001,127.0.0.1
:20002",name:"ShardSetB"})
db.runCommand({addshard:"setC/127.0.0.1:30000,127.0.0.1:30001,127.0.0.1:
30002",name:"ShardSetC"})
printShardingStatus()
D:\\MongoDB\\mongodb3.0.3\\bin>mongo 127.0.0.1:50000
2016-10-13T14:26:08.284+0800 I CONTROL Hotfix KB2731284 or later update is not
installed, will zero-out data files
MongoDB shell version: 3.0.3
connecting to: 127.0.0.1:50000/test
mongos> use admin
switched to db admin
mongos> 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 }
mongos> 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 }
mongos> 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 }
mongos> printShardingStatus()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("57ff2830daabed7a3028cf41")
}
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" : "setB/127.0.0.1:30000,127.0.0.1:30001,
127.0.0.1:30002" }
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
以上是关于MongoDB Windows 集群配置的主要内容,如果未能解决你的问题,请参考以下文章