无法在Mongodb端口27017上连接

时间:2019-06-23 16:14:32

标签: mongodb

我从哪里冒出来这个错误。

{ MongoNetworkError: failed to connect to server [cluster0-shard-00-01-erhon.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to cluster0-shard-00-01-erhon.mongodb.net:27017 closed]
    at Pool.<anonymous> (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/topologies/server.js:431:11)
    at Pool.emit (events.js:189:13)
    at connect (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/pool.js:557:14)
    at callback (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connect.js:109:5)
    at runCommand (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connect.js:129:7)
    at Connection.errorHandler (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/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> (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connection.js:350:12)
    at Object.onceWrapper (events.js:277:13)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }
(node:1854) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [cluster0-shard-00-01-erhon.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to cluster0-shard-00-01-erhon.mongodb.net:27017 closed]
    at Pool.<anonymous> (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/topologies/server.js:431:11)
    at Pool.emit (events.js:189:13)
    at connect (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/pool.js:557:14)
    at callback (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connect.js:109:5)
    at runCommand (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connect.js:129:7)
    at Connection.errorHandler (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/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> (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connection.js:350:12)
    at Object.onceWrapper (events.js:277:13)
(node:1854) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1854) [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.
[nodemon] clean exit - waiting for changes before restart

我正在使用Mongo Atlas(这意味着我已经安装了mongodb),这是我与mongodb进行连接的方式。

const mongoConnect = callback => {
   MongoClient
   .connect('mongodb+srv://theodosiostziomakas:password_goes_here@cluster0-erhon.mongodb.net/shop?retryWrites=true&w=majority',
   {useNewUrlParser: true}
   )
   .then(client => {
      console.log('Connected!');
      _db = client.db();
      callback();
   })
   .catch(err => {
      console.log(err);
      throw err;
   });
}

您也可以从此repo下载我的项目。

如何解决此错误?我还查看了此topic,但找不到解决方案。

谢谢, 西奥。

1 个答案:

答案 0 :(得分:1)

此类错误通常是 IP问题

首先,您必须将IP列入白名单。 即使您提到确实将IP列入白名单,也必须有一些外部因素影响,更改IP /端口(重新连接wifi,ISP等)< / p>

要了解故障是否仅在您的代码中,请尝试通过MongoDB Compass连接MongoDB集群。如果连接,则来自您的代码。

还要确保您没有使用自己的MongoDB用户名和密码,而是使用在Security -> Database Access中创建的用户名和密码。


链接到恰好遇到您的错误的其他问题,这些问题最终都是IP问题:

Mongoose is not connecting MongoDB Atlas (the answer)

Couldn't connect to mongodb on mongodb-atlas(the answer)