11

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
}
11
  • "run rs.initiate(...) if not yet done for the set". Did you do that?
    – mnemosyn
    May 18, 2015 at 7:18
  • Yes i put the command rs.initiate() in master machine.
    – Elango
    May 18, 2015 at 7:20
  • I have one doubt "rs.initiate()" command is run on master machine or other two slave machine
    – Elango
    May 18, 2015 at 7:22
  • Yes, you have to start with rs.initiate(), then call rs.add(). Make sure you put the ip addresses in quotation marks, i.e. rs.add("192.168.1.2")
    – mnemosyn
    May 18, 2015 at 7:26
  • Yes, i added two member that is added success. and i see the rs.status() throw the master machine status is "primary" and other two slave machine status is "Startup". and i insert the data in master machine but not replicate slave machine.
    – Elango
    May 18, 2015 at 7:31

1 Answer 1

19

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.

9
  • I am reeving the following error > db.test.find() Error: error: { "$err" : "not master and slaveOk=false", "code" : 13435 } > rs.slaveOk() > db.test.find() Error: error: { "$err" : "not master or secondary; cannot currently read from this replSet member", "code" : 13436 }
    – Elango
    May 18, 2015 at 7:55
  • did you run rs.add(...) after running rs.initiate()?
    – bagrat
    May 18, 2015 at 7:56
  • Yes i run first "rs.initiate()" then run "rs.add("machine2:27017")"
    – Elango
    May 18, 2015 at 9:11
  • I have updated my answer, please follow the steps and let me know what you get.
    – bagrat
    May 18, 2015 at 9:11
  • rs1:PRIMARY> rs.initiate(conf) { "info" : "try querying local.system.replset to see current configuration", "ok" : 0, "errmsg" : "already initialized", "code" : 23 }
    – Elango
    May 18, 2015 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.