Dylan
Dylan

Reputation: 491

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?

Upvotes: 8

Views: 3369

Answers (5)

Borys Serebrov
Borys Serebrov

Reputation: 16182

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).

Upvotes: 2

Tom Lime
Tom Lime

Reputation: 1204

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.

Upvotes: 0

vijesh
vijesh

Reputation: 1173

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');

    }
});

Upvotes: 1

Treefish Zhang
Treefish Zhang

Reputation: 1161

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.

Upvotes: 0

Francesco Pezzella
Francesco Pezzella

Reputation: 1795

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

Upvotes: 3

Related Questions