ReplicaSetNoPrimary during connection with MongoDb 4.7.0 and docker

Hi,

I have a problem since I have updated my Mongodb package to 4.7.0. I can’t connect to my local docker database for my e2e test. I’m using linux

Here my connection code:

        const uri = 'mongodb://localhost';
        let client = new MongoClient(uri);
        return client.connect()
            .then(() => {
                db = client.db(dbName);
            })
            .catch(err => {
                logger.error('MONGODB', {
                    msg: 'Failed to connect to MongoDB with NodeJs driver',
                    method: 'connect',
                    previousErr: err,
                });

                throw Error('Failed to connect to MongoDB');
            });

An error is throwed with a topology description type ‘ReplicaSetNoPrimary’

I have tested with multiple connection string :
const uri = ‘mongodb://localhost:27017’;
const uri = ‘mongodb://localhost:27017/?replicaSet=rs0’;
const uri = ‘mongodb://localhost:27017/dbName/?replicaSet=rs0’;

and try to replace localhost by 127.0.0.1

Here my initial docker file:

version: "3.4"

services:
  mongo:
    hostname: mongodb
    image: mongo:5.0.5
    volumes:
      - mongodb:/data/db
    ports:
      - 27017:27017
    healthcheck:
      test: test $(echo "rs.initiate().ok || rs.status().ok" | mongo --quiet) -eq 1
      interval: 10s
      start_period: 30s
    command: ["/usr/bin/mongod", "--replSet", "rs0", "--bind_ip_all"]

I have tested to create a real cluster with 3 mongo

services:
  mongo1:
    container_name: mongo1
    image: mongo:5.0.5
    volumes:
      - ./scripts/rs-init.sh:/scripts/rs-init.sh
    networks:
      - mongo-network
    ports:
      - 27017:27017
    depends_on:
      - mongo2
      - mongo3
    links:
      - mongo2
      - mongo3
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]

  mongo2:
    container_name: mongo2
    image: mongo:5.0.5
    networks:
      - mongo-network
    ports:
      - 27018:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
  mongo3:
    container_name: mongo3
    image: mongo:5.0.5
    networks:
      - mongo-network
    ports:
      - 27019:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]

networks:
  mongo-network:
    driver: bridge

and init the replica set with

var config = {
    "_id": "dbrs",
    "version": 1,
    "members": [
        {
            "_id": 1,
            "host": "mongo1:27017",
            // "priority": 2
        },
        {
            "_id": 2,
            "host": "mongo2:27017",
            // "priority": 1
        },
        {
            "_id": 3,
            "host": "mongo3:27017",
            // "priority": 1
            arbiterOnly: true
        }
    ]
};
rs.initiate(config, { force: true });

And change my connection string to have 3 mogo:
mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/?replicaSet=rs0

But I always have the same problem.

I have search on Google and on your forum (Local replica set on docker-compose) but nothing help me. Each time I have the same error.

Anyone can help me ?

I find a fix, using my first docker-compose file

just add in etc/hosts this line 127.0.0.1 mongodb

Here the final docker-compose:

version: "3.4"

services:
  mongo:
    hostname: mongodb
    image: mongo:5.0.9
    volumes:
      - mongodb:/data/db
    expose:
      - 27017
    ports:
      - 27017:27017
    healthcheck:
      test: test $$(echo "rs.initiate().ok || rs.status().ok" | mongo --quiet) -eq 1
      interval: 10s
      start_period: 30s
    entrypoint: ["/usr/bin/mongod", "--replSet", "rs0", "--bind_ip_all"]

Here the connection code:

    const uri = 'mongodb://127.0.0.1:27017?replicaSet=rs0';
    const options = {
        useNewUrlParser: true,
        useUnifiedTopology: true,
    };

    module.exports.connect = () => {
        if (client) {
            return Promise.resolve();
        }

        client = new MongoClient(uri, options);
        return client.connect()
            .then(() => {
                db = client.db(dbName);
            })
            .catch(err => {
                logger.error('MONGODB', {
                    msg: 'Failed to connect to MongoDB with NodeJs driver',
                    method: 'connect',
                    previousErr: err,
                });

                throw Error('Failed to connect to MongoDB');
            });
    };

But now I have some unexpected change stream error:
MongoServerSelectionError: Server selection timed out after 30000 ms

1 Like

I finally found the solution, just add ?directConnection=true on the connection string on developpment and test environment which use local database

6 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.