Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

383
Views
Connection to MongoDB with SSH in my Node App

My DB is with DigitalOcean and I'm trying to connect to it in my node app.

I've found a npm called tunnel-ssh however am having trouble connecting to it. My code is below.

It says DB connection successful, however 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.

var tunnel = require('tunnel-ssh');

var config = {
    agent : 'myuser',
    host: 'xxx:xxx:xxx:xxx'
    agent : process.env.SSH_AUTH_SOCK,
    privateKey:require('fs').readFileSync('id_rsa'),
    port:22,
    dstPort:27010,
    keepAlive: true
};

var server = tunnel(config, function (error, server) {

    if(error){
        console.log("SSH connection error: " + error);
    }

    mongoose.connect('mongodb://127.0.0.1:27017/mysuperdb');

    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'DB connection error:'));
    db.once('open', function() {
        console.log("DB connection successful");
    });

});
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Here's the working code:

var tunnel = require('tunnel-ssh');

var config = {
    username : 'myuser',
    host: 'xxx:xxx:xxx:xxx',
    privateKey:require('fs').readFileSync('id_rsa'),
    port:22,
    dstPort:27010,
    localPort: 2000
};

var server = tunnel(config, function (error, server) {

    if(error){
        console.log("SSH connection error: " + error);
    }

    mongoose.connect('mongodb://127.0.0.1:2000/mysuperdb');

    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'DB connection error:'));
    db.once('open', function() {
        console.log("DB connection successful");
    });

});
over 4 years ago · Santiago Trujillo Report

0

Your ports aren't matching up. You have dstPort: 27010 but your connection string is 'mongodb://127.0.0.1:27017/mysuperdb'. One or the other needs to be corrected.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!