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

useUnifiedTopology: true causes ECONNREFUSED. #9898

Closed
skavan opened this issue Feb 4, 2021 · 2 comments
Closed

useUnifiedTopology: true causes ECONNREFUSED. #9898

skavan opened this issue Feb 4, 2021 · 2 comments
Assignees

Comments

@skavan
Copy link

skavan commented Feb 4, 2021

Hi,

Bit of a mongo newbie, so forgive me if I use the wrong descriptive language.

"mongodb": "^3.6.4",
"mongoose": "^5.11.15",
 node: 14.15.4

Brand new install of mongoDB on a windows machine (A) with a db and collections.
It works fine...and a webapp (not mine) that displays data from the db , on this same machine (A) also works well...and proves that all is well on the mongoDB end.

But, I have a development machine (B) where I am writing a node app to access the data on Machine A (it is on the same segment). I am running into a strange problem.

Up until yesterday, it was working fine. I could happily access mongo on Machine A from node on Machine B (with useUnifiedTopology=true

But then, I decided to migrate my mongoDB to a one-node replicaSet to take advantage of the "watch" function. The process seems to have worked on the mongodb side. That webapp I referenced above is happily working, as is Compass. I see the oplog.rs and replset.* entries on the mongoDB instance.

But on my remote development machine (B), I can no longer connect to mongoDB on machine A, UNLESS I set useUnifiedTopology to false. When set to false, everything works (with the deprecation warning). That includes the collection.watch().on functionality. So the replication set seems to be working.

But when useUnifiedTopology=true, I get a DB connection error: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017.

BTW Compass works fine on the development machine.

My barebones code is below. I have tried a million variants, like wrapping the connection in an await - but all to no avail. What am I missing? Or is it a bug?

import mongoose from "mongoose";

const url = "mongodb://192.168.1.xxx:27017/MyDbName";

var options = {
  keepAlive: true,
  useNewUrlParser: true,
  useUnifiedTopology: false,
  connectWithNoPrimary: true
}

mongoose.connect(url, options);  

var db = mongoose.connection;

db.on("error", console.error.bind(console, "DB connection error:"));

db.once("open", function () {
  console.log("DB connection successful");
});
@skavan
Copy link
Author

skavan commented Feb 4, 2021

Of course, after hours and hours of searching, I just found the solution in another issue.
Hopefully it will help others: #9836 (comment)
append ?directConnection=true to the url.
Still feels like a bug or at least a doc change to me -- but what do I know.

@vkarpov15
Copy link
Collaborator

This sounds like an issue with replica set host names: https://mongoosejs.com/docs/connections.html#replicaset-hostnames . Typically this issue is because the machine running MongoDB has a hostname that is something the client can't reach.

directConnection=true means you aren't connecting to a replica set, you're connecting directly to one mongod server. Which may be fine for your use case, but defeats the point of having a replica set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants