Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Solution: rs.add () of mongodb reported an error can't use localhost in repl set member names

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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'}]})

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report