1 / 1
Jul 2024

We have a 3-node MongoDB replica set cluster deployed via Ops Manager with TLS enabled. Our Node.js application can only connect when the first node is the primary. When the primary node switches to another node, the application fails to connect.

Here is a nodejs code
const { MongoClient } = require(‘mongodb’);

const uri = “mongodb://test:mongodev@node1:37017,node2:37017,node3:37017/test?replicaSet=devrs&tls=true&tlsCAFile=/etc/ssl/mongodev.pem&tlsCertificateKeyFile=/etc/ssl/mongodev.pem&authSource=test”;
const client = new MongoClient(uri,{useUnifiedTopology: true});

async function run() {
try {
await client.connect();
console.log(‘Connected to MongoDB over TLS’);
// Use client to interact with MongoDB
} catch (err) {
console.error(‘Error connecting to MongoDB:’, err);
} finally {
await client.close();
}
}

run();

=================================
Error
/usr/src/app # node mongodb.js
Error connecting to MongoDB: MongoServerSelectionError: Server selection timed out after 30000 ms
at Timeout._onTimeout (/usr/src/app/node_modules/mongodb/lib/sdam/topology.js:278:38)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7) {
reason: TopologyDescription {
type: ‘ReplicaSetNoPrimary’,
servers: Map(2) {
‘node1.:37017’ => [ServerDescription],
‘node2.:37017’ => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: ‘devrs’,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: 30
},
code: undefined,
[Symbol(errorLabels)]: Set(0) {}
}