Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to a remote server mongoDB via ssh through mongoose in nodeJS using tunnel-ssh

I was trying to connect to a remote server mongoDB through SSH and made the configurations as provided

import tunnel from 'tunnel-ssh';

const config = {
    username: 'username',
    Password: 'password',
    host: process.env.SSH_SERVER, //192.168.9.104
    port: 22,
    dstHost: 'localhost',
    dstPort: process.env.DESTINATION_PORT, //27017
    localHost: '127.0.0.1',
    localPort: 27018
};

This is the config that has been defined where i need to connect to the remote server 192.168.9.104. So the particular is chosen as the SSH host. Username and password for the same is provided. and the connection made is as follows.

class DB {
    initDB() {
        tunnel(config, (error, server) => {
            if (error) {
                console.log('SSH connection error: ' + error);
            }

            const url = 'mongodb://127.0.0.1:27018/myDBname';
            mongoose.connect(url, { useNewUrlParser: true });
            mongoose.plugin(toJson);
            mongoose.plugin(setProperties);

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

But when the db.init() function is called following error pops up

events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: All configured authentication methods failed

I am not able to figure out where the config goes wrong. i have tried using 127.0.0.1 for dstHost. as well as put the 192.168.9.104 as the dstHost as well but the error persists. kevin lee suggests a similar approach. this question is used as an example

like image 886
anubysh Avatar asked Jul 10 '19 05:07

anubysh


1 Answers

There was an error with the documentation which suggested the config as mentioned above with the key "Password" but it should be "password" so the config would look something like this

const config = {
    username: 'username',
    password: 'password',
    host: process.env.SSH_SERVER, //192.168.9.104
    port: 22,
    dstHost: 'localhost',
    dstPort: process.env.DESTINATION_PORT, //27017
    localHost: '127.0.0.1',
    localPort: 27018
};

Rest of the implementation is spot on and tested.

like image 195
anubysh Avatar answered Sep 21 '22 07:09

anubysh