简体   繁体   English

将 MongoDB 与 nodejs 连接时出现 ReplicaSetNoPrimary 和 MongoServerSelectionError 错误

[英]getting ReplicaSetNoPrimary and MongoServerSelectionError error while connecting MongoDB with nodejs

I am trying to connect to mongodb but getting below error could you please help我正在尝试连接到 mongodb,但出现以下错误,请您帮忙

var mongo = require('mongodb').MongoClient;
mongo.connect('mongodb://usernamexyz:passwordxyz@hostmxy-mw-e6-u1238.nam.nsroot.net:47017/sampleDB?replicaSet=NAME_2436&readPreference=primary&authSource=admin&w=1', { useNewUrlParser: true, useUnifiedTopology: true  })
.then(() => console.log("Mongodb connected"))
.catch(err => console.log(err));

And the error i am getting as below我得到的错误如下

MongoServerSelectionError: connection <monitor> to 155.30.360.129:37017 closed
    at Timeout._onTimeout (C:\Fintech\NodeFirstApp\node_modules\mongodb\lib\core\sdam\topology.js:448:30)
    at listOnTimeout (internal/timers.js:531:17)
    at processTimers (internal/timers.js:475:7) {
  name: 'MongoServerSelectionError',
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map {
      'hostmxy-mw-e6-u1238.nam.nsroot.net:47017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  },
  [Symbol(mongoErrorContextSymbol)]: {}
}

转到创建集群的 Mongodb,在“ipwhitelist”下单击编辑 IP 地址按钮,然后选择“添加当前 IP 地址”

There was a firewall issue for me, and here are the steps i followed:我遇到了防火墙问题,以下是我遵循的步骤:

  1. Search for Windows Firewall with Advanced Security搜索具有高级安全性的 Windows 防火墙
  2. Select Outbound Rules选择Outbound Rules
  3. Select New Rule选择New Rule
  4. Select Port in Rule Type , Click nextRule Type中选择Port ,点击下一步
  5. Select TCP and Specific remote ports and write in there 27015-27017选择TCPSpecific remote ports并在那里写入27015-27017
  6. Save the rule This worked for me :)保存规则这对我有用:)

在此处输入图像描述 在此处输入图像描述

I setup replica set on mongod server in order to have oplog facility and use it to sync database programmatically with golang client.我在 mongod 服务器上设置了副本集,以便拥有 oplog 设施并使用它以编程方式与 golang 客户端同步数据库。 It was working fine with local windows server.它在本地 Windows 服务器上运行良好。

mongod.exe --dbpath $dpath --bind_ip 0.0.0.0 --port $port --replSet rs0
mongo.exe' -port $port --eval 'rs.initiate()'

When I used docker it showed me that ReplicaSetNoPrimary Error.当我使用 docker 时,它向我显示了 ReplicaSetNoPrimary 错误。

docker run --name mongo21 -d -p 27021:27017 mongo  --bind_ip_all  --replSet rs0
docker container exec mongo21 mongosh --eval 'rs.initiate()' 127.0.0.1

I noticed that perl client as well as Robo3 T, mongosh connected to the server localhost:27021 without problem.我注意到 perl 客户端以及 Robo3 T,mongosh 连接到服务器 localhost:27021 没有问题。 mongosh shows which mongo uri it uses: mongosh 显示它使用哪个 mongo uri:

mongodb://127.0.0.1:27017/test?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.1.9

I found I can specify client option before connect:我发现我可以在连接之前指定客户端选项:

clientOpts := options.Client().ApplyURI(uri).SetDirect(true)
client, err := mongo.Connect(ctx, clientOpts)

That option fixed golang client connection issue.该选项修复了 golang 客户端连接问题。

i also got same error.我也有同样的错误。 I was using another wifi.我正在使用另一个wifi。 But when i connected my laptop with same wifi which i was using earlier , then my mongodb connected again.但是,当我将笔记本电脑与之前使用的相同 wifi 连接时,我的 mongodb 再次连接。

I was also getting the same error while connecting to MongoDB atlas.连接到 MongoDB 地图集时,我也遇到了同样的错误。 You have to do following things.你必须做以下事情。 1. allow outbound port 27015-27017 in firewall in your system. 1. 允许系统中防火墙的出站端口 27015-27017。 2. update the latest MongoDB and NodeJs. 2.更新最新的MongoDB和NodeJs。 3. whitelist your system IP in MongoDB atlas cluster under network access. 3. 将您的系统IP在网络访问下的MongoDB atlas集群中列入白名单。

