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

MongoDB connection error: MongooseServerSelectionError: connection timed out #9308

Closed
elemikelo opened this issue Aug 6, 2020 · 14 comments
Closed
Labels
needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity

Comments

@elemikelo
Copy link

Hello,
when I launch the tests (unit and integration) in my project mongodb returns an error (MongoDB connection error: MongooseServerSelectionError: connection timed out).

My mongo configuration parameters are:
mongoose.connect('mongodb://localhost:27017/my_db_name?replicaSet=rs0', {}) // without options

IMPORTANT: The configuration of mongo is in replicat set but with a single cluster.

If I add the option 'use UnifiedTopology: false' the tests work correctly but it returns a warning of type: 'DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.'.

In the latest version of mongoose it indicates that using UnifiedTopology is false by default.

Why do I have to add it manually?

Framework test: mocha v5.2.0
Node version: v10.19.0
MongoDB: v4.2.3
Mongoose: v5.9.27

@mulfyx
Copy link

mulfyx commented Aug 10, 2020

I have the same problem, the temporary solution is to install mongoose@5.8.13

@elemikelo
Copy link
Author

I have the same problem, the temporary solution is to install mongoose@5.8.13

yes, I didn't have a problem with earlier versions either, it was when I upgraded to the latest version of mongoose

@vkarpov15 vkarpov15 added this to the 5.10.1 milestone Aug 16, 2020
@vkarpov15
Copy link
Collaborator

Can you please explain what you mean by "the configuration of mongo is in replicat set but with a single cluster"? There isn't much we can do to debug this without more information.

@vkarpov15 vkarpov15 added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Aug 24, 2020
@vkarpov15 vkarpov15 removed this from the 5.10.1 milestone Aug 24, 2020
@elemikelo
Copy link
Author

I'm sorry, I meant a single node @vkarpov15

@CarlosCondor
Copy link

Can you please explain what you mean by "the configuration of mongo is in replicat set but with a single cluster"? There isn't much we can do to debug this without more information.

I have the same problem. I think "single cluster" means "a replicaset with only one node", because this is the better way to be able to use transactions on local (because a replicaset is required)

@vkarpov15
Copy link
Collaborator

@CarlosCondor take a look at this section on replica set hostnames: https://mongoosejs.com/docs/connections.html#replicaset-hostnames . Often, replica set connection issues are due to your machine's hostname not matching the host name in the connection string you're passing to connect().

@CarlosCondor
Copy link

@CarlosCondor take a look at this section on replica set hostnames: https://mongoosejs.com/docs/connections.html#replicaset-hostnames . Often, replica set connection issues are due to your machine's hostname not matching the host name in the connection string you're passing to connect().

I read that section. And tried with different hostnames and it doesnt' seems to be the problem. (Now i am using 127.0.0.1 as host).

Thanks for your reply

@vkarpov15
Copy link
Collaborator

Are you on OSX or Linux? And, if so, what is the output of running hostname from your terminal?

@CarlosCondor
Copy link

Are you on OSX or Linux? And, if so, what is the output of running hostname from your terminal?

OSX. My hostname is MacBook-Pro.local

@vkarpov15
Copy link
Collaborator

Try using mongodb://MacBook-Pro.local:27017 in your connection string, that may help.

@CarlosCondor
Copy link

Try using mongodb://MacBook-Pro.local:27017 in your connection string, that may help.

Doesn't work. Same error using unifiedTopology:true

@vkarpov15
Copy link
Collaborator

That's strange. Can you print the error's reason property? That may explain what's happening:

mongoose.connect('mongodb://localhost:27017/my_db_name?replicaSet=rs0', { useUnifiedTopology: true }).catch(err => console.log(err.reason));

@oshoomhaneme
Copy link

hi, im also having problems with connection with localhost for mongo. it is saying:
connection error MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at NativeConnection.Connection.openUri
please what do i do?

@Automattic Automattic locked and limited conversation to collaborators Jan 5, 2022
@vkarpov15
Copy link
Collaborator

@oshoomhaneme please open a new issue and follow the issue template

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity
Projects
None yet
Development

No branches or pull requests

5 participants