MongoNetworkError node.js

时间:2019-09-11 02:43:15

标签: node.js mongodb server

Server up and running on port 5000 !
{ MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [MongoError: Authentication failed.]
    at Pool.<anonymous> (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/topologies/server.js:431:11)
    at Pool.emit (events.js:198:13)
    at connect (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/pool.js:580:14)
    at callback (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/connect.js:109:5)
    at provider.auth.err (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/connect.js:352:21)
    at _authenticateSingleConnection (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/auth/auth_provider.js:66:11)
    at sendAuthCommand (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/auth/scram.js:177:16)
    at Connection.messageHandler (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/connect.js:334:5)
    at Connection.emit (events.js:198:13)
    at processMessage (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/connection.js:364:10)
    at Socket.<anonymous> (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/connection.js:533:15)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }

3 个答案:

答案 0 :(得分:0)

如错误所示,请使用正确的用户名和密码

答案 1 :(得分:0)

确保mongodb服务器正在运行

var MongoClient = require('mongodb').MongoClient;

var url = "mongodb://localhost:27017/mydb";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
      console.log("Database connected");     
});

答案 2 :(得分:0)

MongoError: Authentication failed是无效凭据的错误。请使用您的用户名和密码进行安全连接。

mongoose.connect('mongodb://username:password@host:port/database');