0

I've recently followed the aws quick start cloudfront deployment for a 3 node mongo replica set.

I've set up an NLB so they are accessible from my local network and am trying to connect via mongo shell.

Trying

mongo "mongodb://<username:<password>@<url1>,<url2>,<url3>"

connects me to the database but doesn't always connect to primary so must be the incorrect way of connecting

Trying

mongo "mongodb://<username:<password>@<url1>,<url2>,<url3>/?replicaSet=s0"

I get the error Error: Could not find host matching read preference { mode: "nearest" } for set s0

The /etc/mongo.conf is

net:
  port: 27017
  bindIpAll: true

systemLog:
  destination: file
  logAppend: true
  path: /log/mongod.log

storage:
  dbPath: /data
  journal:
    enabled: true

security: 
  authorization: enabled 
  keyFile: /mongo_auth/mongodb.key 

processManagement:
  fork: true
  pidFilePath: /var/run/mongodb/mongod.pid
replication:
  replSetName: s0

And the rs.conf() is

{
    "_id" : "s0",
    "version" : 1,
    "protocolVersion" : NumberLong(1),
    "writeConcernMajorityJournalDefault" : true,
    "members" : [
        {
            "_id" : 1,
            "host" : "10.0.7.79:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 10,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        },
        {
            "_id" : 2,
            "host" : "10.0.38.224:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 5,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        },
        {
            "_id" : 3,
            "host" : "10.0.80.111:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 5,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        }
    ],
    "settings" : {
        "chainingAllowed" : true,
        "heartbeatIntervalMillis" : 2000,
        "heartbeatTimeoutSecs" : 10,
        "electionTimeoutMillis" : 10000,
        "catchUpTimeoutMillis" : -1,
        "catchUpTakeoverDelayMillis" : 30000,
        "getLastErrorModes" : {
            
        },
        "getLastErrorDefaults" : {
            "w" : 1,
            "wtimeout" : 0
        },
        "replicaSetId" : ObjectId("myid")
    }
}

The link to the quickstart guide is here

Any advice on what I might be doing wrong would be appreciated

rs.status() is

{
    "set" : "s0",
    "date" : ISODate("2022-01-12T19:49:16.066Z"),
    "myState" : 1,
    "term" : NumberLong(6),
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "majorityVoteCount" : 2,
    "writeMajorityCount" : 2,
    "votingMembersCount" : 3,
    "writableVotingMembersCount" : 3,
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1642016948, 1),
            "t" : NumberLong(6)
        },
        "lastCommittedWallTime" : ISODate("2022-01-12T19:49:08.522Z"),
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1642016948, 1),
            "t" : NumberLong(6)
        },
        "readConcernMajorityWallTime" : ISODate("2022-01-12T19:49:08.522Z"),
        "appliedOpTime" : {
            "ts" : Timestamp(1642016948, 1),
            "t" : NumberLong(6)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1642016948, 1),
            "t" : NumberLong(6)
        },
        "lastAppliedWallTime" : ISODate("2022-01-12T19:49:08.522Z"),
        "lastDurableWallTime" : ISODate("2022-01-12T19:49:08.522Z")
    },
    "lastStableRecoveryTimestamp" : Timestamp(1642016908, 1),
    "electionCandidateMetrics" : {
        "lastElectionReason" : "priorityTakeover",
        "lastElectionDate" : ISODate("2022-01-12T13:46:38.130Z"),
        "electionTerm" : NumberLong(6),
        "lastCommittedOpTimeAtElection" : {
            "ts" : Timestamp(1641995191, 1),
            "t" : NumberLong(5)
        },
        "lastSeenOpTimeAtElection" : {
            "ts" : Timestamp(1641995191, 1),
            "t" : NumberLong(5)
        },
        "numVotesNeeded" : 2,
        "priorityAtElection" : 10,
        "electionTimeoutMillis" : NumberLong(10000),
        "priorPrimaryMemberId" : 2,
        "numCatchUpOps" : NumberLong(0),
        "newTermStartDate" : ISODate("2022-01-12T13:46:38.137Z"),
        "wMajorityWriteAvailabilityDate" : ISODate("2022-01-12T13:46:40.143Z")
    },
    "members" : [
        {
            "_id" : 1,
            "name" : "10.0.7.79:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 21770,
            "optime" : {
                "ts" : Timestamp(1642016948, 1),
                "t" : NumberLong(6)
            },
            "optimeDate" : ISODate("2022-01-12T19:49:08Z"),
            "lastAppliedWallTime" : ISODate("2022-01-12T19:49:08.522Z"),
            "lastDurableWallTime" : ISODate("2022-01-12T19:49:08.522Z"),
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1641995198, 1),
            "electionDate" : ISODate("2022-01-12T13:46:38Z"),
            "configVersion" : 1,
            "configTerm" : -1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 2,
            "name" : "10.0.38.224:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 21768,
            "optime" : {
                "ts" : Timestamp(1642016948, 1),
                "t" : NumberLong(6)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1642016948, 1),
                "t" : NumberLong(6)
            },
            "optimeDate" : ISODate("2022-01-12T19:49:08Z"),
            "optimeDurableDate" : ISODate("2022-01-12T19:49:08Z"),
            "lastAppliedWallTime" : ISODate("2022-01-12T19:49:08.522Z"),
            "lastDurableWallTime" : ISODate("2022-01-12T19:49:08.522Z"),
            "lastHeartbeat" : ISODate("2022-01-12T19:49:14.148Z"),
            "lastHeartbeatRecv" : ISODate("2022-01-12T19:49:14.507Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncSourceHost" : "10.0.7.79:27017",
            "syncSourceId" : 1,
            "infoMessage" : "",
            "configVersion" : 1,
            "configTerm" : -1
        },
        {
            "_id" : 3,
            "name" : "10.0.80.111:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 21768,
            "optime" : {
                "ts" : Timestamp(1642016948, 1),
                "t" : NumberLong(6)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1642016948, 1),
                "t" : NumberLong(6)
            },
            "optimeDate" : ISODate("2022-01-12T19:49:08Z"),
            "optimeDurableDate" : ISODate("2022-01-12T19:49:08Z"),
            "lastAppliedWallTime" : ISODate("2022-01-12T19:49:08.522Z"),
            "lastDurableWallTime" : ISODate("2022-01-12T19:49:08.522Z"),
            "lastHeartbeat" : ISODate("2022-01-12T19:49:14.145Z"),
            "lastHeartbeatRecv" : ISODate("2022-01-12T19:49:14.952Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncSourceHost" : "10.0.38.224:27017",
            "syncSourceId" : 2,
            "infoMessage" : "",
            "configVersion" : 1,
            "configTerm" : -1
        }
    ],
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1642016948, 1),
        "signature" : {
            "hash" : BinData(0,"<signature>"),
            "keyId" : NumberLong("<long>")
        }
    },
    "operationTime" : Timestamp(1642016948, 1)
}
Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
user2086359
  • 325
  • 3
  • 13
  • What do you get from `rs.status()`? – Wernfried Domscheit Jan 12 '22 at 18:00
  • I've updated my post above with rs.status() – user2086359 Jan 12 '22 at 19:51
  • Updated, it's supposed to be a comma separated list of the replica URLs and port numbers. – user2086359 Jan 12 '22 at 23:15
  • Can you connect directly to `10.0.7.79:27017` using the IP address? – Joe Jan 13 '22 at 04:14
  • Have a look at https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 However, your error messages does not match. Are you really sure you put the correct host names in the connection string? Please provide the real sting (without real password) - there is nothing to hide, you provided everything already by `rs.conf()` output. How do you start the `mongod`? Does your password contain a `@`? – Wernfried Domscheit Jan 13 '22 at 07:08
  • I can connect to 10.0.7.29:27017 yes. – user2086359 Jan 13 '22 at 08:30
  • I'm sure the connection strings are correct yes because when I don't define the replica set I can connect. The string is mongo "mongodb://username:password@MongoRegionA-bd298c48ad34dc83.elb.eu-west-1.amazonaws.com:27017,MongoRegionA-bd298c48ad34dc83.elb.eu-west-1.amazonaws.com:28017,MongoRegionA-bd298c48ad34dc83.elb.eu-west-1.amazonaws.com:29017/?replicaSet=s0" Removing ?replicaSet=s0 connects, but not always to the primary – user2086359 Jan 13 '22 at 08:33
  • Each of these three ports on the ELB points to a different target group which targets a single replica – user2086359 Jan 13 '22 at 08:34

0 Answers0