Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB 3.0.1 Replication Setup

Tags:

mongodb

I am using MongoDB 3.0.1 version. i am try to setup for mongodb replicaiton in our machine. i have use three mongodb in septate machine for replication.

machine1 - master
machine2 - slave
machine3 - slave

I refer this url http://docs.mongodb.org/manual/tutorial/deploy-replica-set/

I have configure for every machine in mongodb.conf file

replSet = rs1
fork = true

and i added two members in master machine using this command

rs.add(192.168.1.2)
rs.add(192.168.1.3)

but i insert a document in master machine but not replicate other two slave machine. i did check slave machine throw the following error

> show dbs
2015-05-18T12:43:22.020+0530 E QUERY    Error: listDatabases failed:{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
    at Error (<anonymous>)
    at Mongo.getDBs (src/mongo/shell/mongo.js:47:15)
    at shellHelper.show (src/mongo/shell/utils.js:630:33)
    at shellHelper (src/mongo/shell/utils.js:524:36)
    at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47
> 
> rs.conf()
2015-05-18T12:43:38.692+0530 E QUERY    Error: Could not retrieve replica set config: {
    "info" : "run rs.initiate(...) if not yet done for the set",
    "ok" : 0,
    "errmsg" : "no replset config has been received",
    "code" : 94
}
    at Function.rs.conf (src/mongo/shell/utils.js:1011:11)
    at (shell):1:4 at src/mongo/shell/utils.js:1011
> 

Please help me to solve the problem. thanks & Advance.

EDIT:

rs1:PRIMARY> rs.conf()
{
    "_id" : "rs1",
    "version" : 4,
    "members" : [
        {
            "_id" : 0,
            "host" : "analyzer-xubuntu:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {

            },
            "slaveDelay" : 0,
            "votes" : 1
        },
        {
            "_id" : 1,
            "host" : "192.168.1.31:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 0.75,
            "tags" : {

            },
            "slaveDelay" : 0,
            "votes" : 1
        },
        {
            "_id" : 2,
            "host" : "192.168.1.33:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 0.5,
            "tags" : {

            },
            "slaveDelay" : 0,
            "votes" : 1
        }
    ],
    "settings" : {
        "chainingAllowed" : true,
        "heartbeatTimeoutSecs" : 10,
        "getLastErrorModes" : {

        },
        "getLastErrorDefaults" : {
            "w" : 1,
            "wtimeout" : 0
        }
    }
}

rs1:PRIMARY> rs.status()
{
    "set" : "rs1",
    "date" : ISODate("2015-05-18T09:07:31.767Z"),
    "myState" : 1,
    "members" : [
        {
            "_id" : 0,
            "name" : "analyzer-xubuntu:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 9236,
            "optime" : Timestamp(1431939509, 2),
            "optimeDate" : ISODate("2015-05-18T08:58:29Z"),
            "electionTime" : Timestamp(1431931054, 2),
            "electionDate" : ISODate("2015-05-18T06:37:34Z"),
            "configVersion" : 4,
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "192.168.1.31:27017",
            "health" : 1,
            "state" : 0,
            "stateStr" : "STARTUP",
            "uptime" : 8953,
            "optime" : Timestamp(0, 0),
            "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
            "lastHeartbeat" : ISODate("2015-05-18T09:07:29.831Z"),
            "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
            "pingMs" : 1,
            "configVersion" : -2
        },
        {
            "_id" : 2,
            "name" : "192.168.1.33:27017",
            "health" : 1,
            "state" : 0,
            "stateStr" : "STARTUP",
            "uptime" : 8946,
            "optime" : Timestamp(0, 0),
            "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
            "lastHeartbeat" : ISODate("2015-05-18T09:07:30.533Z"),
            "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
            "pingMs" : 1,
            "configVersion" : -2
        }
    ],
    "ok" : 1
}
like image 283
Elango Avatar asked May 18 '15 07:05

Elango


1 Answers

Let us go step by step.

Your MongoDB config is ok, the rest of work may be done in the shell. I will use machine names instead of IPs.

First connect to machine1, and run the following there:

> conf = {
            _id: "rs1",
            members:
                      [
                         {_id : 0, host : "machine1:27017"},
                         {_id : 1, host : "machine2:27017"},
                         {_id : 2, host : "machine3:27017"}
                      ]
         }
> rs.initiate(conf)

Then just run rs.slaveOk() on the secondaries. The secondaries will start replicating, and you will be able to query them and see your data, inserted from the primary.

like image 116
bagrat Avatar answered Oct 04 '22 14:10

bagrat