question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't Connect To MongoDB Atlas Cluster

See original GitHub issue

Issue type:

[ ] question [X] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [X] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[X] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

I have a valid connection string to a Mongo Atlas DB that I am trying to use with TypeORM. I know the connection string works because I have used it from Python and from the Mongo shell. But it doesn’t work from typeorm. I have tried both the mongodb:// and mongo+srv synax. Not sure how to debug or figure this one out. Here is the error:

(node:19257) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
{ MongoNetworkError: connection 5 to cluster0-XXXX.mongodb.net:27017 closed
    at Socket.<anonymous> (/home/administrator/Source/Repos/emoon-relayer/node_modules/mongodb-core/lib/connection/connection.js:276:9)
    at Object.onceWrapper (events.js:273:13)
    at Socket.emit (events.js:182:13)
    at TCP._handle.close (net.js:611:12)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:19 (4 by maintainers)

github_iconTop GitHub Comments

13reactions
vlapocommented, Jan 4, 2019

After few tries I have two valid configurations for typeorm and mongo atlas:

const connection = await createConnection({
            type: "mongodb",
            useNewUrlParser: true,
            url: "mongodb://admin:password@testcluster0-shard-00-00-1cmyo.mongodb.net:27017,testcluster0-shard-00-01-1cmyo.mongodb.net:27017,testcluster0-shard-00-02-1cmyo.mongodb.net:27017/test?ssl=true&replicaSet=TestCluster0-shard-0&authSource=admin&retryWrites=true",
            ssl: true,
            authSource: "admin",
            replicaSet: "TestCluster0-shard-0"
        });

and second one

const connection = await createConnection({
            type: "mongodb",
            url: "mongodb+srv://admin:admin@testcluster0-1cmyo.mongodb.net/test?retryWrites=true",
            ssl: true,
            authSource: "admin",
            replicaSet: "TestCluster0-shard-0"
        });

But I think @pointtoken is right. Calling mongodb connect method with undefined option properties overwrites settings from url.

8reactions
AmbassEugenecommented, Apr 1, 2021

So the below worked for me. I am using Nestjs

{
  "type": "mongodb",
  "url": "mongodb+srv://<user>:<password>@cluster0.somethingsomething.mongodb.net/<dbname>?retryWrites=true&w=majority",
  "useNewUrlParser": true,
  "synchronize": true,
  "logging": true,
  "useUnifiedTopology": true,
  "entities": ["dist/**/*.entity{.ts,.js}"]
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't connect to mongodb.Could not connect to any servers in ...
First try to Allow Access From Anywhere. If it works it is most likely because you whitelisted an IP address that is not...
Read more >
"Could not connect to any servers in your MongoDB Atlas ...
In your Mongo project, go to the network access section, and edit the IP to allow access from anywhere ( ...
Read more >
Could not connect to any servers in your MongoDB Atlas cluster.
Go to Network Access in Security section. Click + Add IP Address button. Click Add current IP address . Access your localhost and ......
Read more >
NodeJS Container can't connect to MongoDB Atlas Cluster
NodeJS Container can't connect to MongoDB Atlas Cluster ... Hello docker devs: I'm using docker with node js and MongoDB atlas cluster.
Read more >
Can't connect to MongoDB atlas serverless instance
'm trying to connect to an Atlas Serverless database from Nodejs. It just won't work. Tried and triple checked everything I found. mongosh ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found