Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to MongoDB on aws Server From Another Server

I am creating a javascript meteor app from my localhost, but I would like my database to be stored on a separate aws server.

I am new to MongoDB and aws. I am wondering how I would go about connecting to my database from my local host?

like image 684
Dylan Avatar asked May 07 '16 20:05

Dylan


2 Answers

That's as simple as starting your Meteor app with MONGO_URL env variable set to point to the Mongo instance running on your AWS machine.
Assuming you have already opened port 27017 on remote machine:

MONGO_URL=mongodb://addresshere.compute-1.amazonaws.com:27017/yourdbname meteor
like image 81
Francesco Pezzella Avatar answered Sep 23 '22 20:09

Francesco Pezzella


In addition to configuring the application to connect to remote MongoDB, as mentioned in other answers, it is also necessary to make sure the server is accessible from your network.

There are different ways to access the network resource on AWS:

  • Change the security group configuration for EC2 instance - this acts as a firewall - and allow access to port 27017. If you only need to access it from your machine, there is an option to allow access from your current IP.

  • Use ssh tunnel to connect to the instance and have that port tunneled locally, something like ssh aws-host -L 27017:localhost:27017or, if you are connecting through the different instance ssh aws-host -L 2017:target.mongo.host.amazonaws.com:27017. There is a good presentation on this topic - The Black Magic Of SSH / SSH Can Do That?.

  • Use VPN on AWS and connect to AWS network via VPN (making resources inside your AWS network available locally).

like image 25
Boris Serebrov Avatar answered Sep 21 '22 20:09

Boris Serebrov