mongodbsshvpsmongodb-compass

How to connect local MongoDB Compass to a third-party VPS Mongo database?


I have a remote Virtual Private Server (VPS) running Linux CentOS on which MongoDB has been installed by my service provider. I've long hosted websites there, but I am new to MongoDB. I am able to access that VPS via my local terminal.

I SSH into the server with this simple command, without requiring a password because I have a public RSA key in the server's .ssh directory:

ssh sshusername@111.111.111.11

If I specify a port, I am then prompted for a password to SSH in:

ssh sshusername@111.111.111.11 -p22

Either way, I am connected and able to work with my Mongo database on that VPS starting with this command, which prompts for a password:

mongosh --username mongousername --password

I can create users and the basics via command line. That's not an issue.

What I need help with is that I want to be able to work with this database via the GUI in MongoDB Compass, but I can't figure out how to configure Compass in order to make the proper connection. It never connects and I keep getting "socket closed" errors. I've tried several ways and can't figure it out. I've tried doing it with usernames and passwords (both for my SSH connection and the MongoDB users), as well as uploading the matching private key for the public one on my VPS. I've tried using passwords and the key at the same time. I've tried with TLS on and off. I can't can't find clear documentation for what I need to do.

I guess I need to come up with the correct URI and/or Advanced Connection Options in Compass. I started with this for the URI, tried including the port, and have played around with a bunch of different connection options, as I mentioned above.

mongodb://111.111.111.11

I presume I will need to save multiple connection presets for multiple users, because my admin user can only change other users (if I understand correctly) and the other users are the ones that actually manipulate various databases. Is this correct?

Any and all help appreciated, as well as any pointers or tips to better get me going. Thanks.


Solution

  • Summarizing/"explaining" the comments:

    With my naive notion of how applications connect to a server through an SSH tunnel, I think of "What gets me through the tunnel?" and after I'm there, "What connects my app to the server?".

    You already did those steps manually.

    ssh sshusername@111.111.111.11 got you through the SSH tunnel (and gave you a commandline shell). So, the app (MongoDB Compass) needs to know how to do that too. Setting MongoDB Compass's SSH Hostname to 111.111.111.11 (and any other SSH parameters you want/need) gets you through the tunnel.

    mongosh --username mongousername --password connected the app (mongosh) to the server (from the commandline shell). MongoDB Compass (after getting through the tunnel) needs to do the same thing. That corresponds to a (basic) URI of mongodb://127.0.0.1:27017.