user3302146
user3302146

Reputation: 335

Connection to mongodb replicaset fails when one of the members is unavailable

I have a mongodb replicaset with the following configuration

{
    "_id" : "rs0",
    "version" : 2,
    "protocolVersion" : NumberLong(1),
    "members" : [
            {
                    "_id" : 0,
                    "host" : "mongodb-mongodb-replicaset-0.mongodb-mongodb-replicaset.default.svc.cluster.local:27017",
                    "arbiterOnly" : false,
                    "buildIndexes" : true,
                    "hidden" : false,
                    "priority" : 1,
                    "tags" : {

                    },
                    "slaveDelay" : NumberLong(0),
                    "votes" : 1
            },
            {
                    "_id" : 1,
                    "host" : "mongodb-mongodb-replicaset-1.mongodb-mongodb-replicaset.default.svc.cluster.local:27017",
                    "arbiterOnly" : false,
                    "buildIndexes" : true,
                    "hidden" : false,
                    "priority" : 1,
                    "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("5c65617521068bf38fd3634a")
    }

} To test failover scenarios, I killed one of the members and tried to connect to the replicaset using mongodb node js driver. The connection string is as follows:

const url = mongodb+srv://mongodb-mongodb-replicaset-client.default.svc.cluster.local/users?replicaSet=rs0

The connection fails with the following error:

{ Error: failed to connect to server [mongodb-mongodb-replicaset-1.mongodb-mongodb-replicaset.default.svc.cluster.local:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 10.8.2.12:27017]
at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:564:11)
at emitOne (events.js:115:13)
at Pool.emit (events.js:210:7)
at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:318:30)
at emitTwo (events.js:125:13)
at Connection.emit (events.js:213:7)
at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:316:30)
at emitOne (events.js:115:13)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }

I have tried connecting with connectWithNoPrimary:true as well but the same error occurs. Is this expected behaviour ? or there is something that I am missing?

Upvotes: 1

Views: 2739

Answers (1)

klhr
klhr

Reputation: 3390

It looks like you have a replica set with only two members & no arbiter -- this means that it's not able to elect a new leader which means there's nothing to connect to after you failover. Take a look at Two nodes MongoDB replica set without arbiter for a much better description of what's going on.

Upvotes: 1

Related Questions