无法连接到 MongoDB atlas 数据库 [英] Not able to connect to MongoDB atlas database

查看:72
本文介绍了无法连接到 MongoDB atlas 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 A2 共享托管服务器来托管我的 node express 应用程序.我提供以下 URI 以连接到猫鼬.

I am using A2 shared hosting server to host my node express application. I am giving the following URI to connect to mongoose.

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 地址列入白名单.然后,当我运行 npm start 时,它也会出现以下错误:

I have whitelisted the public as well as private IP address of the host. Then also when I run an npm start it gives the following error :

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 列入白名单.当我在本地运行相同的代码时,它会连接到数据库.它只是不能在共享服务器上工作并给出上述错误.如何纠正?

I have also whitelisted the public IP of my laptop. When I run the same code locally, it connects to database. Its just not working on the shared server and gives above error. How to correct this ?

推荐答案

所以我能够让它发挥作用.实际上,我的共享托管服务器阻止了用于 mongodb 的端口 27017.代码或配置没有任何问题.他们启用了端口并且它起作用了.

So I was able to make it work. Actually my shared hosting server has blocked the port 27017 which is used for mongodb. Nothing was wrong with the code or configuration. They enabled the port and it worked.

这篇关于无法连接到 MongoDB atlas 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