6

I have a mongodb database hosted on an Atlas MongoDB Cloud cluster. I'm currently accessing the database in my node.js application using mongoose:

mongoose.connect("mongodb://user:pw@cluster0-shard-00-00-***.mongodb.net:***,cluster0-shard-00-01-***.mongodb.net:***,cluster0-shard-00-02-***.mongodb.net:***/admin?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin");

Because Atlas MongoDB Cloud have a whitelist, and Heroku doesn't provide the possibility to obtain a fixed IP address for my application, I'm using Fixie add-on. Basically Fixie acts as a proxy for outbound traffic.

This way, I can request resources via HTTP, which are tunneled through a fixed IP address provided by Fixie. But what I do need is to connect to the Atlas Cloud cluster using Fixie's proxy, in order to retrieve and modify data from the database.

Can it be done using mongoose?

The mongoose.connect function accepts an option parameter, but I couldn't find any option regarding the establishment of a connection through a proxy.

amaralbf
  • 900
  • 1
  • 6
  • 12

2 Answers2

11

Just got a reply from Fixie's team:

Fixie is an http/https proxy, so it won't work for lower-level TCP connections like you'd need for your mongodb connection

When I asked about the possibility of using SOCKS for this case, they replied:

It looks like mongoose does not natively support socks proxies, and it does not accept a custom socket (which is how Node database drivers for MySQL and Postgres support it).

So apparently, in my case, there is no way to establish a connection to MongoDB Atlas cluster using mongoose through the proxy solution offered by Fixie (Heroku Add-on).

amaralbf
  • 900
  • 1
  • 6
  • 12
  • 4
    I'm in the same situation (Heroku + Node/Express + Mongoose + MongoDB Atlas). The two current options I'm aware of are 1) allowing MongoDB Atlas to accept traffic from `0.0.0.0/0` (all IP addresses), or 2) get a Heroku Private Space, which has a static IP option (requires their enterprise edition, which rumor has starting at >$1k/mo). It looks like Mongoose's `node-mongodb-native/connection` driver is where it'd need to accept a custom socket connection. – Pete Jul 21 '17 at 19:30
  • @ykorach I had to whitelist my mongo network ip to accept traffic `0.0.0.0/0`. Since this is a sandbox app, i don't really care. I may in the future move to aws on a ec2 to get a static ip. – drew578 Jan 23 '20 at 17:26
  • @drew578 if you whitelist to accept 0.0.0.0/0 you are open it to the entire internet so there is no point to use Fixie to get static IP. I ended up with deploying to AWS's ec2 instance. – ykorach Jan 26 '20 at 10:34
  • 1
    @ykorach Fixie now has another addon called Fixie Socks, which DOES accept TCP traffic. I'm just trying to figure out how to actually use it to connect to my Mongo instance. – paulwithap Mar 24 '20 at 16:58
  • @paulwithap did you manage to use Fixie Socks to connect to Mongo ? I'm trying to use it, but can't figure out how to make it work. Not sure what changes I need to make to my node.js api. – matrix4use Apr 22 '20 at 21:34
  • 1
    @matrix4use Nope - we had no luck with that after a lot of time spent trying to figure it out. The only thing I was able to get working was something like the last example in this article: https://blog.apify.com/tunneling-arbitrary-protocols-over-http-proxy-with-static-ip-address-b3a2222191ff. However, you need to use regular Fixie, not Fixie Socks for this to work. – paulwithap Apr 24 '20 at 20:47
0

A lot has changed since this question was originally asked in 2017. Since then, Mongoose has added support for proxy options, and Fixie released Fixie Socks, a SOCKS5 proxy designed for proxying database connections.

To connect to a MongoDB server (including MondoDB Atlas) through Fixie Socks, you can do the following:

const mongoose = require('mongoose');
const fixieData = process.env.FIXIE_SOCKS_HOST.split(new RegExp('[/(:\\/@/]+'));

mongoose.connect(process.env.DB_CONNECTION,
    {
      proxyUsername: fixieData[0],
      proxyPassword: fixieData[1],
      proxyHost: fixieData[2],
      proxyPort: fixieData[3]
     },
    (error) => {
      if(error){
        console.log(error);
      } else {
        console.log('Connected to database');
      }
    }
);
Fixie
  • 21
  • 3