mongo快速搭建复制集
程序员文章站
2022-05-09 11:38:37
...
第一步:
在D:\MongoDB目录建立3个目录:conf(保存配置文件夹)、data(保存数据文件夹)、log(保存日志文件夹)
第二步:
在data文件夹下分别建rs1,rs2,rs3(分别保存primary,secondary,arbiter服务器的数据)
第三步:
在conf文件夹下建立rs1.conf,rs2.conf,rs3.conf配置文件
rs1的内容:
dbpath=D:\MongoDB\data\rs1
logpath=D:\MongoDB\log\rs1.log
journal=true
port=40000
replSet=rs0
rs2的内容:
dbpath=D:\MongoDB\data\rs2
logpath=D:\MongoDB\log\rs2.log
journal=true
port=40001
replSet=rs0
rs3的内容:
dbpath=D:\MongoDB\data\rs3
logpath=D:\MongoDB\log\rs3.log
journal=true
port=40002
replSet=rs0
第四步:
以用conf的方式启动mongod服务
下面的3条命令分别在3个命令行中执行
mongod --config D:\MongoDB\conf\rs1.conf
mongod --config D:\MongoDB\conf\rs2.conf
mongod --config D:\MongoDB\conf\rs3.conf
第五步:
登录3个服务器
下面3条命令分别在3个命令行中执行
mongo --port 40000
mongo --port 40001
mongo --port 40002
第六步:
在40000端口对应的客户端上依次执行如下命令
rs.initiate()
rs.conf()
rs.add("DESKTOP-MNG19L3:40001")
rs.addArb("DESKTOP-MNG19L3:40002")
rs.status()
运行的结果如下:
> rs.initiate()
{
"info2":"no configuration specified. Using a default configuration for the set",
"me":"DESKTOP-MNG19L3:40000",
"ok":1
}
rs0:SECONDARY> rs.conf()
{
"_id":"rs0",
"version":1,
"protocolVersion":NumberLong(1),
"members":[
{
"_id":0,
"host":"DESKTOP-MNG19L3:40000",
"arbiterOnly":false,
"buildIndexes":true,
"hidden":false,
"priority":1,
"tags":{
},
"slaveDelay":NumberLong(0),
"votes":1
}
],
"settings":{
"chainingAllowed":true,
"heartbeatIntervalMillis":2000,
"heartbeatTimeoutSecs":10,
"electionTimeoutMillis":10000,
"getLastErrorModes":{
},
"getLastErrorDefaults":{
"w":1,
"wtimeout":0
},
"replicaSetId":ObjectId("593ff0ff57e4befb6527fdac")
}
}
rs0:PRIMARY> rs.add("DESKTOP-MNG19L3:40001")
{"ok":1}
rs0:PRIMARY> rs.addArb("DESKTOP-MNG19L3:40002")
{"ok":1}
rs0:PRIMARY> rs.status()
{
"set":"rs0",
"date":ISODate("2017-06-13T14:05:34.257Z"),
"myState":1,
"term":NumberLong(1),
"heartbeatIntervalMillis":NumberLong(2000),
"members":[
{
"_id":0,
"name":"DESKTOP-MNG19L3:40000",
"health":1,
"state":1,
"stateStr":"PRIMARY",
"uptime":484,
"optime":{
"ts":Timestamp(1497362727,1),
"t":NumberLong(1)
},
"optimeDate":ISODate("2017-06-13T14:05:27Z"),
"infoMessage":"could not find member to sync from",
"electionTime":Timestamp(1497362687,2),
"electionDate":ISODate("2017-06-13T14:04:47Z"),
"configVersion":3,
"self":true
},
{
"_id":1,
"name":"DESKTOP-MNG19L3:40001",
"health":1,
"state":2,
"stateStr":"SECONDARY",
"uptime":15,
"optime":{
"ts":Timestamp(1497362727,1),
"t":NumberLong(1)
},
"optimeDate":ISODate("2017-06-13T14:05:27Z"),
"lastHeartbeat":ISODate("2017-06-13T14:05:33.513Z"),
"lastHeartbeatRecv":ISODate("2017-06-13T14:05:32.541Z"),
"pingMs":NumberLong(1),
"syncingTo":"DESKTOP-MNG19L3:40000",
"configVersion":3
},
{
"_id":2,
"name":"DESKTOP-MNG19L3:40002",
"health":1,
"state":0,
"stateStr":"STARTUP",
"uptime":3,
"lastHeartbeat":ISODate("2017-06-13T14:05:32.526Z"),
"lastHeartbeatRecv":ISODate("2017-06-13T14:05:33.649Z"),
"pingMs":NumberLong(1),
"configVersion":-2
}
],
"ok":1
}
rs0:PRIMARY>