Connect Heroku app to Atlas MongoDB Cloud service

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


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

My current database is hosted in mLab (as a complement to Heroku), and the MongoDB URI used to access the cluster through mongoose (using xxx to omit sensitive information):

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

Now that I have 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"

When I launch my Heroku application locally on my machine, I can easily access the database. I can also connect to the cluster using the mongo shell.

However, when starting the application in Heroku, the connection could not be established. In the JS Browser console, I get a 503 service unavailability message. In heroku, I get an error message:

no primary found in replica set

I know that Atlas MongoDB Cloud requires an SSL connection, unlike mLab. On my local machine, I believe that a self-signed certificate is used to successfully connect to the cluster.

: SSL Heroku, Heroku MongoDB Atlas? SSL- Heroku /Heroku?

+4
2

, ,

: Heroku, MongoDB Atlas, .

Github, [1], , IP- MongoDB.

MongoDB Atlas [2], , Heroku, - 0.0.0.0/0 (.. ) MongoDB Atlas.

, .

SSL

SSL, , Heroku , , .

MongoDB , Node.js ( w630 > .js [3]):

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 [3]):

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

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

, Atlas [4] Node.js, , SSL 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();
});

[1] https://github.com/meteor/meteor/issues/7492#issuecomment-236562860
[2] https://docs.atlas.mongodb.com/security-whitelist/
[3] https://mongodb.imtqy.com/node-mongodb-native/2.2/tutorials/connect/ssl/
[4] https://docs.atlas.mongodb.com/driver-connection/#node-js-driver-example
+4

Source: https://habr.com/ru/post/1669367/


All Articles