【问题标题】:Couldn't connect to mongodb on mongodb-atlas无法连接到 mongodb-atlas 上的 mongodb
【发布时间】:2019-10-31 17:08:29
【问题描述】:

尝试连接到 mongodb-atlas 上的 mongodb 免费数据库 我几乎尝试了所有检查文档,更改了网址,更改密码,甚至删除了用户并创建了新用户,但仍然没有解决问题

这里是 myurl.js 文件

module.exports = {
    mongoURL: "mongodb+srv://nansDB:nansDB123@nodecluster-qs6cv.mongodb.net/test?retryWrites=true&w=majority",
    secret: "mystrongsecret"
}

这是我的 index.js 文件

const express = require('express');
const mongoose = require('mongoose');
const app = express();

//mongodb Configuration
const db = require('./setup/myurl').mongoURL;

//attempt to connect to database
mongoose.connect(db,  { useNewUrlParser: true })
        .then(()=> console.log('MongoDb Connect successfully'))
        .catch(err => console.log(err));

const port = process.env.PORT || 3000;

这是我尝试运行时遇到的错误行

{ MongoNetworkError: failed to connect to server [nodecluster-shard-00-02-qs6cv.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to nodecluster-shard-00-02-qs6cv.mongodb.net:27017 closed]
    at Pool.<anonymous> (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\topologies\server.js:431:11)
    at Pool.emit (events.js:189:13)
    at connect (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\connection\pool.js:557:14)
    at callback (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\connection\connect.js:109:5)
    at runCommand (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\connection\connect.js:129:7)
    at Connection.errorHandler (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\connection\connect.js:321:5)
    at Object.onceWrapper (events.js:277:13)
    at Connection.emit (events.js:189:13)
    at TLSSocket.<anonymous> (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\connection\connection.js:350:12)
    at Object.onceWrapper (events.js:277:13)
    at TLSSocket.emit (events.js:189:13)
    at _handle.close (net.js:597:12)
    at TCP.done (_tls_wrap.js:388:7)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }

【问题讨论】:

  • 您是否在安全选项卡上设置了允许的 ip 地址?
  • 可能没有将您的 ip 列入白名单。还要检查我的答案:stackoverflow.com/a/56586161/11330560
  • 是的,我已将我的 ip 列入白名单
  • 请帮助我@ArdyFebriansyah
  • 感谢@MatinSasan 实际上由于连接问题重新连接到 wifi,白名单 ip 已更改

标签: node.js mongodb mongoose mongodb-atlas


【解决方案1】:

它实际上解决了我的白名单 ip 由于连接问题 [重新连接到 wifi] 而被更改。虽然我的建议是检查您列入白名单的 ip 是否存在。

【讨论】:

    猜你喜欢
    • 2021-09-20
    • 2019-06-08
    • 2020-09-13
    • 2019-10-13
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    相关资源
    最近更新 更多