Giter VIP home page Giter VIP logo

Comments (6)

LaurentGoderre avatar LaurentGoderre commented on July 17, 2024

Connecting to the replicaset works from within the network. It seems to be an issue when connecting from outside.

from mongo.

LaurentGoderre avatar LaurentGoderre commented on July 17, 2024

There are a few ways you could get around this problem.

  1. Implement a reverse proxy using nginx or similar.
  2. Add explicit ip addresses, change the ports mapping in the container and add the hosts to your hostfile,.

from mongo.

LaurentGoderre avatar LaurentGoderre commented on July 17, 2024

The following worked for me

compose

version: "3.0"  # Update the version to "3.0" as per your requirement

volumes:
  db-mongo-keys26:
  db-mongo-data-26:
  db-mongo-data-27:
  db-mongo-data-28:

networks:
  db-cluster26:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 162.10.21.0/24

services:

  db-mongo-keys26:
    image: depop/openssl-bats
    volumes:
      - db-mongo-keys26:/mongo-conf
    command: 'bash -c "openssl rand -base64 741 > /mongo-conf/mongodb-keyfile; chmod 600 /mongo-conf/mongodb-keyfile; chown 999 /mongo-conf/mongodb-keyfile"'

  # Node1
  db-mongo-node17:
    container_name: db-mongo-node17
    image: mongo:5.0.24  # Update the MongoDB version to "5.0.24"
    volumes:
      - db-mongo-keys26:/opt/keyfile
      - db-mongo-data-26:/data/db
    env_file:
      ./mongod.env
    ports:
      - 27017:27017  # Change the host port to 27017
    command: 'mongod --auth --keyFile /opt/keyfile/mongodb-keyfile --replSet db-ppd'
    depends_on:
        - db-mongo-keys26
 
  # Node2
  db-mongo-node18:
    container_name: db-mongo-node18
    image: mongo:5.0.24
    volumes:
      - db-mongo-keys26:/opt/keyfile
      - db-mongo-data-27:/data/db
    env_file:
      ./mongod.env
    ports:
      - 27018:27018  # Change the host port to 27018
    command: 'mongod --port 27018 --auth --keyFile /opt/keyfile/mongodb-keyfile --replSet db-ppd'
    depends_on:
        - db-mongo-keys26
 
  # Node3
  db-mongo-node19:
    container_name: db-mongo-node19
    image: mongo:5.0.24
    volumes:
      - db-mongo-keys26:/opt/keyfile
      - db-mongo-data-28:/data/db
    env_file:
      ./mongod.env
    ports:
      - 27019:27019  # Change the host port to 27019
    command: 'mongod --port 27019 --auth --keyFile /opt/keyfile/mongodb-keyfile --replSet db-ppd'
    depends_on:
        - db-mongo-keys26

Command:

rs.initiate({
_id: "db-ppd",
   members: [
     {_id: 0, host: "db-mongo-node17:27017"},
     {_id: 1, host: "db-mongo-node18:27018"},
     {_id: 2, host: "db-mongo-node19:27019"}
   ]
 })

Host file on host

127.0.0.1       db-mongo-node17
127.0.0.1       db-mongo-node18
127.0.0.1       db-mongo-node19

Connection string:

mongodb://root:example@db-mongo-node17:27017,db-mongo-node18:27018,db-mongo-node19:27019/?replicaSet=db-ppd

from mongo.

gulskr avatar gulskr commented on July 17, 2024

@LaurentGoderre are you working on your local system or some clous because the same thing i am trying on my aws ec2, it is giving error WARNING: Some networks were defined but are not used by any service: db-cluster26
Creating network "ec2-user_default" with the default driver
Creating volume "ec2-user_db-mongo-keys26" with default driver
Creating volume "ec2-user_db-mongo-data-26" with default driver
Creating volume "ec2-user_db-mongo-data-27" with default driver
Creating volume "ec2-user_db-mongo-data-28" with default driver
Creating ec2-user_db-mongo-keys26_1 ... done
Creating db-mongo-node19 ...
Creating db-mongo-node17 ... error
Creating db-mongo-node18 ...

Creating db-mongo-node19 ... done
de17 (07973999b06c76bc04444a43530a0aff03a9dbc623e9ac18dd2019e7f5748f63): Error starting userland proxy: listen tcp4 0.0.0.0:27017: bind: Creating db-mongo-node18 ... done

ERROR: for db-mongo-node17 Cannot start service db-mongo-node17: driver failed programming external connectivity on endpoint db-mongo-node17 (07973999b06c76bc04444a43530a0aff03a9dbc623e9ac18dd2019e7f5748f63): Error starting userland proxy: listen tcp4 0.0.0.0:27017: bind: address already in use
ERROR: Encountered errors while bringing up the project.
[ec2-user@ip-12-1-27-125 ~]$ docker exec -it db-mongo-node18 bash
root@5252644ac2d7:/# mongo
MongoDB shell version v5.0.24
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:372:17
@(connect):2:6
exception: connect failed

from mongo.

gulskr avatar gulskr commented on July 17, 2024

If you have any reference to install it with full setup with docker-compose or something, It will be very helpful on Amazon linux2, 5.0.24

from mongo.

LaurentGoderre avatar LaurentGoderre commented on July 17, 2024

This was on a local setup. Maybe you can use all non-default ports to avoid port in use error (27018, 27019, 27020)

from mongo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.