问题
So I'm currently wirting a web application and I need database so I decided to go with mongodb and mongoose. Until now I tested everything on localhost and it worked but I wanted to move the data to a server. I had heard of Atlas and registered myself and "uploaded" the data.
Now I want to connect to the cluster via a node.js app.
mongoose.connect('mongodb+srv://engllucas:p%40ssw0rd@insight-quhku.mongodb.net/test');
I got the string of the mongodb Atlas site at the point Connect Your Application
Then I swapped my password.
mongoose.connect('mongodb://engllucas:p%40ssw0rd@insight-quhku.mongodb.net/test');
This didn't work either.
Thats the error message:
{ MongoError: failed to connect to server [insight-shard-00-02-quhku.mongodb.net:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 18.194.163.64:27017]
at Pool.<anonymous> (U:\WEBT\ProjectInsight\Quiz\node_modules\mongodb-core\lib\topologies\server.js:503:11)
at emitOne (events.js:115:13)
at Pool.emit (events.js:210:7)
at Connection.<anonymous> (U:\WEBT\ProjectInsight\Quiz\node_modules\mongodb-core\lib\connection\pool.js:326:12)
at Object.onceWrapper (events.js:318:30)
at emitTwo (events.js:125:13)
at Connection.emit (events.js:213:7)
at TLSSocket.<anonymous> (U:\WEBT\ProjectInsight\Quiz\node_modules\mongodb-core\lib\connection\connection.js:245:50)
at Object.onceWrapper (events.js:316:30)
at emitOne (events.js:115:13)
at TLSSocket.emit (events.js:210:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoNetworkError',
message: 'failed to connect to server [insight-shard-00-02-quhku.mongodb.net:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 18.194.163.64:27017]' } (node:4920) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoNetworkError: failed to connect to server [insight-shard-00-02-quhku.mongodb.net:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 18.194.163.64:27017] (node:4920) [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.
回答1:
I also had a similar issue, and I was able to solve it by adding my white-listing my IP address under Clusters -> security -> IP Whitelist. Instead of clicking my current ip address
, just search what's my ip on google and paste that instead.
I hope it works!
回答2:
if you are using mongoose previous to version 5.0.15:
the mongodb+srv://
uri is not valid to the underlying native driver.
just removing the +srv will not work either, as the uri will need to include your replica hosts and the replicaSet/authSource parameters.
you can either
1. Upgrade to mongoose 5.0.15 and use the shorter +srv format
or
2. Use your current version with the full uri from your Atlas cluster dashboard, selecting the older driver (see Note below) version uri.
from your cluster dashboard, press the connect button, then (assuming you have whitelisted your node server's IP ) select "connect your application", then press "I am using 3.4 driver or older". Use the resulting uri string substituting your password.
Note: In the dialog for choosing your URI string in the connect helper on the Atlas cluster dashboard, it says "I am using driver 3.* or (newer|older)". This is a poor choice of words, as the 3.6 and 3.4 don't refer to the driver version, but the server version. Further, using one version of the server or the other has nothing to do with the driver being used to connect, which is where the actual dependency lies that determines which version of the URI to use.
回答3:
I have had this issue connecting to a MongoDB Atlas cluster. I have had to resolve this issue twice because I was working on a project from two different computers (a Macbook Pro and a Windows PC).
Here is my comprehensive answer for everything that I have figured out to resolve the issue of my application not connecting to the cluster.
First Make sure your IP is whitelisted like the above suggestions. The easiest solution is the "All IPs" whitelist of 0.0.0.0/0
Second Check your VPN if you are using one. I was trying to connect to the cluster while connected to a VPN. Once I turned off my VPN, I was able to connect.
Third Make sure your internet settings are not preventing you from connecting to the cluster. My home Xfinity wifi security settings were set too high and this was preventing the connection. I think the router was not allowing a connection to port 27017. One way to test if this issue is occurring is to tether your computer to your phone for internet instead of your wifi and try connecting. I was able to connect using my iPhone as a hotspot for internet. I reset my router to factory settings which fixed the issue.
回答4:
Above answer is right but just i want to added one point:
From security perspective it is not good;
I also had a similar issue, and I was able to solve it by adding my white-listing my IP address under
Clusters -> security -> IP Whitelist.
Instead of clicking mycurrent ip address
, just search what's my ip on google and paste that instead. I hope it works!I also had a similar issue, and I was able to solve it by adding my white-listing my IP address under
Clusters -> security -> IP Whitelist.
added this IP in list:0.0.0.0/0
it will open to all.
回答5:
It's an issue of your "IP Whitelist". Edit and update it once, it should work fine then.
回答6:
Apparently, changing
mongoose.connect(keys.mongoURI);
to
mongoose.connect(keys.mongoURI, () => { }, { useNewUrlParser: true })
.catch(err => {
console.log(err);
});
worked for me.
回答7:
Check that your current IP address is on the MongoAtlas IP whitelist. I came across this problem after I updated my VPN.
回答8:
I had a similar issue and wasn't able to connect to mongo. Apparently mongodb didn't like my password which contained special characters. I tried doing URL encoding and all other kinds of manipulations, but what proved to solved the issue for me eventually was simply changing the password to one that doesn't use special characters. I used the mongodb password generator and all works fine now.
回答9:
I had same problem, while IP whitelisting was set correctly!
The reason was, that MongoDB Enterprise Atlas requires SSL excryption layer trying to connect without that, produces non-reasonable and not self explanatory error like:
Failed to connect to mongodb-m0-nnxxx.mongodb.net:27017
No chance to Authorize
Trying to connect using mondodb client with IP whitelisting being the problem, error message like this is appearing:
mongo "mongodb+srv://mongodb-m0-nnxxx.mongodb.net/test" --username admin
DBClientConnection failed to receive message from mongodb-m0-shard-00-00-nnxxx.mongodb.net.:27017 - HostUnreachable: Connection closed by peer
Unable to reach primary for set mongodb-m0-shard-0
Cannot reach any nodes for set mongodb-m0-shard-0. Please check network connectivity and the status of the set. This has happened for 5 checks in a row.
For example using Robo 3T client, the checkbox "Use SSL Protocol" must be enabled, and **Authentication Method: Self-Signed Certificate **
Test produces Diagnostic output:
v Connected to mongodb-m0-nnxxx.mongodb.net:27017 via SSL tunnel
v Authorized on admin database as admin
Hope this helps somebody.
回答10:
I was getting that problem and I fixed it. Below is the steps that I used:
- First of all add new user in mongodb (Security > Database Access > ADD NEW USER > )
- then write to .env file new username and password.
- Kill the server and rerun the server.
This is the solution that worked for me.
回答11:
google => my ip adress => copyit
got to Atlas => Once you re in the Clusters Tab, check at the left side=> Security => NetWork Access => Edit Ip => paste the ip adress.
rerun your server and it should be working :)
来源:https://stackoverflow.com/questions/49894913/error-at-connecting-to-mongodb-atlas-server