Mongodb – changing domain name for Mongodb primary

mongodbreplication

Im trying to initialize my replica set sp that the primary node has a specific domain:port pairing. what I want is:

"vagrant-ubuntu-trusty-64:27022"

but what I get is:

"localhost:27022"

This is the command I use to start the mongo instance:

sudo mongod –port 27022 –dbpath /db/config/data –configsvr –replSet config

This is what I use to access the mongo instance:

mongo –port 27022

I tried to modify my host file to add the domain name to the default domain of 127.0.0.1, but that didn't work.

127.0.0.1 localhost
127.0.0.1 vagrant-ubuntu-trusty-64
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

How do I get the specific "vagrant-ubuntu-trusty-64:27022" pairing when I initialize the mongo instance?

Best Answer

To add replica set members with hostname, pass the host name when initializing the replica set.

rs.initiate(
    {
        _id: "config",
        version: 1,
        members: [
        {_id: 0, host: "vagrant-ubuntu-trusty-64:27022"}
        ]
    }
)

_id --> is your replica set name

Or if you want to edit the existing configuration follow the below steps

var conf = rs.conf()

conf.members[0].host="vagrant-ubuntu-trusty-64:27022"

rs.reconfig(conf)

If you have more than one member in your replica set, all members should refer a hostname. Which means all members should be localhost or all members should be hostnames