Mongoose: MongoError: no primary found in replicaset or invalid replica set name

Created on 13 Jan 2020  路  4Comments  路  Source: Automattic/mongoose

When I try mongoose.connect() to mongodb atlas through private endpoint, MongoError: no primary found in replicaset or invalid replica set name occured.

But, trying with mongo shell, I can connect to atlas.

package.json

"mongoose": "^5.6.9"

index.js

const mongoose = require('mongoose')

  const url = process.env.DB_URL

  const options = {
    useNewUrlParser: true,
    useFindAndModify: false
  }

  mongoose.connect(url, options)
    .then((connection) => {
      console.log('DB connected')
      return connection
    })

DB_URLs

not working

mongodb+srv://<username>:<password>@clusterlis-pl-0-prgy7.mongodb.net/test?retryWrites=true&w=majority

not working too

mongodb://<username>:<password>@pl-0-ap-northeast-2-prgy7.mongodb.net:7,pl-0-ap-northeast-2-prgy7.mongodb.net:8,pl-0-ap-northeast-2-prgy7.mongodb.net:9/test?ssl=true&replicaSet=ClusterLIS-shard-0&authSource=admin&retryWrites=true&w=majority

mongo shell command is working

$ mongo "mongodb+srv://clusterlis-pl-0-prgy7.mongodb.net/test"  --username <username>

MongoDB shell version v4.2.2
Enter password:
connecting to: mongodb://pl-0-ap-northeast-2-prgy7.mongodb.net:8,pl-0-ap-northeast-2-prgy7.mongodb.net:9,pl-0-ap-northeast-2-prgy7.mongodb.net:7/lis?authSource=admin&compressors=disabled&gssapiServiceName=mongodb&replicaSet=ClusterLIS-shard-0&ssl=true
2020-01-13T15:47:03.436+0900 I  NETWORK  [js] Starting new replica set monitor for ClusterLIS-shard-0/pl-0-ap-northeast-2-prgy7.mongodb.net:8,pl-0-ap-northeast-2-prgy7.mongodb.net:9,pl-0-ap-northeast-2-prgy7.mongodb.net:7
2020-01-13T15:47:03.437+0900 I  CONNPOOL [ReplicaSetMonitor-TaskExecutor] Connecting to pl-0-ap-northeast-2-prgy7.mongodb.net:8
2020-01-13T15:47:03.437+0900 I  CONNPOOL [ReplicaSetMonitor-TaskExecutor] Connecting to pl-0-ap-northeast-2-prgy7.mongodb.net:9
2020-01-13T15:47:03.437+0900 I  CONNPOOL [ReplicaSetMonitor-TaskExecutor] Connecting to pl-0-ap-northeast-2-prgy7.mongodb.net:7
2020-01-13T15:47:03.510+0900 I  NETWORK  [ReplicaSetMonitor-TaskExecutor] Confirmed replica set for ClusterLIS-shard-0 is ClusterLIS-shard-0/pl-0-ap-northeast-2-prgy7.mongodb.net:7,pl-0-ap-northeast-2-prgy7.mongodb.net:8,pl-0-ap-northeast-2-prgy7.mongodb.net:9
Implicit session: session { "id" : UUID("38596f54-f367-499b-b016-5a22f735221b") }
MongoDB server version: 4.0.14
WARNING: shell and server versions do not match
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
MongoDB Enterprise ClusterLIS-shard-0:PRIMARY>

how can I solve this problem? thx for your answer, and sorry for my poor english.

needs clarification

Most helpful comment

i was having this same issue and got past this error by adding useUnifiedTopology:true to the mongoose connection options (https://mongoosejs.com/docs/connections.html#server-selection). there seem to be some side effects from this option switch that i'm working through, but it is connecting now at least.

edit: the "side effects" turned out to be just getting things in line with the deprecation warnings (https://mongoosejs.com/docs/deprecations.html), now the connection works as expected with the mongodb+srv uri. Try getting the options in line with those deprecations and see if that solves the issue. Hope it helps!

All 4 comments

What does "through private endpoint" mean? Also, can you please provide the error's stack trace?

Mongo DB atlas private endpoint : https://docs.atlas.mongodb.com/security-private-endpoint/

I'll reopen the issue for later with more information.

Thank you.

i was having this same issue and got past this error by adding useUnifiedTopology:true to the mongoose connection options (https://mongoosejs.com/docs/connections.html#server-selection). there seem to be some side effects from this option switch that i'm working through, but it is connecting now at least.

edit: the "side effects" turned out to be just getting things in line with the deprecation warnings (https://mongoosejs.com/docs/deprecations.html), now the connection works as expected with the mongodb+srv uri. Try getting the options in line with those deprecations and see if that solves the issue. Hope it helps!

i was having this same issue and got past this error by adding useUnifiedTopology:true to the mongoose connection options (https://mongoosejs.com/docs/connections.html#server-selection). there seem to be some side effects from this option switch that i'm working through, but it is connecting now at least.

edit: the "side effects" turned out to be just getting things in line with the deprecation warnings (https://mongoosejs.com/docs/deprecations.html), now the connection works as expected with the mongodb+srv uri. Try getting the options in line with those deprecations and see if that solves the issue. Hope it helps!

It work, thanks you, but i don't now how it work. :))))))

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jeneser picture jeneser  路  3Comments

simonxca picture simonxca  路  3Comments

p3x-robot picture p3x-robot  路  3Comments

tarun1793 picture tarun1793  路  3Comments

gustavomanolo picture gustavomanolo  路  3Comments