Unable to connect using replicas uri (docker setup)

Hi,

I’m trying to setup mongo-replicas w/ arbiter using docker-compose. Here’s my script

click to show
version: '3.7'

services:
  mongo-primary:
    image: mongo:4.4.0
    container_name: mongo-primary
    ports:
      - 30000:27017
    networks:
      - mongo_db_network
    volumes:
      - mongo_db_primary:/data/db
      - mongo_configdb_primary:/data/configdb
    restart: unless-stopped
    depends_on:
      - mongo-secondary
      - mongo-arbiter
    command: "--bind_ip_all --replSet rs0"

  mongo-secondary:
    image: mongo:4.4.0
    container_name: mongo-secondary
    ports:
      - 30001:27017
    networks:
      - mongo_db_network
    volumes:
      - mongo_db_secondary:/data/db
      - mongo_configdb_secondary:/data/configdb
    restart: unless-stopped
    command: "--bind_ip_all --replSet rs0"

  mongo-arbiter:
    image: mongo:4.4.0
    container_name: mongo-arbiter
    ports:
      - 30002:27017
    networks:
      - mongo_db_network
    volumes:
      - mongo_db_arbiter:/data/db
      - mongo_configdb_arbiter:/data/configdb
    restart: unless-stopped
    command: "--bind_ip_all --replSet rs0"

  mongo-setup:
    image: mongo:4.4.0
    container_name: mongo-setup
    volumes:
      - ./scripts:/scripts
    networks:
      - mongo_db_network
    depends_on:
      - mongo-primary
      - mongo-secondary
      - mongo-arbiter
    entrypoint: [ "/scripts/setup.sh" ]
    restart: on-failure

  mongo-replicas-express:
    image: mongo-express:0.54.0
    container_name: mongo-replicas-express
    ports:
      - 38081:8081
    networks:
      - mongo_db_network
    depends_on:
      - mongo-primary
    environment:
      ME_CONFIG_MONGODB_SERVER: mongo-primary,mongo-secondary,mongo-arbiter
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: password
    restart: unless-stopped

volumes:
    mongo_db_primary:
    mongo_configdb_primary:
    mongo_db_secondary:
    mongo_configdb_secondary:
    mongo_db_arbiter:
    mongo_configdb_arbiter:

networks:
  mongo_db_network:
// scripts
#!/bin/bash

echo "Starting replica set initialize"
until mongo --host mongo-primary --eval "print(\"waited for connection\")"
do
    sleep 2
done
echo "Connection finished"
echo "Creating replica set"

mongo --host mongo-primary:27017 <<EOF
    var config = {
        "_id": "rs0",
        "members": [
            {
                "_id": 0,
                "host": "mongo-primary:27017",
                "priority": 2
            },
            {
                "_id": 1,
                "host": "mongo-secondary:27017",
            },
            {
                "_id": 2,
                "host": "mongo-arbiter:27017",
                "arbiterOnly": true
            }
        ]
    };
    rs.initiate(config);
    rs.slaveOk();
EOF

This is my rs.status() when I run the command in mongo-primary

