在mongodb-atlas上无法连接到mongodb

时间:2019-06-18 06:05:49

标签: node.js mongodb mongoose mongodb-atlas

尝试连接到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)]: {} }

1 个答案:

答案 0 :(得分:1)

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