'MongoDB replica set, error when connecting from mongoose node.js

I am running mongodb replicaSet inside docker containers in Windows. This is docker compose file

version: '3'

services:

  rs0:
    image: mongo:4.4
    ports:
      - "27018:27017"
    command: mongod --replSet rsnmbp
    volumes:
      - rs0_data:/data/db
      - ./nmbprsdata0:/nmbpdata

  rs1:
    image: mongo:4.4
    ports:
      - "27019:27017"
    command: mongod --replSet rsnmbp
    volumes:
      - rs1_data:/data/db
      - ./nmbprsdata1:/nmbpdata

  rs2:
    image: mongo:4.4
    ports:
      - "27020:27017"
    command: mongod --replSet rsnmbp
    volumes:
      - rs2_data:/data/db
      - ./nmbprsdata2:/nmbpdata
      
  rs3:
    image: mongo:4.4
    ports:
      - "27021:27017"
    command: mongod --replSet rsnmbp
    volumes:
      - rs3_data:/data/db
      - ./nmbprsdata3:/nmbpdata
      
  rs4:
    image: mongo:4.4
    ports:
      - "27022:27017"
    command: mongod --replSet rsnmbp
    volumes:
      - rs4_data:/data/db
      - ./nmbprsdata4:/nmbpdata
      
   

volumes:
  rs0_data:
  rs1_data:
  rs2_data:
  rs3_data:
  rs4_data:

Replica set is configured via

rsconf = { 
  _id: "rsnmbp", 
  members: [ 
    { 
      _id: 0, 
      host: "rs0:27017" 
    }, 
    { 
      _id: 1, 
      host: "rs1:27017" 
    }, 
    { 
      _id: 2, 
      host: "rs2:27017" 
    },
    { 
      _id: 3, 
      host: "rs3:27017" 
    },
    { 
      _id: 4, 
      host: "rs4:27017" 
    },
  ] 
} 
rs.initiate(rsconf)

I am trying to connect to replica set via mongoose in node.js

const DB_URI = 'mongodb://localhost:27018,localhost:27019,localhost:27020,localhost:27021,localhost:27022/test'
mongoose.connect(DB_URI)
    .then((result) =>console.log ("connected to database"))
    .catch((err) =>console.log (err))

but I am receiving following error

MongooseServerSelectionError: getaddrinfo ENOTFOUND rs0
 ...
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(5) {
      'rs0:27017' => [ServerDescription],
      'rs1:27017' => [ServerDescription],
      'rs2:27017' => [ServerDescription],
      'rs3:27017' => [ServerDescription],
      'rs4:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'rsnmbp',
    maxSetVersion: 1,
    maxElectionId: new ObjectId("7fffffff0000000000000003"),
    commonWireVersion: 9,
    logicalSessionTimeoutMinutes: undefined
  }
}

I have added following lines to etc/hosts on Windows

127.0.0.1 rs0
127.0.0.1 rs1
127.0.0.1 rs2
127.0.0.1 rs3    
127.0.0.1 rs4

and changed const DB_URI to

const DB_URI = 'mongodb://rs0:27018,rs1:27019,rs2:27020,rs3:27021,rs4:27022/test'

but now I am receiving following error

MongooseServerSelectionError: Server selection timed out after 30000 ms
...
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(0) {},
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'rsnmbp',
    maxSetVersion: 1,
    maxElectionId: new ObjectId("7fffffff0000000000000003"),
    commonWireVersion: 9,
    logicalSessionTimeoutMinutes: undefined
  }
}

How can I connect to this replicaSet with mongoose in node.js Thanks in advance.



Solution 1:[1]

You must use one Replica set & a Replica set can have multiple members. and make sure that you have added Replica set nodes in the host machine in etc/hosts file. Just like the below example -

127.0.0.1   mongoset1 mongoset2 mongoset3

Note - 127.0.0.1 is your host machine and mongoset1, mongoset2 and mongoset3 are the nodes (members) of the replicaset.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohammad Faisal Khan