How to set up Mongodb Server and connect to it programmatically with Spring from local machine?

Posted May 15, 2017 5.4k views

I want to create a MongoDB server and connect it from local machine using Spring Data MongoDB. I’m able to connect to MongoDB using ssh via Mac CLI but don’t know how to create URI to connect to it. In another words, I don’t know how to create MongoClient.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Hi @devbc7ce689810e2095410142c

I would recommend that you keep MongoDB on your droplet contained, so it only listens on localhost of the droplet - and not exposing it to the internet.

Then you can create a SSH tunnel to your server and you would then connect to localhost on your computer in Spring Data.
This tutorial is for various other services and ports, but you just need to setup port 27017 which is MongoDB’s default port (unless you’ve changed that):

In this article, you'll learn how to create a safe, encrypted tunnel between your computer and your VPS along with how to bypass limits in a corporate network, how to bypass NAT, etc.