将 Heroku App 连接到 Atlas MongoDB Cloud 服务 [英] Connecting Heroku App to Atlas MongoDB Cloud service

查看:26
本文介绍了将 Heroku App 连接到 Atlas MongoDB Cloud 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

预测问题:我是否需要在 Heroku 上获得 SSL 支持才能在 HerokuAtlas MongoDB Cloud 之间建立连接使用 SSL?(TSL/SSL 连接是要求 访问 Atlas MongoDB Cloud 服务).

To antecipate the question: do I need to get SSL support on Heroku in order to establish a connection between Heroku and Atlas MongoDB Cloud using SSL? (TSL/SSL connection is a requirement to access Atlas MongoDB Cloud service).


我正在尝试将用 node.js 编写的 Heroku 应用程序连接到托管在 Atlas MongoDB Cloud 上的集群.

I am trying to connect my Heroku App, written in node.js, to a cluster hosted at Atlas MongoDB Cloud.

我当前的数据库托管在 mLab(作为 Heroku 附加组件),用于通过 mongoose 访问集群的 MongoDB URI 是(使用 xxx 以省略机密信息):

My current database is hosted at mLab (as a Heroku Add-on), and the MongoDB URI used to access the cluster through mongoose is (using xxx to omit confidential info):

MONGODB_URI="mongodb://xxx:xxx@xxx-a0.mlab.com:23266,xxx-a1.mlab.com:xxx/xxx?replicaSet=rs-xxx"

现在我已将数据从 mLab 迁移到 Atlas MongoDB Cloud,我目前正在使用 URI 访问集群:

Now that I've migrated my data from mLab to Atlas MongoDB Cloud, I am currently accessing the cluster using the URI:

MONGODB_URI="mongodb://xxx:xxx@cluster0-shard-xxx.mongodb.net:xxx,cluster0-shard-xxx.mongodb.net:xxx,cluster0-shard-xxx.mongodb.net:xxx/xxx?replicaSet=xxx&ssl=true&authSource=admin"

在我的机器上本地运行我的 Heroku 应用程序时,我可以毫无问题地访问数据库.我还可以使用 mongo shell 连接到集群.

When running my Heroku App locally in my machine I can access the database with no problem. I'm also able to connect to the cluster using mongo shell.

但是,在 Heroku 中运行 App 时,无法建立连接.在浏览器 JS 控制台中,我收到 503 服务不可用消息.在heroku中,我收到错误:

However, when running the App in Heroku, the connection cannot be established. In the Browser JS console, I get the 503 service unavailable message. In heroku, I get the error:

no primary found in replica set

我知道 Atlas MongoDB Cloud 需要 SSL 连接,这与 mLab 不同.在我的本地机器上,我想使用自签名证书成功连接到集群.

I am aware that Atlas MongoDB Cloud requires SSL connection, differently from mLab. In my local machine, I suppose a self signed certificate is being used to connect successfully to the cluster.

我的问题是:我是否需要在 Heroku 中获得 SSL 支持才能访问在 Heroku 和 MongoDB Atlas 之间建立安全连接?还是只有客户端/Heroku 安全连接才需要 Heroku 中的 SSL 支持?

My question is: do I need to get SSL support in Heroku in order to be able to access establish the secure connection between Heroku and MongoDB Atlas? Or the SSL suport in Heroku is only required to client/Heroku secure connection?

推荐答案

我认为可能会解决您的问题

免责声明:我既没有使用过 Heroku 也没有使用过 MongoDB Atlas,但我正在研究它们.

What I think might fix your problem

Disclaimer: I have used neither Heroku nor MongoDB Atlas but I am looking into them.

根据我发现的一个 Github 问题,你会明白如果您尚未将 MongoDB Atlas 中的服务器 IP 地址列入白名单,则会出现错误消息.

According to a Github issue I found, you will get that error message if you haven't whitelisted the server IP addresses in MongoDB Atlas.

阅读 MongoDB Atlas 文档,这是我看到的唯一方法与 Heroku dynos 结合是将 0.0.0.0/0(即所有地址)添加到您的 MongoDB Atlas 白名单中.

Reading the MongoDB Atlas docs, the only way I see to do this in combination with Heroku dynos is to add 0.0.0.0/0 (i.e. all addresses) to your MongoDB Atlas whitelist.

试试看,请报告您是否可以实例化连接.

Give that a try and please report back whether you can instantiate a connection.

尝试回复 SSL 问题,根据我的阅读,我认为您不需要在 Heroku 上启用它,尽管我不完全确定.

Trying to reply to the SSL question, I do not think that you need to enable it on Heroku based on what I read, although I am not totally sure.

如果 MongoDB 服务器执行证书验证,则用于连接到它的 Node.js 代码必须如下所示(取自 Node.js 驱动程序文档):

If the MongoDB server performed certificate validation, the Node.js code for connecting to it would have to look like the following (taken from the Node.js driver documentation):

var MongoClient = require('mongodb').MongoClient,
  f = require('util').format,
  fs = require('fs');

// Read the certificates
var ca = [fs.readFileSync(__dirname + "/ssl/ca.pem")];
var cert = fs.readFileSync(__dirname + "/ssl/client.pem");
var key = fs.readFileSync(__dirname + "/ssl/client.pem");

// Connect validating the returned certificates from the server
MongoClient.connect("mongodb://localhost:27017/test?ssl=true", {
  server: {
      sslValidate:true
    , sslCA:ca
    , sslKey:key
    , sslCert:cert
    , sslPass:'10gen'
  }
}, function(err, db) {
  db.close();
});

如果 MongoDB 服务器不检查任何 SSL 证书,您可以简单地使用如下代码(也取自 Node.js 驱动程序文档):

If the MongoDB server does not check for any SSL certificates, you can simply use code like the following (also taken from the Node.js driver documentation):

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

MongoClient.connect("mongodb://localhost:27017/test?ssl=true", function(err, db) {
  db.close();
});

鉴于 Atlas 文档 包含以下示例代码用于从 Node.js 连接到它,我认为您不必必须在 Heroku 上启用 SSL:

Given that the Atlas documentation contains the following example code for connecting to it from Node.js, I think that you do not have to enable SSL on Heroku:

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

var uri = "mongodb://kay:myRealPassword@mycluster0-shard-00-00-wpeiv.mongodb.net:27017,mycluster0-shard-00-01-wpeiv.mongodb.net:27017,mycluster0-shard-00-02-wpeiv.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin";
MongoClient.connect(uri, function(err, db) {
  db.close();
});

这篇关于将 Heroku App 连接到 Atlas MongoDB Cloud 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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