others-Mongodb replica set this node is not electable under the new configuration

1. The purpose of this post

Sometimes, when we initialize the replica set of mongodb, we got this error:

mongo --host 1.1.1.20 --port 27002
> rs.initiate({
...     _id:"shard",
...     members:[
...         {_id:0,host:"1.1.1.20:27002",arbiterOnly:true},
...         {_id:1,host:"1.1.1.21:27002"},
...         {_id:2,host:"1.1.1.22:27002"}
...     ]
... });
{
	"ok" : 0,
	"errmsg" : "This node, 1.1.1.20:27002, with _id 0 is not electable under the new configuration version 1 for replica set shard2",
	"code" : 93,
	"codeName" : "InvalidReplicaSetConfig"
}

2. Environments

  • Mongodb 3.x

3. How to fix this error

This error occurred because you tried to initialize the replica set in the arbiter node, you should login to other node to initialize it, or you can change your arbiter node:

3.1 Login to other node

This time we use 1.1.1.21 node to initialize the replica set:

mongo --host 1.1.1.21 --port 27002
> rs.initiate({
...     _id:"shard",
...     members:[
...         {_id:0,host:"1.1.1.20:27002",arbiterOnly:true},
...         {_id:1,host:"1.1.1.21:27002"},
...         {_id:2,host:"1.1.1.22:27002"}
...     ]
... });
{ "ok" : 1 }

3.2 Change your arbiter node

Or else, you can change your arbiter node like this:

mongo --host 1.1.1.20 --port 27002
> rs.initiate({
...     _id:"shard",
...     members:[
...         {_id:0,host:"1.1.1.20:27002"},
...         {_id:1,host:"1.1.1.21:27002",arbiterOnly:true},
...         {_id:2,host:"1.1.1.22:27002"}
...     ]
... });
{ "ok" : 1 }

4. Conclusion

Mongodb is a fast and leading no-sql database for users. If you want to initialize the replica set ,make sure not to do this job on the arbiter node.