FG
๐Ÿ—„๏ธ DatabasesMongoDB

Mongoose 4.6.2 produces error "MongoError: no primary found in replicaset" while 4.4.19 works just fine

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score80%
80%

Problem

Simple connection to a MongoDB replica set. Works great in Mongoose 4.4.19 but not in 4.6.2 mongoURI = "mongodb://IP:27017,IP:27017/" + dbName + "?replicaSet=my_replica_set"; Using 4.6.2, I have tried the following: - defining replset in connection options - connectWithNoPrimary: true - tried both mongoose.createConnection vs mongoose.connect - included and excluded arbiter node in the connection string If connection string only uses one database, it works fine. I am really curious as to why this error occurs. Any thoughts? Full Error: /Users/adeel/dev/navi/euler/node_modules/mongodb/lib/replset.js:360 process.nextTick(function() { throw err; }) ^ MongoError: no primary found in replicaset at /Users/adeel/dev/navi/euler/node_modules/mongodb-core/lib/topologies/replset.js:631:32 at .<anonymous> (/Users/adeel/dev/navi/euler/node_modules/mongodb-core/lib/topologies/replset.js:421:24) at g (events.js:286:16) at emitOne (events.js:96:13) at emit (events.js:188:7) at .<anonymous> (/Users/adeel/dev/navi/euler/node_modules/mongodb-core/lib/topologies/server.js:313:21) at emitOne (events.js:96:13) at emit (events.js:188:7) at .<anonymous> (/Users/adeel/dev/navi/euler/node_modules/mongodb-core/lib/connection/pool.js:260:12) at g (events.js:286:16) at emitTwo (events.js:106:13) at emit (events.js:191:7) at Socket.<anonymous> (/Users/adeel/dev/navi/euler/node_modules/mongodb-core/li

Error Output

error occurs. Any thoughts?

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Update MongoDB Connection String for Mongoose 4.6.2

Medium Risk

The error 'no primary found in replicaset' occurs because Mongoose 4.6.2 may have stricter requirements for the replica set configuration or connection string format compared to 4.4.19. This can lead to issues if the replica set is not properly configured or if the connection string does not specify the correct parameters for the replica set.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Verify Replica Set Configuration

    Ensure that the MongoDB replica set is properly configured and that there is a primary node available. Use the MongoDB shell to check the status of the replica set.

    bash
    rs.status()
  2. 2

    Modify Connection String

    Update the connection string to include the correct replica set name and ensure that you are using the correct IP addresses for all members of the replica set. Ensure that the format is correct.

    javascript
    mongoURI = 'mongodb://IP1:27017,IP2:27017,IP3:27017/' + dbName + '?replicaSet=my_replica_set';
  3. 3

    Use Mongoose Connection Options

    When connecting with Mongoose, explicitly set the connection options to ensure compatibility with the replica set. This includes specifying the 'useNewUrlParser' and 'useUnifiedTopology' options.

    javascript
    mongoose.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true });
  4. 4

    Check for Network Issues

    Ensure that there are no network issues or firewalls blocking access to the MongoDB instances. Check connectivity to each IP address specified in the connection string.

    bash
    ping IP1; ping IP2; ping IP3;
  5. 5

    Test Connection

    After making the above changes, test the connection to ensure that Mongoose can successfully connect to the MongoDB replica set without errors.

    javascript
    mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection error:'));

Validation

Confirm that the application can connect to the MongoDB replica set without throwing the 'no primary found in replicaset' error. Monitor the logs for successful connection messages.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

mongoosemongodbodmneeds-clarification