Home > Mobile >  Unable to connect to local MongoDB docker cluster with Compass
Unable to connect to local MongoDB docker cluster with Compass

Time:06-24

I have a following MongoDB docker cluster as defined in the following docker-compose.yml:

version: "3"
services:
  mongo1:
    hostname: mongo1
    container_name: mongo1
    image: mongo:5
    volumes:
      - ${PWD}/data/db/mongo1:/data/db
    expose:
    - 27017
    ports:
      - "27011:27017"
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
  mongo2:
    hostname: mongo2
    container_name: mongo2
    image: mongo:5
    volumes:
      - ${PWD}/data/db/mongo2:/data/db
    expose:
    - 27017
    ports:
    - "27012:27017"
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
  mongo3:
    hostname: mongo3
    container_name: mongo3
    image: mongo:5
    volumes:
      - ${PWD}/data/db/mongo3:/data/db
    expose:
    - 27017
    ports:
    - "27013:27017"
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]

I start the cluster as follows:

$ mkdir data
$ docker-compose up -d

I initiate the replica set as follows:

$ docker exec -it mongo1 mongosh --eval "rs.initiate({
 _id: \"rs0\",
 members: [
   {_id: 0, host: \"mongo1\"},
   {_id: 1, host: \"mongo2\"},
   {_id: 2, host: \"mongo3\"}
 ]
})"

I can connect to it using Mongo Shell as follows:

$ mongosh "mongodb://localhost:27011/my_db"

However, specifying replicaSet in the connection string doesn't work the host mongo1 cannot be resolved.

$ mongosh "mongodb://localhost:27011/my_db?replicaSet=rs0"
Current Mongosh Log ID: 62b54fa6573be03f584017b8
Connecting to:      mongodb://localhost:27011/my_db?replicaSet=rs0&serverSelectionTimeoutMS=2000&appName=mongosh 1.5.0
MongoServerSelectionError: getaddrinfo EAI_AGAIN mongo1

Neither does mongodb://localhost:27011,localhost:27012,localhost:27013/my_db work, same error as above.

I also cannot connect to it using MongoDB Compass with the connection string mongodb://localhost:27011/my_db that works with Mongo Shell.

I suppose I can use network_mode: "host" in docker-compose config to alleviate the host discovery issue, but I would rather not do it.

Thanks!

CodePudding user response:

First of all, the format is mongodb://localhost:27011,localhost:27012,localhost:27013/?replicaSet=rs0&authSource=admin

Also please note hostnames in the connection url should match hostnames in the replica set configuration

Docs: https://www.mongodb.com/docs/manual/reference/connection-string/

Your config says "mongo1, mongo2, mongo3", your connection string says "localhost:27011,localhost:27012,localhost:27013"

If you like to keep mongoX hosts - connect to them with ssh tunnel through one of the containers.

If you prefer localhost:2701X hosts - change docker network mode to "host" and update rs config.

CodePudding user response:

try:

config = {
    "_id" : "my-mongo-set",
    "members" : [
        {
            "_id" : 0,
            "host" : "mongo1:27017"
        },
        {
            "_id" : 1,
            "host" : "mongo2:27017"
        },
        {
            "_id" : 2,
            "host" : "mongo3:27017"
        }
    ]
  }
  • Related