Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongo atlas configuration #314

Closed
1 task done
VivianaGarces opened this issue Jun 23, 2020 · 13 comments · Fixed by #337
Closed
1 task done

Mongo atlas configuration #314

VivianaGarces opened this issue Jun 23, 2020 · 13 comments · Fixed by #337
Labels
question Ask how to do something or how something works

Comments

@VivianaGarces
Copy link

Your question
How can I use the mongodb+srv string on the .env file?

What are you trying to do
I've tried to use that notation since I saw on a previous post that this was running correctly but when I try to connect I get a failed to connect to server on first connect
I tried several things

  1. Turned off firewall (I'm on windows)
  2. Whitelisted all IPs for on Mongo atlas for now
  3. Used the exact string that appears when you choose the 'connect your application' option but I get Error: getaddrinfo ENOTFOUND cluster0-server0.gcp.mongodb.net at GetAddrInfoReqWrap.onlookup [as oncomplete]
  4. Used a shard to connect, this time the error is MongoNetworkError: connection 0 to cluster0-shard-00-01-server0.gcp.mongodb.net:27017 closed

For reference:
Step 3 string: mongodb+srv://user:password@cluster0-server0.gcp.mongodb.net/dbname?retryWrites=true&w=majority

Step 4 string: mongodb+srv://user:password@cluster0-shard-00-01-server0.gcp.mongodb.net/dbname?retryWrites=true&w=majority

Appreciate the help.
Documentation feedback

  • Found documentation but was incomplete
@VivianaGarces VivianaGarces added the question Ask how to do something or how something works label Jun 23, 2020
@iaincollins
Copy link
Member

Hmm the getaddrinfo() suggests a problem with DNS look up but I am not certain what is triggering that.

It might be helpful to try using mongodb:// instead of mongodb+srv:// and seeing if that works.

@VivianaGarces
Copy link
Author

Hmm the getaddrinfo() suggests a problem with DNS look up but I am not certain what is triggering that.

It might be helpful to try using mongodb:// instead of mongodb+srv:// and seeing if that works.

Tried with step 3 and 4 strings and got the same errors, I installed mongodb's official driver like docs said but maybe I'm missing another thing?

@iaincollins
Copy link
Member

Hmm thanks. Just to check, are you literally using the hostname cluster0-server0.gcp.mongodb.net / cluster0-shard-00-01-server0.gcp.mongodb.net or are those placeholders for the actual hostnames?

@VivianaGarces
Copy link
Author

Hmm thanks. Just to check, are you literally using the hostname cluster0-server0.gcp.mongodb.net / cluster0-shard-00-01-server0.gcp.mongodb.net or are those placeholders for the actual hostnames?

Placeholders, the real ones are cluster0-gaik0.gcp.mongodb.net and cluster0-shard-00-01-gaik0.gcp.mongodb.net

@iaincollins
Copy link
Member

I did some digging, spin up a cluster on Atlas (mongodb.net) and got the same error.

I got it working if I pass an object (rather than a string) and set "useNewUrlParser" to true.

  database: {
    type: "mongodb",
    useNewUrlParser: true,
    url: process.env.DATABASE_URL,
  },

I don't have to do this when invoking a connection from the mongodb driver directly, so this seems to be caused by something that TypeORM does with 'mongodb+srv' URLs.

In a future update we can patch this to enable that option by default for mongodb+srv URLs, so you can just pass it as a string.

@VivianaGarces
Copy link
Author

VivianaGarces commented Jun 23, 2020

First, thanks for answering so fast, a true lifesaver. It works but I had to remove the last part of the URL ?retryWrites=true&w=majority because it was being identified like part of the database name. Does it make any difference or the plugin takes care of those additional flags?
Final string url on .env file is mongodb+srv://user:password@cluster0-gaik0.gcp.mongodb.net/dbName

@iaincollins
Copy link
Member

iaincollins commented Jun 23, 2020

Oh awesome thanks for confirming!

Doh I can see that same behaviour too. Not sure what to do about that yet!

I think maybe you can pass them to TypeORM as options on the object (maybe in a property called 'extra'?).

We can leave this open and I'll investigate that!

@sfny
Copy link

sfny commented Jun 23, 2020

I ran into the same mongo connection issues today, followed the steps in this thread (pass an object instead of the connection string) and got it working. Thanks!

@VivianaGarces
Copy link
Author

About the extra argument: I got this after a while TypeError: Cannot use 'in' operator to search for 'poolSize' in ?retryWrites=true&w=majority, looks like it is working in my development env without any issue without that extra part of the URL but I hope you can find a solution

@iaincollins
Copy link
Member

iaincollins commented Jun 24, 2020

I think you should be able to find an answer in the TypeORM docs / issues somewhere, it looks like talking to Atlas hosted DB's (with the mongdb+srv pragma) has come up a few times.

I'll definitely try and see if we can abstract it automatically, to address whatever TypeORM is doing that is wrong.

@iaincollins
Copy link
Member

Can I customize the authentication callback functions without connecting to the Mongo Atlas for current work?

Yes.

You don't need to use a database to use the callback functions.

@iaincollins
Copy link
Member

@VivianaGarces Thanks again for the bug report! Have created #328 to track it until we can handle it better by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Ask how to do something or how something works
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
@iaincollins @sfny @VivianaGarces and others