【问题标题】:Can not connect on Mongodb port 27017无法连接到 Mongodb 端口 27017
【发布时间】:2019-11-05 14:51:28
【问题描述】:

不知从何而来,我得到了这个错误。

{ 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,但找不到解决方案。

谢谢, 西奥。

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    此类错误通常是IP问题

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

    要知道故障是否仅在于您的代码,请尝试通过MongoDB Compass 连接您的 MongoDB 集群。如果它连接了,那么它来自您的代码。

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


    其他问题的链接正是你的错误,最终都是IP问题:

    Mongoose is not connecting MongoDB Atlas (the answer)

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

    【讨论】:

      猜你喜欢
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      相关资源
      最近更新 更多