Definir replicação Mongodb (windows)

para estimulação, tratamos 3 pastas como 3 discos

F:mongo_repsetdb1

F:mongo_repsetdb2

F:mongo_repsetdb3

em seguida, abra sua ferramenta de comando no Windows,
digite abaixo:

mongod --port 1111 --dbpath F:mongo_repsetdb1datadb --logpath F:mongo_repsetdb1datalogMongoDB.log --replSet test --logappend

em seguida, inicie db2 e db3 da mesma maneira:

mongod --port 2222 --dbpath F:mongo_repsetdb2datadb --logpath F:mongo_repsetdb2datalogMongoDB.log --replSet test --logappend

mongod --port 3333--dbpath F:mongo_repsetdb3datadb --logpath F:mongo_repsetdb3datalogMongoDB.log --replSet test --logappend

** quando 3 servidores forem configurados, coloque-os juntos **

abra uma nova janela de linha de comando e digite

> config_test={"_id": "test" ,
... members:[
... {_id:0,host:"127.0.0.1:1111"},
... {_id:1,host:"127.0.0.1:2222"},
... {_id:2,host:"127.0.0.1:3333"}]
... }

output

{
"_id" : "test",
"members" : [
{
"_id" : 0,
"host" : "127.0.0.1:1111"
},
{
"_id" : 1,
"host" : "127.0.0.1:2222"
},
{
"_id" : 2,
"host" : "127.0.0.1:3333"
}
]
}

em seguida, execute cmd rs.initiate(config_test)para iniciar a replicação

output

> rs.initiate(config_test)
{ "ok" : 1 }

você também pode executar rs.status()para verificar o status da replicação

which:

test:SECONDARY> rs.status()
{
"set" : "test",
"date" : ISODate("2015-05-10T06:59:39.582Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:1111",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1108,
"optime" : Timestamp(1431241093, 1),
"optimeDate" : ISODate("2015-05-10T06:58:13Z"),
"electionTime" : Timestamp(1431241096, 1),
"electionDate" : ISODate("2015-05-10T06:58:16Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "127.0.0.1:2222",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 86,
"optime" : Timestamp(1431241093, 1),
"optimeDate" : ISODate("2015-05-10T06:58:13Z"),
"lastHeartbeat" : ISODate("2015-05-10T06:59:38.922Z"),
"lastHeartbeatRecv" : ISODate("2015-05-10T06:59:38.919Z"),
"pingMs" : 0,
"configVersion" : 1
},
{
"_id" : 2,
"name" : "127.0.0.1:3333",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 86,
"optime" : Timestamp(1431241093, 1),
"optimeDate" : ISODate("2015-05-10T06:58:13Z"),
"lastHeartbeat" : ISODate("2015-05-10T06:59:38.919Z"),
"lastHeartbeatRecv" : ISODate("2015-05-10T06:59:38.918Z"),
"pingMs" : 0,
"configVersion" : 1
}
],
"ok" : 1
}

descrição de parâmetros

saúde : 1 // 1-> normal , 0-> exceção

estado: 1 // 1-> primário, 2-> escravo, para backup

experimentar

quando pararmos o mongodb1, o que acontecerá?

então, paramos o db1 e executamos rs.status()novamente na linha de comando;

output

test:PRIMARY> rs.status();
{
"set" : "test",
"date" : ISODate("2015-05-10T07:05:53.860Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:1111",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2015-05-10T07:05:53.214Z"),
"lastHeartbeatRecv" : ISODate("2015-05-10T07:04:35.101Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "Failed attempt to connect to 127.0.0.1:1111; couldn't connect to server 127.0.0.1:1111 (127.0.0.1), connection attempt failed",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "127.0.0.1:2222",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1121,
"optime" : Timestamp(1431241093, 1),
"optimeDate" : ISODate("2015-05-10T06:58:13Z"),
"electionTime" : Timestamp(1431241478, 1),
"electionDate" : ISODate("2015-05-10T07:04:38Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "127.0.0.1:3333",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 460,
"optime" : Timestamp(1431241093, 1),
"optimeDate" : ISODate("2015-05-10T06:58:13Z"),
"lastHeartbeat" : ISODate("2015-05-10T07:05:53.136Z"),
"lastHeartbeatRecv" : ISODate("2015-05-10T07:05:53.136Z"),
"pingMs" : 0,
"configVersion" : 1
}
],
"ok" : 1
}

as we can see, one SECONDARY repliSet becomes PRIMARY