Friday, May 26, 2017

Connect to MongoDB on aws Server From Another Server

Leave a Comment

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?

4 Answers

Answers 1

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 

Answers 2

Per this link you might want to check 1) OS firewalls; 2) correct service binding to private IP address (if not, add your IP to the IP address whitelist ; 3) your Mongoose version if used. Use 'npm list mongoose' to find out the version, and update it to the latest version.

You can use Mongodb Atlas , a 'MongoDB as a Service offering available on Amazon Web Services (AWS)', to host your database(it has a free tier to start), and use MongoDB Compass to easily manage your data.

Answers 3

You can connect it using mongoose.

var mongoose = require('mongoose');

mongoose.connect('mongodb://serverIpaddress/databasename', function (err) {     if (err) {        return "no"     } else {         console.log('connection successful');      } }); 

Answers 4

In order to be able to connect external mongodb over Internet. Following conditions should be met:

  • mongodb should have routable public IP
  • mongodb security-group should white-list your public IP

In case of mongodb don't have public IP (private network inside a VPC) it still can be accessed by your localhost using one of these methods:

  • VPN to private network
  • Site-to-Site between your office/home and AWS VPC
  • attach an EIP to mongodb instance eni (if eni have public subnet routed via Internet Gateway)
  • create an eni with public IP add attach it to mongodb instance

In any case - before you start write code: try to connect to mongodb by using telnet, netcat or some working mongodb client.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment