Matt
Matt

Reputation: 4387

NodeJs Mongodb connection issue replica set not found

I've an issue when I try to open a remote(Public IP) connection to a MongoDB replica set. Using the mongo shell from my laptop, I'm able to open the connection but with nodejs I'm receiving the error "MongoError: no primary found in replicaset or invalid replica set name".

NodeJs(v11.4.0 - mongo lib: 3.1.10)

var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://test:XXXXX@mongo0:27017,mongo1:27017/mydb', {useNewUrlParser: true, replicaSet: "rstest"}); 

Output:

node:1050) UnhandledPromiseRejectionWarning: MongoError: no primary found in replicaset or invalid replica set name
at /node_modules/mongodb-core/lib/topologies/replset.js:636:11
at Server.<anonymous> (/node_modules/mongodb-core/lib/topologies/replset.js:357:9)
at Object.onceWrapper (events.js:277:13)
at Server.emit (events.js:189:13)
at /node_modules/mongodb-core/lib/topologies/server.js:508:16
at /node_modules/mongodb-core/lib/connection/pool.js:532:18
at process.internalTickCallback (internal/process/next_tick.js:70:11)
(node:1050) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1050) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Mongo Shell(MongoDB shell version v4.0.4):

mongo -u "test" -p 'XXXXX' 'mongodb://mongo0:27017,mongo1:27017/mydb?replicaSet=rstest'

Output:

MongoDB shell version v4.0.4
connecting to: mongodb://mongo1:27017,mongo0:27017/mydb?replicaSet=rstest
2018-12-14T12:39:38.536+0000 I NETWORK  [js] Starting new replica set monitor for rstest/mongo1:27017,mongo0:27017
2018-12-14T12:39:38.646+0000 I NETWORK  [ReplicaSetMonitor-TaskExecutor]  Successfully connected to mongo1:27017 (1 connections now open to mongo1:27017 with a 5 second timeout)
2018-12-14T12:39:38.649+0000 I NETWORK  [js] Successfully connected to mongo0:27017 (1 connections now open to mongo0:27017 with a 5 second timeout)
WARNING: No implicit session: Logical Sessions are only supported on  server versions 3.6 and greater.
Implicit session: dummy session
MongoDB server version: 3.4.4
WARNING: shell and server versions do not match
rstest:PRIMARY> 

Maybe be I'm doing something wrong...

EDIT: I've also try with pymongo for python and it works perfectly. Another thing is with nodejs, I need to set the IP of the mongo arbitrer in my /etc/hosts but not with mongo shell and pymongo.

Thanks

Upvotes: 0

Views: 3196

Answers (1)

Matt
Matt

Reputation: 4387

Well, finally I found it. I'd made a mistake in my /etc/hosts. Mongo0 had the Public IP of Mongo1 and Mongo1 the public IP of Mongo0. The weird part is, python and mongo shell don't complain about it but Nodejs yes.

Upvotes: 2

Related Questions