Closed Aviral-Gupta101 closed 1 year ago
Have you upgraded your Node.js version lately? I see you're using v19.7.0. I'm also seeing issues with mongoose when I run the newest 18.15 Node.js. I've downgraded to Node 16.19.1 and all is fine.
No, I have been using the same version from the beginning.
I am having the same issue. I have been trying to connect to mongo atlas with mongoose all day but get the same error as the OP.
I am using next.js built in api routes, but that uses Express.js like OP.
@Phasor It maybe possible that the problem is not with the mongoose or Node.js but atlas mongodb instance itself. As I am using free tier ( shared cluster ) I have limited access of what I can do with that cluster, please if you have a private instance of Atlas mongodb instance can you check if restarting the instance fixes the problem or not ? Else I think it would be nice if I talk to Atlas mongodb support for the same, maybe they can conform the problem is with their end or not.
I tried to chat to an Atlas support person, they weren't much help unfortunately!
@Luke-McMahon That means we now solely dependent upon mongoose team for looking into this issue, at least opening this issue conform that there are many people facing the same issue.
Are you able to connect to this MongoDB Atlas instance using the MongoDB shell?
@vkarpov15 certainly can!
Connecting to: mongodb+srv://<credentials>@cluster0.impowsr.mongodb.net/myFirstDatabase?appName=mongosh+1.8.0
Using MongoDB: 5.0.15 (API Version 1)
Using Mongosh: 1.8.0
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
Atlas atlas-pln5qm-shard-0 [primary] myFirstDatabase>
Do your credentials include any special characters? Escaping username and password with encodeURIComponent()
may help in that case.
Also, do you get the same error when running your application locally? Or does this just happen on your application servers?
Password only has alpha-numeric characters, so I can't imagine there's any issues there.
Have only been running the application locally
It also seems as though hitting a local mongodb works just fine
Just to confirm, what does your MONGO_URL
look like in your script? Please omit any sensitive details - username, password, hostname. But leave any query string parameters.
My MONGO_URL
looks like this
MONGO_URL=mongodb+srv://someusername:somepassword@mycluster.somerandomtext.mongodb.net/test
@Aviral-Gupta101 just to confirm, are you able to connect to that MONGO_URL using the MongoDB shell?
@vkarpov15 @Aviral-Gupta101 @Luke-McMahon The proper mongo connection string to use with mongodb / mongoose is
mongodb+srv://user:pass@mycluster.myproject.mongodb.net/mydb?retryWrites=true&w=majority
If you login to MongoDB Atlas and click on Connect and choose the one for connecting your app, i.e. native mongodb driver (which mongoose uses under the hood), you will see the additional query string after the db name ?retryWrites=true&w=majority
- I think that's actually required when using the mongodb driver to connect to Atlas.
For the MongoDB shell (mongosh) it doesn't seem to be needed, it probably configures those query params automatically when connecting. Also not needed for connecting to local mongodb server instance (not a replica set and not sharded under normal circumstances).
More details about the params here: https://stackoverflow.com/questions/65866769/the-meaning-of-retrywrites-truew-majority-when-you-connect-node-js-app-to-clus
@Phasor It maybe possible that the problem is not with the mongoose or Node.js but atlas mongodb instance itself. As I am using free tier ( shared cluster ) I have limited access of what I can do with that cluster, please if you have a private instance of Atlas mongodb instance can you check if restarting the instance fixes the problem or not ? Else I think it would be nice if I talk to Atlas mongodb support for the same, maybe they can conform the problem is with their end or not.
the instance is fine, I can connect with Compass.
@Aviral-Gupta101 just to confirm, are you able to connect to that MONGO_URL using the MongoDB shell?
I have never actually used that, I will let you know once I install mongosh.
@vkarpov15 @Aviral-Gupta101 @Luke-McMahon The proper mongo connection string to use with mongodb / mongoose is
mongodb+srv://user:pass@mycluster.myproject.mongodb.net/mydb?retryWrites=true&w=majority
If you login to MongoDB Atlas and click on Connect and choose the one for connecting your app, i.e. native mongodb driver (which mongoose uses under the hood), you will see the additional query string after the db name
?retryWrites=true&w=majority
- I think that's actually required when using the mongodb driver to connect to Atlas.For the MongoDB shell (mongosh) it doesn't seem to be needed, it probably configures those query params automatically when connecting. Also not needed for connecting to local mongodb server instance (not a replica set and not sharded under normal circumstances).
More details about the params here: https://stackoverflow.com/questions/65866769/the-meaning-of-retrywrites-truew-majority-when-you-connect-node-js-app-to-clus
There are many ways to connect to Atlas the one I am using is Connect using VS code
, I have been using that from beginning and no issue until now, it does not take any url parameter even though I tried the way you mentioned but still it did't worked for me.
@Aviral-Gupta101 Can you connect with Compass? VSCode connection is buggy. Which version of VSCode are you using?
@Aviral-Gupta101 Can you connect with Compass? VSCode connection is buggy. Which version of VSCode are you using?
Yes I can connect with Compass, and I am using VSCode Version: 1.76.2
@Aviral-Gupta101 Can you connect with Compass? VSCode connection is buggy. Which version of VSCode are you using?
Yes I can connect with Compass, and I am using VSCode Version:
1.76.2
There's your answer: don't connect with VSCode 1.76 (February). You could try reverting to the previous version 1.75.1 (January), if that worked for you https://code.visualstudio.com/updates/v1_75 Or, if you're up to it, the latest Insiders (beta) version 1.77 https://code.visualstudio.com/insiders/
@Aviral-Gupta101 either way, this isn't the place to report issues connecting to Atlas with VS Code.
@lorand-horvath I'm able to connect to Atlas even without ?retryWrites=true&w=majority
.
@Aviral-Gupta101 your comments are off topic. This is not the place to report issues connecting to MongoDB with VSCode.
Prerequisites
Mongoose version
6.9.1
Node.js version
19.7.0
MongoDB server version
5.0.15
Typescript version (if applicable)
No response
Description
I am having a problem connecting Atlas Mongodb instance, don't know if the problem is with mongoose or Atlas Mongodb instance, the problem is all my NodeJs application which are using Atlas MongoDB instance stop connecting to the database.
I remembered one of the application worked perfectly fine on 24th of Feb, but when I tried to run the same application again on 16 March 2023 it showed me an error given below
MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/ at Connection.openUri (/Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/connection.js:825:32) at /Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/index.js:411:10 at /Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5 at new Promise (<anonymous>) at promiseOrCallback (/Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10) at Mongoose._promiseOrCallback (/Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/index.js:1285:10) at Mongoose.connect (/Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/index.js:410:20) at connectDB (/Users/aviralgupta/Development/NodeJs/MongoDB/config/dbConfig.js:8:24) at Object.<anonymous> (/Users/aviralgupta/Development/NodeJs/MongoDB/index.js:12:1) at Module._compile (node:internal/modules/cjs/loader:1275:14) { reason: TopologyDescription { type: 'ReplicaSetNoPrimary', servers: Map(3) { 'ac-a6to6lu-shard-00-00.nfmpr49.mongodb.net:27017' => [ServerDescription], 'ac-a6to6lu-shard-00-01.nfmpr49.mongodb.net:27017' => [ServerDescription], 'ac-a6to6lu-shard-00-02.nfmpr49.mongodb.net:27017' => [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, setName: 'atlas-tvdhx4-shard-0', maxElectionId: null, maxSetVersion: null, commonWireVersion: 0, logicalSessionTimeoutMinutes: null }, code: undefined } Error in dbConfig.js
As it mentions that
you're trying to access the database from an IP that isn't whitelisted
I tried to add my current IP address (even though access from anywhere IP: 0.0.0.0/0 was already present ) but still shows the same problem.Steps to Reproduce
index.js
package.json
Make sure to download express and mongoose package
Expected Behavior
It should be able to connect Atlas mongodb Instance.