click to show
rs0:PRIMARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2020-08-29T14:12:17.005Z"),
        "myState" : 1,
        "term" : NumberLong(10),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1598710328, 1),
                        "t" : NumberLong(10)
                },
                "lastCommittedWallTime" : ISODate("2020-08-29T14:12:08.975Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1598710328, 1),
                        "t" : NumberLong(10)
                },
                "readConcernMajorityWallTime" : ISODate("2020-08-29T14:12:08.975Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1598710328, 1),
                        "t" : NumberLong(10)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1598710328, 1),
                        "t" : NumberLong(10)
                },
                "lastAppliedWallTime" : ISODate("2020-08-29T14:12:08.975Z"),
                "lastDurableWallTime" : ISODate("2020-08-29T14:12:08.975Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1598710308, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "priorityTakeover",
                "lastElectionDate" : ISODate("2020-08-29T13:53:08.904Z"),
                "electionTerm" : NumberLong(10),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(1598709178, 2),
                        "t" : NumberLong(9)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1598709188, 1),
                        "t" : NumberLong(9)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 2,
                "electionTimeoutMillis" : NumberLong(10000),
                "priorPrimaryMemberId" : 1,
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-08-29T13:53:08.924Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-08-29T13:53:09.932Z")
        },
        "electionParticipantMetrics" : {
                "votedForCandidate" : true,
                "electionTerm" : NumberLong(9),
                "lastVoteDate" : ISODate("2020-08-29T13:52:58.834Z"),
                "electionCandidateMemberId" : 1,
                "voteReason" : "",
                "lastAppliedOpTimeAtElection" : {
                        "ts" : Timestamp(1598709096, 1),
                        "t" : NumberLong(8)
                },
                "maxAppliedOpTimeInSet" : {
                        "ts" : Timestamp(1598709096, 1),
                        "t" : NumberLong(8)
                },
                "priorityAtElection" : 2
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "mongo-primary:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 1171,
                        "optime" : {
                                "ts" : Timestamp(1598710328, 1),
                                "t" : NumberLong(10)
                        },
                        "optimeDate" : ISODate("2020-08-29T14:12:08Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1598709188, 2),
                        "electionDate" : ISODate("2020-08-29T13:53:08Z"),
                        "configVersion" : 1,
                        "configTerm" : 10,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "mongo-secondary:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1168,
                        "optime" : {
                                "ts" : Timestamp(1598710328, 1),
                                "t" : NumberLong(10)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1598710328, 1),
                                "t" : NumberLong(10)
                        },
                        "optimeDate" : ISODate("2020-08-29T14:12:08Z"),
                        "optimeDurableDate" : ISODate("2020-08-29T14:12:08Z"),
                        "lastHeartbeat" : ISODate("2020-08-29T14:12:15.140Z"),
                        "lastHeartbeatRecv" : ISODate("2020-08-29T14:12:16.194Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "mongo-primary:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 10
                },
                {
                        "_id" : 2,
                        "name" : "mongo-arbiter:27017",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 1168,
                        "lastHeartbeat" : ISODate("2020-08-29T14:12:15.106Z"),
                        "lastHeartbeatRecv" : ISODate("2020-08-29T14:12:15.106Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 10
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1598710328, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1598710328, 1)
}

I can access mongo-express


I can connect via mongo3t using the following directly to 30000, or 30001

image

When I connect programmatically through my app using mongoose via mongodb://localhost:30000/ttt uri, I have no issue.


The problem comes when I try to connect using mongodb://localhost:30000,localhost:30001,localhost:30002/ttt?replicaSet=rs0, it will complain this. Either connect through mongo3t or via mongoose doesn’t work

[2020-08-29 22:29:44] [error] Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! xxx@0.1.0 dev: `ts-node-dev --no-notify src/`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the xxx@0.1.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Joseph\AppData\Roaming\npm-cache\_logs\2020-08-29T14_29_44_686Z-debug.log

I’m not sure why it is complaining about the MongoDB Atlas cluster though. I’m running this locally on my machine

Thanks!

Hi,

Any help for this? Thanks!

Bumping this again for assistance. Been stuck at this for months.

If you are connecting to admin the databases then connecting to a primary only is adequate.

When connecting to a replica set the client has to be able to connect to each node as defined in the replica set configuration.

When connecting to a replicaSet the hosts listed (explicit or implicit mongodb+srv) are the seed hosts for the client. The client will then discover the hosts in the cluster from that seed node.

If you still want to connect ‘properly’ to the replica set, you can update the ports of the mongod to match the exposed ports and update your system’s hosts file.

127.0.0.1 mongo-primary
127.0.0.1 mongo-secondary
127.0.0.1 mongo-arbiter
1 Like

Hi @chris,

Thank you for your response.

I have no issue connecting to the database via mongo3t to the primary set

image

But when I tried to connect to my primary and secondary, it can’t.

both mongodb://mongo-primary:30000,mongo-secondary:30001/test?replicaSet=rs0 and mongodb://mongo-primary:30000,mongo-secondary:30001,mongo-arbiter:30002/test?replicaSet=rs0 won’t connect

image

I have no problem pinging to each individually, I have added those to my host system hosts file

image

image

image


When connecting to a replica set the client has to be able to connect to each node as defined in the replica set configuration .

When connecting to a replicaSet the hosts listed (explicit or implicit mongodb+srv) are the seed hosts for the client. The client will then discover the hosts in the cluster from that seed node.

I’m not exactly sure what you meant here.


Programmatically, I am able to connect via mongodb://mongo-primary:30000/test through mongoose as well but am not able to when I change to mongodb://mongo-primary:30000,mongo-secondary:30001,mongo-arbiter:30002/test?replicaSet=rs0. My goal here is to be able to connect via mongodb://mongo-primary:30000,mongo-secondary:30001,mongo-arbiter:30002/test?replicaSet=rs0

I’m not exactly sure which step I did wrongly, please guide me along. Appreciate it.

Thank you

Bumping this again for assistance

Having the same issue @Joseph_Gan has. It looks like one has to set up host files in order for the replica set connection string to work; however, we are trying to set up a replica set to run integration tests on CI hosts. Modifying the hosts file is unlikely a proper solution.

After some digging, I was able to fix the problem, see MongoDB docker replica set connection error "Host not found" - Stack Overflow

Thanks, I’ve tried the hosts method but it didn’t work for me as mentioned in the post