8

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?

Dylan
  • 469
  • 7
  • 19
  • 1
    Also see [this](https://stackoverflow.com/questions/22054856/using-meteor-mongo-on-localhost-but-with-remote-database) and [this](https://stackoverflow.com/questions/10588038/how-to-use-the-existing-mongodb-in-a-meteor-project). I suppose this isn't a duplicate question because you are asking about the specific case of AWS, but the answer is the same: use `MONGO_URL`. – David Weldon May 07 '16 at 21:17
  • Here is [specific tutorial of MongoDB configuration for Meteor](https://veliovgroup.com/article/2qsjtNf8NSB9XxZDh/mongodb-replica-set-with-oplog), it's worth to take a look. – dr.dimitru May 24 '17 at 22:56

5 Answers5

3

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
Francesco Pezzella
  • 1,491
  • 2
  • 13
  • 17
  • Thanks, but for some reason I'm getting an error when I try to connect from localhost.Error: failed to connect to [address.compute-1.amazonaws.com:27017] – Dylan May 09 '16 at 03:37
  • The connection string I wrote was just a placeholder. You have to use yours – Francesco Pezzella May 09 '16 at 07:55
  • Right. I tried MONGO_URL=mongodb://xx-xx-xxx-xx.us-west-2.compute.amazonaws.com/meteor meteor too and I get the same error. All traffic is open. Is it maybe because I am trying to connect to a DB on the aws server that was created by meteor? – Dylan May 09 '16 at 19:56
  • 1
    Got the same error. Basically the mongodb is in AWS behind a VPC which you usually connect to via ssh. So I don't really know how to let my app reach the mongodb just by using the line above but I assume, that username and password will be not sufficient enough. – Jankapunkt May 19 '17 at 12:59
  • @Jankapunkt What is the setting for `bindIp` in your `mongod.conf` on your server? The file should be at `/etc/mongod.conf` – user818510 May 22 '17 at 16:13
  • bindIp is uncommented. There is no issue with this, since nc 10.0.something 27017 inside my bastion returns successful, while nc from a client (whose IP is inside the CIDR for allowed TCP inflow) returns connection refused. – Jankapunkt May 22 '17 at 16:17
  • 4
    Not sure, but hope that helps: in general there are two ways to access the resource on AWS - 1) change the "security group" configuration for EC2 instance - this acts as a firewall - and allow access to port 27017; 2) use ssh tunnel to connect to the instance and have that port tunelled locally, something like `ssh aws-host -L 27017:localhost:27017` or, if you are connecting through the different instance `ssh aws-host -L 2017:target.mongo.host.amazonaws.com:27017`. – Boris Serebrov May 23 '17 at 20:51
  • @BorisSerebrov, I'd post it as an answer. It might worth to add a VPN option to it. – Alex Blex May 24 '17 at 16:17
  • @AlexBlex I didn't post it as answer, as I am not confident about mongo settings, I think Francesco has a more important half of the answer already, as the question was about Mongo. Francesco, maybe you can update your answer with info from my comment? You can also refer to many other questions/answers about how to open specific port on AWS (https://stackoverflow.com/questions/17161345/how-to-open-a-web-server-port-on-ec2-instance, https://stackoverflow.com/questions/5004159/opening-port-80-ec2-amazon-web-services, ...). – Boris Serebrov May 24 '17 at 18:05
  • @BorisSerebrov please make your comment an answer to receive the bounty, since your answer comes closest to solve the issue. You may refer to security issues there, too. The solution is not just to open the port (on your NAT instance) but to forward the traffic within your private subnet in order to make it run. – Jankapunkt May 26 '17 at 12:13
2

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

Boris Serebrov
  • 13,820
  • 1
  • 32
  • 49
1

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

    }
});
vijesh
  • 987
  • 8
  • 24
0

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.

Treefish Zhang
  • 1,056
  • 1
  • 13
  • 23
0

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.

Tom Lime
  • 984
  • 10
  • 12