无法连接到MongoDB Atlas数据库

时间:2020-06-22 20:01:03

标签: node.js mongodb mongoose shared-hosting mongodb-atlas

我正在使用A2共享托管服务器来托管我的节点快速应用程序。我提供了以下URI以连接到猫鼬。

const uri = mongodb+srv://<username>:<password>@cluster0-gwff9.gcp.mongodb.net/customer-data?retryWrites=true&w=majority

和猫鼬连接功能是:

mongoose.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true}).then(() => {
console.log("Successfully connected to the database");    }).catch(err => {
console.log('Could not connect to the database. Exiting now...', err);
process.exit();});

我已将主机的公共IP地址和私有IP地址列入白名单。然后,当我运行npm start时,也会出现以下错误:

Could not connect to the database. Exiting now... { MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
    at NativeConnection.Connection.openUri (/home/cleanlab/apps/prelaunch/node_modules/mongoose/lib/connection.js:826:32)
    at Mongoose.connect (/home/cleanlab/apps/prelaunch/node_modules/mongoose/lib/index.js:335:15)
    at Object.<anonymous> (/home/cleanlab/apps/prelaunch/server/model/mongodb.js:5:10)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Module.require (internal/modules/cjs/loader.js:650:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/home/cleanlab/apps/prelaunch/server/server.js:22:1)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
  message:
   'Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you\'re trying to access the database from an IP that isn\'t whitelisted. Make sure your current IP address is on your Atlas cluster\'s IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/',
  reason:
   TopologyDescription {
     type: 'ReplicaSetNoPrimary',
     setName: null,
     maxSetVersion: null,
     maxElectionId: null,
     servers:
      Map {
        'cluster0-shard-00-02-gwff9.gcp.mongodb.net:27017' => [ServerDescription],
        'cluster0-shard-00-01-gwff9.gcp.mongodb.net:27017' => [ServerDescription],
        'cluster0-shard-00-00-gwff9.gcp.mongodb.net:27017' => [ServerDescription] },
     stale: false,
     compatible: true,
     compatibilityError: null,
     logicalSessionTimeoutMinutes: null,
     heartbeatFrequencyMS: 10000,
     localThresholdMS: 15,
     commonWireVersion: null } }

我还将笔记本电脑的公共IP列入了白名单。当我在本地运行相同的代码时,它会连接到数据库。它只是不能在共享服务器上工作,并给出上述错误。如何纠正呢?

2 个答案:

答案 0 :(得分:0)

我有和你一样的问题。但是在使用谷歌的DNS服务器(8.8.8.8,8.8.4.4)并将我的IP地址列入白名单之后,我设法连接到了MongoDB Atlas。 希望能解决您的问题

答案 1 :(得分:0)

所以我能够使它工作。实际上,我的共享托管服务器已阻止用于mongodb的端口27017。代码或配置没有错。他们启用了端口,并且可以正常工作。