Connect Compass with remote database via two ssh tunnels

My mongodb database is hosted on a server within the school’s intranet, accessible through two ssh tunnels, one to the gateway of the intranet, and one to the server. I’ve looked at previous posts regarding using ssh to link MongoDB compass with a remote database, but all of them only have one ssh tunnel. I’m wondering how do I link Compass with the remote database if there are two ssh tunnels in between? Thank you all so much in advance for answering.

You can do it with two tunnels and ssh -L and ssh -R but that will tend towards instability.
Can you set up some kind of VPN instead?

are there any command I can look at?

Let’s say I use the following two command to access the remote database

  1. ssh account of intranet@intranet address
  2. insert the password for the above
  3. ssh account of the remote server@server address
  4. insert the password for the above

How will the ssh command be like in this case?

ssh -t -L 27017: myHopServer ssh -L 27017: myMongoDBServer

Then open Compass as the same user and address the localhost:27017 as if it were the MongoDB server.

I tested this and it works for me.

I tried the above code and saw the following error message and regardless, I open the Compass GUI and input localhost:27017 (with no authentication) and it only connects to my own computer’s databases.

bind []:27017: Address already in use
channel_setup_fwd_listener_tcpip: cannot listen to port: 27017
Could not request local forwarding.

@Timmy_Hsu , my example assumes that you are not running MongoDB locally.
Apparently you are running MongoDB locally.
In that case, change the example to something like:
ssh -t -L 27117: myHopServer ssh -L 27017: myMongoDBServer
and connect with Compass to port 27117 on your local machine to hop thru to your target.
Of course, this assumes that you’re not running MongoDB on myHopServer … if you are doing so, you’ll need to change the port there as well!

1 Like

I’m opening my compass on my own computer, but the intended MongoDB server is on a remote server that’s accessible via two ssh tunnels.

This works!!! Thank you so much, your answer has been very helpful!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.