Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to mongodb through ssh username/password through nodejs

I am trying to connect to Mongodb through ssh username/password through nodejs as below :

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

var config = {
username : 'xyz',
host: 'xx-xxx-xx.com',
port:22,
password:'xxx',
dstPort:27017,
localPort:27017
};

console.log(tunnel);
var server = tunnel(config, function (error, server) {

if(error){
    console.log("SSH connection error: " + error);
}
console.log(server);
mongoose.connect('mongodb://localhost:27017/myDB');

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

But I am getting error as below : DB connection error: { MongoError: connection 0 to localhost:27017 timed out

What is the issue here?

like image 832
Sowmya Avatar asked Oct 24 '25 13:10

Sowmya


1 Answers

Probably is because you are missing the 'dstHost'in the config options.

The 'dstHost' must be the destination server url.

from official tunnel-ssh doc

 var config = {
      username:'root',
      Password:'secret',
      host:sshServer,
      port:22,
      dstHost:destinationServer,
      dstPort:27017,
      localHost:'127.0.0.1',
      localPort: 27000
    };

    var tunnel = require('tunnel-ssh');
    tunnel(config, function (error, server) {
      //.... 
    });
like image 95
gzp___ Avatar answered Oct 27 '25 02:10

gzp___



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!