Solution: rs.add () of mongodb reported an error can't use localhost in repl set member names
This problem often occurs in the case of multiple instances on a single virtual machine, and localhost is used in the rs.initiate () configuration.
As shown in the following configuration, start multiple mongod instances in a virtual machine and listen on different ports for mongod-- replSet rs1-- keyFile / data/k1-- fork-- port 27017-- dbpath / data/db1-- logpath / data/log/r1.log-- logappend.
Mongod-replSet rs1-keyFile / data/k2-fork-port 27018-dbpath / data/db2-logpath / data/log/r2.log-logappend
Mongod-replSet rs1-keyFile / data/k3-fork-port 27019-dbpath / data/db3-logpath / data/log/r3.log-logappend
Initialize [root@vm3 ~] # mongo with the following configuration
MongoDB shell version: 2.2.2
Connecting to: test
Rs1:PRIMARY > rs.initiate ({_ id:'rs1',members: [{_ id:0,host:'localhost:27017',priority:1}, {_ id:1,host:'localhost:27018'}, {_ id:2,host:'localhost:27019'}]})
When we want to add mongodb instances of other machines to the cluster, the problem arises, with the following error: [root@vm3 ~] # mongo
MongoDB shell version: 2.2.2
Connecting to: test
Rs1:PRIMARY > rs.add ('192.168.18.241purl 27020')
{
"errmsg": "exception: can't use localhost in repl set member names except when using it for all members"
"code": 13393
"ok": 0
} through the error prompt, we can see that localhost cannot be used in the name of a repl set member. It is reasonable for us to think about it, because when this configuration is transferred to another machine instance, that machine will look for the master in the configuration, and the configuration of master is localhost:27017 (initially determined by priority:1), which is equivalent to master in the native machine, but the local machine does not have master, so the addition failed.
Generally speaking, this problem is not easy to encounter in the production environment.
I recommend using private network ip instead of localhost, such as 192.168.xxx.xxx, and do not use 127.0.0.1. The recommended configuration is as follows: rs.initiate ({_ id:'rs1',members: [{_ id:0,host:'192.168.18.240:27017',priority:1}, {_ id:1,host:'192.168.18.240:27018'}, {_ id:2,host:'192.168.18.240:27019'}]})