Steps to setup mongo replica set I am having problems setting up a mongo replica set.  I have 2 servers and want one to be primary and one to be secondary.

On primary machine in mongod.conf:

replSet=rs1


Then in the console I type:

rs.initiate()


Which works great.  rs.config() shows:

{
  _id: "rs1",
  version: 1,
  members: [
    {
      _id: 0,
      host: "myhost1:27017"
    }
  ]
}


This is where things get messy.  What am I supposed to do on the second machine?  I have modified the mongod.conf to point to the same replica set name 'rs1'.  I start the sever and all is well.

Am I supposed to do an rs.initiate() on the second machine?  If so both instances now think that they are the 'PRIMARY'.

If I try to do an rs.add(...) on the first server to add the second server:

rs.add("myhost2:27017")


I get an error:

{
    "errmsg" : "need most members up to reconfigure, not ok : myhost2:27017",
    "code" : 13144,
    "ok" : 0
}


if I do an rs.initiate() on the second machine and then an rs.config() I get:

{
  _id: "rs1",
  version: 1,
  members: [
    {
      _id: 0,
      host: "myhost2:27017"
    }
  ]
}


Any ideas?

EDIT:

Here is my /etc/hosts file in case I messed something up there:

host1:

127.0.0.1   myhost1
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost4.localdomain6


host2:

127.0.0.1   myhost2
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost4.localdomain6
tl;dr

Your hosts were unable to resolve the hostnames you gave to actual IPs, since those were missing in the /etc/hosts file.

How to solve


The hosts files on the all the machines must resolve the IPs of the respective machines to their respective hostnames.

For example

192.168.0.1 myhost1
192.168.0.2 myhost2
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6  


Make sure all hostnames are resolved correctly by pinging each other host from each host, eg.

myhost1 $ ping myhost2

Please read the docs thoroughly and make sure you understood them.
Proceed to step 2 until you really got it.
You only have to call rs.initiate() once and only on one machine, then use rs.add() on the same machine to add replica set members.


With your setup, you need an additional node or an arbiter

Personal note

Please do not take this personal, but you lack basic system administration skills, and that's put politely.

For your own sake: Do not run MongoDB for production until you acquired basic system and MongoDB administration skills!

I'd look for RHCSA first (works for CentOS as well) and  M102, M202 and maybe even MongoDB DBA at MongoU later (without basic sysadmin skills, the latter won't help you much).