Single Instance Mongodb Replica Set - cannot perform query/insert operations

d0c_s4vage picture d0c_s4vage · Apr 20, 2015 · Viewed 11.3k times · Source

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?

Answer

Yihe picture Yihe · Jul 10, 2018

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>