MongoDB replica set configuration error

mongodbreplication

I am trying to set up a MongoDB replica set on Digital Ocean.

I edited /etc/mongo.conf and specified replica set id:

replSet=rs1

and did not change anything else.

I did rs.initiate() on the first instance. Here is my config:

{
"_id" : "rs1",
"version" : 2,
"members" : [
    {
        "_id" : 1,
        "host" : "localhost:27017",
        "arbiterOnly" : false,
        "buildIndexes" : true,
        "hidden" : false,
        "priority" : 1,
        "tags" : {

        },
        "slaveDelay" : 0,
        "votes" : 1
    }
],
"settings" : {
    "chainingAllowed" : true,
    "heartbeatTimeoutSecs" : 10,
    "getLastErrorModes" : {

    },
    "getLastErrorDefaults" : {
        "w" : 1,
        "wtimeout" : 0
    }
}

}

Now, I want to add two more instances, but I see the error:

{
"ok" : 0,
"errmsg" : "Either all host names in a replica set configuration must be localhost references, or none must be; found 1 out of 2",
"code" : 103

}

How can I change the host to the server's ip address? If I try reconfig I get:

{
"ok" : 0,
"errmsg" : "No host described in new configuration 3 for replica set rs1 maps to this node",
"code" : 103

}

/etc/hostname contents:

mongodb-1

/etc/hosts contents:

# Your system has configured 'manage_etc_hosts' as True.
# As a result, if you wish for changes to this file to persist
# then you will need to either
# a.) make changes to the master file in /etc/cloud/templates/hosts.tmpl
# b.) change or remove the value of 'manage_etc_hosts' in
#     /etc/cloud/cloud.cfg or cloud-config from user-data
127.0.1.1 mongodb-1 mongodb-1
127.0.0.1 localhost

# 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

Best Answer

The problem is clearly stated: the first replica set member is bound to localhost.

Since with this name the other replica set members could not connect to this instance, you can only have other replica set members on the same host (but a different port for obvious reasons).

Chances are that the MongoDB instances at DO are bound to localhost by default. You need to bind them to a different IP address using the net.bindIp configuration option. Please keep in mind that this makes you MongoDB instance accessible to everybody being able to connect to the chosen port. You should enable authentication if a MongoDB instance is bound to a different IP than localhost.