当我尝试打开与MongoDB副本集的远程(公共IP)连接时遇到问题。 使用便携式计算机上的mongo shell,我可以打开连接,但是使用nodejs时,我收到错误消息“ MongoError:在副本集中找不到主副本或无效的副本集名称”。
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"});
输出:
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版本v4.0.4):
mongo -u "test" -p 'XXXXX' 'mongodb://mongo0:27017,mongo1:27017/mydb?replicaSet=rstest'
输出:
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>
也许是我做错了...
编辑:我也尝试将pymongo用于python,它可以完美运行。另一件事是使用nodejs,我需要在/ etc / hosts中设置mongo仲裁器的IP,但不要使用mongo shell和pymongo。
谢谢
答案 0 :(得分:1)
好吧,终于我找到了。我在/ etc / hosts中犯了一个错误。 Mongo0具有Mongo1的公共IP,而Mongo1具有Mongo0的公共IP。奇怪的是,python和mongo shell不会抱怨它,但是Nodejs是。