Reputation: 87
I am trying to create a master-slave mongo instances. I created to instances:
mongod.exe --config "C:\mongodb\config\repl1.cfg"
mongod.exe --config "C:\mongodb\config\repl2.cfg"
Then I logged into my first node and created replicas, second node is started as well:
mongo.exe --port 27031
mongo.exe --port 27032
rs.initiate( {
_id : "replKK",
members: [
{ _id: 0, host: "localhost:27031", priority:5 },
{ _id: 1, host: "localhost:27032", hidden: true, priority:0 }
]
})
I can read and write when both nodes are up. But when I shut down the secondary node I can insert into collection, because I'm getting following error:
WriteCommandError({
"topologyVersion" : {
"processId" : ObjectId("6149c7170fc6426b073f16c3"),
"counter" : NumberLong(11)
},
"ok" : 0,
"errmsg" : "not master",
"code" : 10107,
"codeName" : "NotWritablePrimary",
"$clusterTime" : {
"clusterTime" : Timestamp(1632225689, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1632225689, 1)
})
What I am doing wrong? How can I achieve that first node is the instance that can insert rows and secondary is just kind of backup (master-slave)? I'm using MongoDB 5.0.3
Upvotes: 1
Views: 1625
Reputation: 59456
In order to elect a writable PRIMARY node, the majority of all nodes must be up.
1 out of 2 is not the majority. Typical solution is to create an Arbiter. An Arbiter is a very lightweight mongod process, it does not store any data.
Upvotes: 1