11

After installing mongodb, I ran mongod with

mongod --dbpath <pathtodb> --logpath <pathtolog> --replSet rs0

I then connected with the mongo shell and ran

rs.initiate()

I then tried to insert a document into a collection, but received an error:

> db.blah.insert({a:1})
WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } })

Looking at rs.status(), I see the status is REMOVED:

> rs.status()
{
        "state" : 10,
        "stateStr" : "REMOVED",
        "uptime" : 1041,
        "optime" : Timestamp(1429037007, 1),
        "optimeDate" : ISODate("2015-04-14T18:43:27Z"),
        "ok" : 0,
        "errmsg" : "Our replica set config is invalid or we are not a member of it",
        "code" : 93
}

I have no idea what I could have done to mess this up. This should have worked I think. How do I get past this?

d0c_s4vage
  • 3,947
  • 6
  • 23
  • 32
  • There is only one member of the replica set. I am trying to use it as a replica set so I can tail the oplog and see changes as they occur. – d0c_s4vage Apr 20 '15 at 14:42
  • Since it's just a test setup, wipe it out and start over. Something was done to mess it up but we can't tell what, not without seeing the rs.conf(), at least. – wdberkeley Apr 20 '15 at 18:40
  • Hrmm. that did seem to work. No clue what exactly caused the problem. Works now... – d0c_s4vage Apr 22 '15 at 19:18

4 Answers4

11

As above answers said, the config is not set correctly.

I tried to re-init the replica, but got the error msg:

singleNodeRepl:OTHER> rs.initiate({ _id: "rs0", members: [ { _id: 0, host : "localhost:27017" } ] } )
{
    "info" : "try querying local.system.replset to see current configuration",
    "ok" : 0,
    "errmsg" : "already initialized",
    "code" : 23,
    "codeName" : "AlreadyInitialized"
}

The solution is to reconf the mongo:

singleNodeRepl:OTHER> rsconf = rs.conf()
singleNodeRepl:OTHER> rsconf.members = [{_id: 0, host: "localhost:27017"}]
[ { "_id" : 0, "host" : "localhost:27017" } ]
singleNodeRepl:OTHER> rs.reconfig(rsconf, {force: true})
{ "ok" : 1 }
singleNodeRepl:OTHER>
singleNodeRepl:SECONDARY>
singleNodeRepl:PRIMARY>
Yihe
  • 4,094
  • 2
  • 19
  • 21
  • Thank you very much! Ran into the exact same problem. Should be good now when container is restarted/updated since using localhost. – mparkes Apr 08 '21 at 17:12
9

Problem here is that you ran rs.initiate().. As EMPTY! You didn't tell what machines belongs to that replica set.

So..

rs.initiate({
      _id: "rs0",
      version: 1,
      members: [
         { _id: 0, host : "address.to.this.machine:27017" }
      ]
   }
)
Madbreaks
  • 19,094
  • 7
  • 58
  • 72
JJussi
  • 1,540
  • 12
  • 12
2

Short Answer:

I needed to do:

rs.initiate({_id:'rs0', version: 1, members: [{_id: 0, host:'localhost:27017'}]}

rather than rs.initiate().

Long Answer:

I am almost the same as @Madbreaks and @Yihe 's comment, but I was in the different background so that I'm adding my comment here.

Background

I used docker container of mongoDB and initiated the replicaset by rs.initiate(). (The data volume is mounted to the host, but it is out-of-topic here).

What Happend

When I restart the mongoDB container, the error "MongoError: not master and slaveOk=false" happened. Yes, the error message was different from @d0c_s4vage 's, but the workaround is the same as @Yihe 's.

Root Cause

The root cause was dynamically assigned hostname as follows:

rs0:PRIMARY> rs.conf()
{
    "_id" : "rs0",
    ...
    "members" : [
        {
            ...
            "host" : "ee4ed99b555e:27017",  # <----- !!!!

Where, the host "ee4..." above comes from docker container's internal hostname; this is set by my first rs.initiate(). This would be changed when recreate container. In my case, localhost is fine because of single server and single replicaset for 'rocketchat' app evaluation purpose.

Fumisky Wells
  • 1,150
  • 10
  • 21
0

I'm also facing same issue, I tried below steps,

NOTE: If you have already cluster setup follow my steps

  • I stopped particular server (host : "address.to.this.machine:27017")
  • Remove mongod.lock file
  • create one more data directory - (deafult: /data/db, new Data directory: /data/db_rs0)

  • update the **configuration ** file -change dbpath ( "/data/db_rs0" ), - check bindIP (default: 127.0.0.0 to 0.0.0.0)

  • Check Hostname & Hosts

    hostname

    sudo vi /etc/hosts

add to hosts

         127.0.0.0 hostname

         127.0.1.1 hostname

        (add your Public/Private IP)   hostname 
  • Start the MONGODB server in

    sudo /usr/bin/mongod -f /etc/mongod.conf &

rs.initiate({ _id: "rs0", members: [ { _id: 0, host : "hostname:27017" } ] } )

rs.status()

{

....

"members": [

{

  "_id": 0,

  "name": "hostname:27017",

  "health": 1,

  "state": 1,

  "stateStr": "PRIMARY",

  "uptime": 98231,

  "optime": {

    "ts": Timestamp(1474963880, 46),

    "t": NumberLong(339)
  },

  "optimeDate": ISODate("2016-09-27T08:11:20Z"),

  "electionTime": Timestamp(1474956813, 1),

  "electionDate": ISODate("2016-09-27T06:13:33Z"),

  "configVersion": 12,

  "self": true
},

...........

]

"ok": 1

}

-------- THANK YOU --------

  • Local host address is 127.0.0.1 and not 127.0.0.0. You are talking about cluster, when you really mean replica set... ;-) – JJussi Jun 30 '17 at 05:55