Search code examples
mongodbmongodb-replica-set

MongoDB v4.0.0 Replica Configuration failed to Setup


I'm trying to configure replication in 3 machines (OS UbuntuMATE 18.04) with MongoDB v4.0.0 installed.

I'm using Deploy a MongoDB Replica Set link for a reference. and tried to configure as follows...

Step 1: Update all three machine's host file with local-IP and hostnames i.e.

127.0.0.1   localhost
192.168.10.158  dell-Inspiron-15-7000-Gaming
192.168.10.125  dell-1080x2
192.168.10.124  vivek-pc

Then for each machine my /etc/mongod.conf is configured as follows...

mongod.conf 192.168.10.158

storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1, 192.168.10.158

# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

replication:
  replSetName: rs-test

mongod.conf 192.168.10.124

storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1, 192.168.10.124

# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo
replication:
  replSetName: rs-test

mongod.conf 192.168.10.125

storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1, 192.168.10.125

# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo
replication:
  replSetName: rs-test

Then I've restarted all three MongoDB.

Now here I want following things to know...

  1. The steps which I've followed is correct for MongoDB V4?
  2. After restart when I'm connecting to mongo by using shell command mongo. In all of three instance display as rs-test:PRIMARY>. (As per the reference there is One Primary and other two are Secondary). So what is wrong here?
  3. When I'm trying to initiate the replica set in One machine using rs.initiate(). it returns me following error message...
{
  "operationTime" : Timestamp(1544783790, 1),
  "ok" : 0,
  "errmsg" : "already initialized",
  "code" : 23,
  "codeName" : "AlreadyInitialized",
  "$clusterTime" : {
      "clusterTime" : Timestamp(1544783790, 1),
      "signature" : {
          "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
          "keyId" : NumberLong(0)
      }
  }
}

Any help would be appreciated.


Solution

  • I've Made three changes and now it's working...

    1. Clear /var/lib/mongodb directory in all three machines
    2. Update /etc/mongod.conf (I think it's not necessary) and remove 127.0.0.1 from bind Ip (Means only localIp or LiveIp in bind Ip field) and Replica set name added in double quotes i.e. "rs-test".

    Then restarted all three MongoDB instance and add following commands to 192.168.10.158 DB to make it primary.

    rs.initiate()
    rs.add("dell-1080x2")
    rs.add("vivek-pc")