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

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

问题描述

要解决这个问题:我是否需要在Heroku上获得 SSL 支持才能在 Heroku Atlas 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插件)上,用于通过猫鼬访问集群的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 App时,我可以毫无问题地访问数据库.我还可以使用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中运行该应用程序时,无法建立连接.在浏览器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需要与SSLab不同的SSL连接.在本地计算机中,我假设正在使用自签名证书来成功连接到群集.

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和MongoDB Atlas之间的安全连接,我是否需要在Heroku中获得SSL支持?还是仅在客户端/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代码必须类似于以下内容(取自

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证书,则可以简单地使用以下代码(也摘录自

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天全站免登陆