Connecting Heroku App to Atlas MongoDB Cloud service

amaralbf :

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).


I am trying to connect my Heroku App, written in node.js, to a cluster hosted at 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 xxx to omit confidential info):

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:

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.

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

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.

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?

Niklas Wenzel :

What I think might fix your problem

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

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.

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.

On 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.

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();
});

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();
});

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:[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();
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MongoDb Atlas not connecting to Heroku

From Dev

App is not connecting to the mongodb Atlas

From Dev

Heroku not connecting to MongoDB Atlas - Error 405 not allowed

From

MongoDB Atlas not connecting with my golang App

From Dev

Problem with deploying a MERN app on Heroku with MongoDB Atlas

From Dev

Connecting to MongoDB Atlas with TypeOrm?

From Dev

Mongoose is not connecting MongoDB Atlas

From Dev

Heroku Not Connecting to Mongo Atlas DB?

From Dev

Connecting Google Cloud Memorystore with App Engine service

From

Error at connecting to MongoDb Atlas Server

From Dev

connecting mongoose to mongoDB atlas and nodejs

From Dev

MongoDB Compass - stuck on connecting to Atlas

From Dev

MongoDB Atlas with NodeJS using Mongodb is not connecting

From Dev

How to connect heroku to MongoDB Atlas

From Dev

Connecting app to ParseServer on Heroku

From Dev

Error connecting to Atlas MongoDB with Node JS

From Dev

Loopback API connecting to MongoDB Atlas, fails

From Dev

MongoDB Atlas with NodeJS using Mongoose is not connecting

From Dev

cPanel Node.js app cannot connect to MongoDB Atlas cluster but works on Heroku

From Dev

MongoDB Atlas connection working but not displaying data in Heroku

From Dev

MongoDB Atlas connection doesn't work on Heroku

From Dev

Connecting Heroku App to outside database

From Dev

Recommended way to connect cloud foundry to mongodb atlas

From Dev

Add local server to MongoDB Atlas cloud instance

From Dev

MongoDB Atlas/Cloud Update and Delete not working

From Dev

Connect Azure Cognitive Search to MongoDB Cloud Atlas

From Dev

Trouble connecting to MongoDB Atlas from Robo 3T

From Dev

How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas?

From Dev

node.js hangs when connecting to mongodb atlas

Related Related

  1. 1

    MongoDb Atlas not connecting to Heroku

  2. 2

    App is not connecting to the mongodb Atlas

  3. 3

    Heroku not connecting to MongoDB Atlas - Error 405 not allowed

  4. 4

    MongoDB Atlas not connecting with my golang App

  5. 5

    Problem with deploying a MERN app on Heroku with MongoDB Atlas

  6. 6

    Connecting to MongoDB Atlas with TypeOrm?

  7. 7

    Mongoose is not connecting MongoDB Atlas

  8. 8

    Heroku Not Connecting to Mongo Atlas DB?

  9. 9

    Connecting Google Cloud Memorystore with App Engine service

  10. 10

    Error at connecting to MongoDb Atlas Server

  11. 11

    connecting mongoose to mongoDB atlas and nodejs

  12. 12

    MongoDB Compass - stuck on connecting to Atlas

  13. 13

    MongoDB Atlas with NodeJS using Mongodb is not connecting

  14. 14

    How to connect heroku to MongoDB Atlas

  15. 15

    Connecting app to ParseServer on Heroku

  16. 16

    Error connecting to Atlas MongoDB with Node JS

  17. 17

    Loopback API connecting to MongoDB Atlas, fails

  18. 18

    MongoDB Atlas with NodeJS using Mongoose is not connecting

  19. 19

    cPanel Node.js app cannot connect to MongoDB Atlas cluster but works on Heroku

  20. 20

    MongoDB Atlas connection working but not displaying data in Heroku

  21. 21

    MongoDB Atlas connection doesn't work on Heroku

  22. 22

    Connecting Heroku App to outside database

  23. 23

    Recommended way to connect cloud foundry to mongodb atlas

  24. 24

    Add local server to MongoDB Atlas cloud instance

  25. 25

    MongoDB Atlas/Cloud Update and Delete not working

  26. 26

    Connect Azure Cognitive Search to MongoDB Cloud Atlas

  27. 27

    Trouble connecting to MongoDB Atlas from Robo 3T

  28. 28

    How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas?

  29. 29

    node.js hangs when connecting to mongodb atlas

HotTag

Archive