Unable to Connect to MongoDB in Node App

Posted January 22, 2017 11.6k views


I’m trying to get my Node app (separate droplet) to connect to my mongoDB droplet.

I’ve found a npm called tunnel-ssh however am having trouble.

It says “DB connection successful”, however data is not coming back. And when I do a console.log(mongoose) it shows the host and host as null.

If I do console.log(mongoose), after the console.log(“DB connection successful”); then it shows me the host.

Anyhow, my code looks like:

var tunnel = require(‘tunnel-ssh’);

var config = {
agent : 'myuser’,
host: 'xxx:xxx:xxx:xxx’
agent : process.env.SSHAUTHSOCK,
keepAlive: true

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() {
    console.log("DB connection successful");


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers

I’m having the SAME issue. Everything works perfectly on cloud9, but I can’t get mongo to connect to the app. Everything in the api folder (all the nodejs files) sends a 400 error bad request - not sure it’s mongo as much as connecting to the non-public parts of the server (which connect to the db). But I don’t know how to fix it, either. Did you find a resolution?


I got it all working however i can’t remember exactly what I did however from what i briefly remember it was something around the port numbers. I think i had them mixed up.
My final config var looks slightly different to what I have above - it’s currently like:

var config = {
      username : 'xxxxxxx',
      host: '',
      localPort: 2xxx0

I’d suggest try debugging around the 3 port numbers (port, dstPort, localPort).

Good luck!