cvbcla
Last Updated: February 25, 2016
·
571
· pensacola1989

Set Mongodb Replication (windows)

for stimulation, We treat 3 folders as 3 disk

F:\mongo_repset\db1\

F:\mongo_repset\db2\

F:\mongo_repset\db3\

then,open your command tool on Windows,
type below:

mongod --port 1111 --dbpath F:\mongo_repset\db1\data\db --logpath F:\mongo_repset\db1\data\log\MongoDB.log --replSet test --logappend

then ,start db2 and db3 in the same way:

mongod --port 2222 --dbpath F:\mongo_repset\db2\data\db --logpath F:\mongo_repset\db2\data\log\MongoDB.log --replSet test --logappend

mongod --port 3333--dbpath F:\mongo_repset\db3\data\db --logpath F:\mongo_repset\db3\data\log\MongoDB.log --replSet test --logappend

** when 3 servers have been set up, Put them togater**

open a new command line window , and type

> 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"
            }
    ]
}

then ,run cmd rs.initiate(config_test)to start Replication

output

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

you may also run rs.status() to check the Replication's status

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
}

paramter discription

health:1 //1->normal,0->exception

state:1 // 1->primary,2->slave,for backup

experiment

when we stop mongodb1, what will happen?

so we stop db1, and run rs.status() again on the command line;

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