It worked for me.它对我有用。 I hope it works for you also.我希望它也对你有用。

As MR Jay said in other words, this can also happen if your IP adress differs from the one that's registered in your Atlas account (for instance if you're not using a static IP).换句话说,正如杰伊先生所说,如果您的 IP 地址与您在 Atlas 帐户中注册的地址不同(例如,如果您没有使用静态 IP),也会发生这种情况。

Check "Network Access" in your MongoDB Atlas account and compare with your current IP.在您的 MongoDB Atlas 帐户中检查“网络访问”并与您当前的 IP 进行比较。

我遇到了同样的问题,但通过简单地更正我的 IP 地址设法解决了它 使用此网站检查您的 IP 地址 - https://www.whatismyip-address.com/?check

Whitelisting the IP obtained by clicking "Add current IP address" gave me the same error.将通过单击“添加当前 IP 地址”获得的 IP 列入白名单给了我同样的错误。 However, whitelisting the IP address shown on https://www.whatismyip.com/ , https://whatismyipaddress.com/ , or https://nordvpn.com/what-is-my-ip/ solved my issue.但是,将https://www.whatismyip.com/https://whatismyipaddress.com/https://nordvpn.com/what-is-my-ip/上显示的 IP 地址列入白名单解决了我的问题。

Updating "Network Access" did not work for me.更新“网络访问”对我不起作用。 What worked for me was to update "Database Access" by "Autogenerate Secure Password" and make sure to click "Update User" to save new password!对我有用的是通过“自动生成安全密码”更新“数据库访问”并确保单击“更新用户”以保存新密码!

MongoDB Atlas > Database Access > complete 3 steps in image MongoDB Atlas > 数据库访问 > 完成图像中的 3 个步骤

在此处输入图像描述

I had the same issue with this.我对此有同样的问题。 It was fixed when I added the current IP address to IP whitelist inside of MongoDB.当我将当前 IP 地址添加到 MongoDB 内的 IP 白名单时,它已修复。

Some MongoDB implementations as a service such as IBM Cloud, GCP or AWS might need a certificate to be able to connect.一些 MongoDB 实施即服务,例如 IBM Cloud、GCP 或 AWS,可能需要证书才能连接。 That was my case.那是我的情况。 The problem was resolved when I added a certificate to the docker file and then passed the parameters as options当我将证书添加到 docker 文件中,然后将参数作为选项传递时,问题得到了解决

let options = {
    tls: true,
    tlsCAFile: `/path/to/cert`,
    useUnifiedTopology: true 
};

// connects to a MongoDB database
MongoClient.connect(connectionString, options) 

Reference https://cloud.ibm.com/docs/databases-for-mongodb?topic=databases-for-mongodb-mongodb-external-app参考https://cloud.ibm.com/docs/databases-for-mongodb?topic=databases-for-mongodb-mongodb-external-app

I was using Atlas Mongo Cluster and using 'mongomirror' I am trying to push the data from one replicaset cluster to another.我正在使用 Atlas Mongo Cluster 并使用 'mongomirror' 我试图将数据从一个副本集集群推送到另一个。 In the process, I was getting this error..在这个过程中,我得到了这个错误..

After ot of research, I understood that, the name of cluster visible in UI should not be used.. instead you need to run the below command经过大量研究,我了解到,不应使用 UI 中可见的集群名称。相反,您需要运行以下命令

rs.status()

This will give a JSON output which has the RS name and respective primary and secondary node endpoints you need to use.这将提供一个 JSON 输出,其中包含您需要使用的 RS 名称以及相应的主节点和辅助节点端点。

In my case, for abc-temp-shard-cluster, the cluster name I got by running the above command was something like below就我而言,对于 abc-temp-shard-cluster,我通过运行上述命令获得的集群名称如下所示

atlas-12avca-shard-0/atlas-12avca-shard-00-00.1aqer5.mongodb.net:27017,atlas-12avca-shard-00-01.1aqer5.mongodb.net:27017,atlas-12avca-shard-00-02.1aqer5.mongodb.net:27017

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM