8

I have a mongodb replicaset on ubuntu.. In replica set, hosts are defined as localhost. You can see ;

{
    "_id" : "myrep",
    "version" : 4,
    "members" : [
            {
                    "_id" : 0,
                    "host" : "localhost:27017"
            },
            {
                    "_id" : 2,
                    "host" : "localhost:27018"
            },
            {
                    "_id" : 1,
                    "host" : "localhost:27019",
                    "priority" : 0
            }
    ]

}

I want to change host adresses with real ip of server. But when i run rs.reconfig, I get error :

{
    "assertion" : "hosts cannot switch between localhost and hostname",
    "assertionCode" : 13645,
    "errmsg" : "db assertion failure",
    "ok" : 0

}

How can i solve it ? Thank you.

2 Answers 2

24

There is a cleaner way to do this:

use local
cfg = db.system.replset.findOne({_id:"replicaSetName"})
cfg.members[0].host="newHost:27017"
db.system.replset.update({_id:"replicaSetName"},cfg)

then restart mongo

Sign up to request clarification or add additional context in comments.

4 Comments

This works - you MUST restart mongod afterwards, to ensure the new host setting is picked up. When successful, "rs.status()" will return the correct host name for each member.
This gets around the error changing the hosts, but I found I also had to execute cfg = rs.conf(); rs.reconfig(cfg); after the restart to make this stick.
I would like to add that newHost, if you re trying to deploy in the same machine, has to be the local network address of it, i.e. something like 192.168... or 10.0...
This was absolutely horrific. Just wanted to debug transactions locally. After about 6 hours of misery I find I had to start mongod with the --bind 0.0.0.0 parameter and then use your solution and set the replica set's host to IP to 192.168.1. . . Is there a way to automate this setup without having to manually set the replica set's host like this?
1

The only way I found to change host names is recreating replica set.. To make it right db directories need to be cleaned.. Then starting all servers with replication mode after that creating new repset with new host names fixed it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.