node.js 将 Heroku App 连接到 Atlas MongoDB Cloud 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42159175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connecting Heroku App to Atlas MongoDB Cloud service
提问by amaralbf
To antecipate the question: do I need to get SSLsupport on Heroku in order to establish a connection between Herokuand Atlas MongoDB Cloudusing SSL? (TSL/SSL connection is a requirementto access Atlas MongoDB Cloud service).
预测这个问题:我是否需要在 Heroku上获得SSL支持才能使用 SSL在Heroku和Atlas MongoDB Cloud之间建立连接?(访问 Atlas MongoDB Cloud 服务需要TSL/SSL 连接)。
I am trying to connect my Heroku App, written in node.js, to a cluster hosted at Atlas MongoDB Cloud.
我正在尝试将用 node.js 编写的 Heroku 应用程序连接到托管在 Atlas MongoDB Cloud 上的集群。
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 xxxto omit confidential info):
我当前的数据库托管在 mLab(作为 Heroku 附加组件),用于通过 mongoose 访问集群的 MongoDB URI 是(使用xxx省略机密信息):
MONGODB_URI="mongodb://xxx:[email protected]:23266,xxx-a1.mlab.com:xxx/xxx?replicaSet=rs-xxx"
Now that I've migrated my data from mLab to Atlas MongoDB Cloud, I am currently accessing the cluster using the URI:
现在我已将数据从 mLab 迁移到 Atlas MongoDB Cloud,我目前正在使用 URI 访问集群:
MONGODB_URI="mongodb://xxx:[email protected]:xxx,cluster0-shard-xxx.mongodb.net:xxx,cluster0-shard-xxx.mongodb.net:xxx/xxx?replicaSet=xxx&ssl=true&authSource=admin"
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 应用程序时,我可以毫无问题地访问数据库。我还可以使用 mongo shell 连接到集群。
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:
但是,在 Heroku 中运行该应用程序时,无法建立连接。在浏览器 JS 控制台中,我收到 503 服务不可用消息。在heroku中,我收到错误:
no primary found in replica set
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.
我知道 Atlas MongoDB Cloud 需要 SSL 连接,这与 mLab 不同。在我的本地机器上,我假设使用自签名证书成功连接到集群。
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 中获得 SSL 支持才能访问在 Heroku 和 MongoDB Atlas 之间建立安全连接?或者 Heroku 中的 SSL 支持只需要客户端/Heroku 安全连接?
回答by Niklas Wenzel
What I think might fix your problem
我认为可能会解决您的问题
Disclaimer:I have used neither Heroku nor MongoDB Atlas but I am looking into them.
免责声明:我既没有使用过 Heroku 也没有使用过 MongoDB Atlas,但我正在研究它们。
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.
根据我发现的 Github 问题,如果您尚未将 MongoDB Atlas 中的服务器 IP 地址列入白名单,您将收到该错误消息。
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.
阅读MongoDB Atlas 文档,我看到与 Heroku dynos 结合使用的唯一方法是将0.0.0.0/0(即所有地址)添加到您的 MongoDB Atlas 白名单。
Give that a try and please report back whether you can instantiate a connection.
尝试一下,请报告您是否可以实例化连接。
On SSL
在 SSL 上
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.
试图回答 SSL 问题,我认为您不需要根据我阅读的内容在 Heroku 上启用它,尽管我不完全确定。
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):
如果 MongoDB 服务器执行证书验证,则用于连接到它的 Node.js 代码必须如下所示(取自Node.js 驱动程序文档):
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();
});
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):
如果 MongoDB 服务器不检查任何 SSL 证书,您可以简单地使用如下代码(也取自Node.js 驱动程序文档):
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost:27017/test?ssl=true", function(err, db) {
db.close();
});
Given that the Atlas documentationcontains the following example code for connecting to it from Node.js, I think that you do nothave to enable SSL on Heroku:
鉴于阿特拉斯文档包含了连接到它从Node.js的下面的示例代码,我认为你不是要在Heroku上启用SSL:
var MongoClient = require('mongodb').MongoClient;
var uri = "mongodb://kay:[email protected]: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();
});
回答by metakungfu
I solved this by installing an addon(i used Fixie Socks) for Static IP addresses for database requests and other TCP connections. More options here: https://elements.heroku.com/addons#network
我通过为数据库请求和其他 TCP 连接的静态 IP 地址安装一个插件(我使用Fixie Socks)解决了这个问题。更多选项:https: //elements.heroku.com/addons#network
回答by yehonatan yehezkel
very simple solution! just add to the white list IP in mongo atlas the adress "0.0.0.0/0"
非常简单的解决方案!只需将地址“0.0.0.0/0”添加到mongo图集中的白名单IP
it will open the mongo atlas to all the world..... so it os not for production but it helps for small tests
它将向全世界开放 mongo 图集......所以它不是用于生产,但它有助于小型测试
回答by Abi
Also had to add 0.0.0.0/0 to the Mongo IP whitelist AND redeploy my app on Heroku for it to finally work (before changing IP, a CORS error was thrown).
还必须将 0.0.0.0/0 添加到 Mongo IP 白名单并在 Heroku 上重新部署我的应用程序才能使其最终工作(在更改 IP 之前,引发了 CORS 错误)。

