How to use Node.js to make a SSH tunneling connection to a MongoDB database

后端 未结 3 1540
栀梦
栀梦 2020-12-15 14:49

My credentials work perfectly with Robomongo but I can\'t make the connection with node.js
I have tried to make the connection using ssh2 and tunnel-ssh npm module and f

3条回答
  •  执念已碎
    2020-12-15 15:31

    As mscdex mentioned ssh2 isn't a good module to use to make an ssh tunnel connection to a database. tunnel-ssh is more appropriate.

    Here are the configuration options I've used :

    dstPort: remote database connection port

    localPort: same as dstPort, It'll be the port you'll use for your local machine

    username: SSH username,

    host: SSH address

    dstHost: database connection url (...mongodbns.com) ,

    privateKey: SSH key

    Then once your tunnel is connected connect via mongoose to your localhost such as mondodb://localhost:27000 (use the localport you defined in localPort)

    var server = tunnel(config, function (error, server) {
        if(error){
            console.log("SSH connection error: " + error);
        }
        mongoose.connect('mongodb://localhost:27000/');
        //...rest of mongoose connection
    }
    

提交回复
热议问题