Forum Discussion

elenal's avatar
Occasional Contributor
7 years ago

Connecting to mongo database through SSH tunnel

Here's what I have.




def sshHost = ''
def sshUser = ''
def sshPort = ''
def localPort = ??
def mongoPort=27017 //default MongoPort

JSch jSch=new JSch();
sshUser, // String userName
prvkey, // byte[] privateKey
null, // byte[] publicKey
emptyPassPhrase // byte[] passPhrase
Session session = jSch.getSession(sshUser, sshHost, sshPort);
session.setConfig("StrictHostKeyChecking", "no")

session.setPortForwardingL(sshPort, mongohost, mongoPort); ("SSH connection set up")

//connect to mongo
def mongo= new GMongo (new MongoURI(mongoendpoint))
def db = mongo.getDB(mongocollection)
def collection = db.getCollection(collection)
def result = collection.findOne()

session.disconnect() ("SSH disconnected")

Looks like SSH connections is established successfully, but it can't connect to Mongo.

The closest solution I found is


Not sure, but I think the problem is in this line:

session.setPortForwardingL(localPort, mongohost, mongoPort);

So how do I know what the local port is?

and also what should be in mongohost if I'm connection to replice?

No RepliesBe the first to reply