简体   繁体   English

无法使用 MEAN 堆栈连接到 Mongodb.atlas 集群

[英]Unable to connect to Mongodb.atlas cluster using MEAN stack

Having issues connecting to my server using the MEAN stack.使用 MEAN 堆栈连接到我的服务器时出现问题。 I had no problems connecting until today and have not made code changes since so I am baffled on why all a sudden I am not able to connect.直到今天我都没有连接问题,并且从那以后没有进行代码更改,所以我对为什么突然无法连接感到困惑。

Connection:联系:

mongoose.connect("mongodb+srv://theller5567:" + process.env.MONGO_ATLAS_PW + "@cluster0-efzkv.mongodb.net/node-angular", { useNewUrlParser: true })
.then(() => {
    console.log("Connected to database!");
})
.catch((error) => {
    console.log("Connection failed!", error);
});

Response:回复:

[nodemon] starting `node server.js`
Connection failed! { MongoNetworkError: failed to connect to server 
[cluster0-shard-00-01-efzkv.mongodb.net:27017] on first connect 
[MongoNetworkError: getaddrinfo ENOTFOUND cluster0-shard-00-01- 
efzkv.mongodb.net cluster0-shard-00-01-efzkv.mongodb.net:27017]
at Pool.<anonymous> (/Users/Travis/Desktop/Github_Repos/OMNI-INC/Omni- 
pl/node_modules/mongodb-core/lib/topologies/server.js:564:11)
at Pool.emit (events.js:182:13)
at Connection.<anonymous> (/Users/Travis/Desktop/Github_Repos/OMNI-INC/Omni- 
pl/node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:273:13)
at Connection.emit (events.js:182:13)
at TLSSocket.<anonymous> (/Users/Travis/Desktop/Github_Repos/OMNI-INC/Omni- 
pl/node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:273:13)
at TLSSocket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }

Response from connecting using MongoShell in terminal:在终端中使用 MongoShell 进行连接的响应:

connecting to: mongodb+srv://cluster0-efzkv.mongodb.net/test
2018-09-21T13:22:28.817-0400 I NETWORK  [js] Starting new replica set monitor 
for Cluster0-shard-0/cluster0-shard-00-02-efzkv.mongodb.net.:27017,cluster0- 
shard-00-00-efzkv.mongodb.net.:27017,cluster0-shard-00-01- 
efzkv.mongodb.net.:27017
2018-09-21T13:23:22.276-0400 W NETWORK  [js] Unable to reach primary for set 
Cluster0-shard-0
2018-09-21T13:23:22.276-0400 I NETWORK  [js] Cannot reach any nodes for set 
Cluster0-shard-0. Please check network connectivity and the status of the 
set. This has happened for 1 checks in a row.
2018-09-21T13:23:22.280-0400 E QUERY    [js] Error: connect failed to replica 
set Cluster0-shard-0/cluster0-shard-00-02-efzkv.mongodb.net.:27017,cluster0- 
shard-00-00-efzkv.mongodb.net.:27017,cluster0-shard-00-01- 
efzkv.mongodb.net.:27017 :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed

I have mongod running in a separate terminal like always.我像往常一样在单独的终端中运行 mongod。 Does anyone have any ideas on what might be the issue and how to fix based on the error response?有没有人对可能出现的问题以及如何根据错误响应进行修复有任何想法?

Any help would be great, thank you!任何帮助都会很棒,谢谢!

there are a few things you could try which helped me out in the same situation.在同样的情况下,您可以尝试一些帮助我解决问题的方法。

  1. Log into mongoDB Atlas and check if Atlas is working.登录 mongoDB Atlas 并检查 Atlas 是否正常工作。 If it's working go to the Security > IP-Whitelist and delete your current IP, then add a new one with your current IP.如果它正常工作,请转到安全 > IP 白名单并删除您当前的 IP,然后使用您当前的 IP 添加一个新 IP。 Maybe your IP changed because of your provider.也许您的 IP 因您的提供商而改变。 Also check if your mongoDB user is still valid, you can check it right next to the IP Whitelist tab.还要检查您的 mongoDB 用户是否仍然有效,您可以在 IP 白名单选项卡旁边检查它。
  2. Try to uninstall and reinstall your mongoose package.尝试卸载并重新安装您的猫鼬包。 Before reinstalling clean your npm cache.在重新安装之前清理你的 npm 缓存。
  3. Change your URI-String type to connect your application.更改您的 URI-String 类型以连接您的应用程序。 Change it from a "SRV connection string" to a "Standard connection string".将其从“SRV 连接字符串”更改为“标准连接字符串”。 In the Dashboard inside your cluster click on CONNECT > Connect your application > Standard connection string.在集群内的仪表板中,单击连接 > 连接您的应用程序 > 标准连接字符串。 Copy the string and replace the SRV-String in your application.复制字符串并替换应用程序中的 SRV-String。 This helped me out most of the time when I had issues connecting to Atlas.大多数时候,当我在连接到 Atlas 时遇到问题,这对我有帮助。
  4. Check that the internet connection you're using isn't blocking traffic/access to mongoDB atlas.检查您使用的互联网连接是否阻止流量/访问 mongoDB 图集。

There might be three reasons why you facing this error.您面临此错误的原因可能有三个。

  1. Check your user Password.检查您的用户密码。
  2. Check your Ip address, use your current IP address in network access -> actions -> edit -> add current IP address检查您的IP地址,在网络访问中使用您当前的IP地址->操作->编辑->添加当前IP地址
  1. Lastly check your database name in mongoClient.connect in your code.最后在代码中的 mongoClient.connect 中检查您的数据库名称。 Your database name in local storage may be different to that of cloud storage.您在本地存储中的数据库名称可能与云存储中的不同。

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

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