Search code examples
mongodbdockermongodb-replica-set

mongodb replication connect without directConnection


I have a problem configuring the replication with mongo and docker, I can make the replication service work correctly, but if I try to connect with MongoCompass it keeps loading and then the error occurs:

getaddrinfo ENOTFOUND dbrepmongotr

URI: mongodb://localhost:27020/?readPreference=primary and mongodb://localhost:27020/?readPreference=secondary

If I use directConnection if it leaves, but when applying it to a database with password and everything, the directConnection skips them and connects, so we must disable directConnection

my docker-file:

FROM mongo:5.0.5

COPY mongod.conf /etc/mongod.conf
COPY keyfile.txt /etc/keyfile.txt

EXPOSE 27017

CMD ["mongod", "--config", "/etc/mongod.conf","--keyfile", "/etc/keyfile.txt"]

My mongod.conf:

# mongod.conf

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,dbRepMongoTR2
#replication:
replication:
  replSet: "myRs0"
#snmp:
security:
  keyFile: /etc/keyfile.txt

My docker-compose:

version: '3.5'
services:
  dbRepMongoTR:
    container_name: dbRepMongoTR
    image: mongorep:3.3
    ports:
      - 27020:27017
    restart: always
    # volumes:
    #   - ./databdGR:/data/db
    command: bin/sh -c "mongod --port 27017 --replSet myRs0 --bind_ip 127.0.0.1,dbRepMongoTR"
  dbRepMongoTR2:
    container_name: dbRepMongoTR2
    image: mongorep:1.3
    ports:
      - 27021:27017
    restart: always
    # volumes:
    #   - ./databdGR:/data/db
    command: bin/sh -c "mongod --port 27017 --replSet myRs0 --bind_ip 127.0.0.1,dbRepMongoTR2"

Then I go to dbRepMongoTR in mongosh and execute:

rs.initiate( { _id: "myRs0", members: [{ _id: 0, host: "dbRepMongoTR" }, { _id: 1, host: "dbRepMongoTR2" }] })
rs.status()

Solution

  • It's not possible without directCOnnection, much of it can be fixed using:

    command: '/bin/sh -c "mongod --auth --dbpath /data/db --storageEngine wiredTiger --keyFile /etc/keyfile.txt --replSet myRsGr --bind_ip 127.0.0.1,dbCodesaMongoGR"'