Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb replicaset host name change error

Tags:

mongodb

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.

like image 580
Fatih Donmez Avatar asked Oct 31 '11 13:10

Fatih Donmez


People also ask

What is hostname in MongoDB?

hostname — the IP address of the MongoDB Server and the port number that the MongoDB service uses. ▪ username — the admin user name that you created to log in to the MongoDB Server. ▪ password — the admin password used to log in to the MongoDB Server.

How does MongoDB connect to replicaSet?

To connect to a replica set deployment, specify the hostname and port numbers of each instance, separated by commas, and the replica set name as the value of the replicaSet parameter in the connection string. In the following example, the hostnames are host1 , host2 , and host3 , and the port numbers are all 27017 .

How do I reinitialize a replica set in MongoDB?

To reset the configuration, make sure every node in your replica set is stopped. Then delete the "local" database for every node. Once you are confident all nodes are not running and the local database are gone, start the mongod process again (of course using the --replSet flag).


1 Answers

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

like image 189
Scott Avatar answered Oct 22 '22 05:10

Scott