Node.js services unable to connect
kolotyluk opened this issue · 1 comments
I am starting up Mongo via
mongoDbReplicaSet = MongoDbReplicaSet.builder()
.mongoDockerImageName("mongo:4.4.4")
.build()
from within my JUnit5/Kotlin framework. I immediately test this using KMongo, and it seems to work fine using the mongoDbReplicaSet.getReplicaSetUrl()
string. For example, something like mongodb://localhost:55051/test&readPreference=primary
Then I start my Node.js service via Java's ProcessBuilder
and the service seems to run okay. I can send it HTTP requests from my JUnit 5 code, and it responds. However, when I try to connect to MongoDB using the same URI, the Mongo Node driver responds with
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at Timeout.<anonymous> (/Users/ekolotyluk/dev/crypto/customer-js/node_modules/mongodb/lib/sdam/topology.js:310:38)
at listOnTimeout (internal/timers.js:554:17)
at process.processTimers (internal/timers.js:497:7) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'docker-rs',
maxSetVersion: 1,
maxElectionId: new ObjectId("7fffffff0000000000000001"),
commonWireVersion: 9,
logicalSessionTimeoutMinutes: undefined
}
}
I don't understand why the message is complaining about connect ECONNREFUSED 127.0.0.1:27017
since the URI specifies port 55051
. I don't know if the message is from the driver or the server?
When I start MongoDB manually in Docker from our scripts, my Node.js service is able to interact fine with MongoDB.
I also tried using https://www.testcontainers.org/modules/databases/mongodb/ but then the error I get is
MongoServerSelectionError: getaddrinfo ENOTFOUND 3d955cee0480
at Timeout.<anonymous> (/Users/ekolotyluk/dev/crypto/customer-js/node_modules/mongodb/lib/sdam/topology.js:310:38)
at listOnTimeout (internal/timers.js:554:17)
at process.processTimers (internal/timers.js:497:7) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map { '3d955cee0480:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'docker-rs',
maxSetVersion: 1,
maxElectionId: new ObjectId("7fffffff0000000000000001"),
commonWireVersion: 9,
logicalSessionTimeoutMinutes: undefined
}
}
This seems to be a problem with white-listing, where MongoDB does not like the IP Address my Node.js service is calling from. https://stackoverflow.com/questions/65277035/mongoserverselectionerror-connection-timed-out-with-nodejs-and-express
Any insight on what the problems may be here would be greatly appreciated.
Hi @kolotyluk,
As an experiment, could you try starting up via:
MongoDbReplicaSet.builder()
.replicaSetNumber(2)
.addArbiter(true)
.build()
to force to use a hostname (requires adding host.docker.internal
or dockerhost
to the OS host file. See Supported features or an article for details) ?
MongoDbReplicaSet#getReplicaSetUrl()
will return something like mongodb://dockerhost:55220,dockerhost:55221/test?replicaSet=docker-rs&readPreference=primary
in this case