I am trying to connect to mongodb but getting below error could you please help
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:
Outbound Rules
New Rule
Port
in Rule Type
, Click nextTCP
and Specific remote ports
and write in there 27015-27017
I setup replica set on mongod server in order to have oplog facility and use it to sync database programmatically with golang client. It was working fine with local windows server.
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 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. mongosh shows which mongo uri it uses:
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.
i also got same error. I was using another wifi. But when i connected my laptop with same wifi which i was using earlier , then my mongodb connected again.
I was also getting the same error while connecting to MongoDB atlas. You have to do following things. 1. allow outbound port 27015-27017 in firewall in your system. 2. update the latest MongoDB and NodeJs. 3. whitelist your system IP in MongoDB atlas cluster under network access.
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).
Check "Network Access" in your MongoDB Atlas account and compare with your current IP.
我遇到了同样的问题,但通过简单地更正我的 IP 地址设法解决了它 使用此网站检查您的 IP 地址 - https://www.whatismyip-address.com/?check
Whitelisting the IP obtained by clicking "Add current IP address" gave me the same error. 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.
I had the same issue with this. It was fixed when I added the current IP address to IP whitelist inside of MongoDB.
Some MongoDB implementations as a service such as IBM Cloud, GCP or AWS might need a certificate to be able to connect. That was my case. The problem was resolved when I added a certificate to the docker file and then passed the parameters as options
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
I was using Atlas Mongo Cluster and using 'mongomirror' I am trying to push the data from one replicaset cluster to another. 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
rs.status()
This will give a JSON output which has the RS name and respective primary and secondary node endpoints you need to use.
In my case, for abc-temp-shard-cluster, the cluster name I got by running the above command was something like below
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.