jeremytripp jeremytripp - 1 year ago 449
Node.js Question

Node.js SSH Tunneling to MongoDB using Mongoose

I have two Mongo DBs. One for my dev environment, one for production as seen here from my Robomongo setup:

Two Mongo DBs

The production db is SSH tunneled to my Digital Ocean virtual server as seen here (specifics have been changed obviously). This setup works fine when connecting to/from my production website:

SSH Tunnel Settings

I am now working on a different/related project and need to connect my local machine to my production DB, so I assumed I'd need to use something like tunnel-ssh to get it done. I've followed this answer as an example, but I'm either getting

Error: (SSH) Channel open failure: open failed
OR it's connecting to my Dev db (when I use 27017 as my dstHost/dstPort/localPort). I must be thinking about this wrong or I'm being dumb with my configuration. I am admittedly a total Mongo/Mongoose novice, so both are equally possible. Any advice?

var fs = require("fs");
var mongoose = require('mongoose');
var tunnel = require('tunnel-ssh');

//===== db connection =====

var config = {
agent : process.env.SSH_AUTH_SOCK,
localPort: 27000

var server = tunnel(config, function (error, server) {
console.log("SSH connection error: " + error);

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
// we're connected!
console.log("DB connection successful");
// console.log(server);

Thanks in advance!

Answer Source

dstHost needs to be a hostname/IP, not a MongoDB connection string. In this particular case, you can actually omit dstHost, localHost, and localPort in this particular case because they already default to the values you're providing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download