Search code examples
mongodbmeteormeteor-up

How can I get my meteor app to connect to a database with MONGO_URL env variable in Meteor Up (mup)?


I would like a my meteor app to connect to a remote database. I thought I could deploy two apps, one that sets up the database on a server and another that hosts the app but connects to the database on the other server. I am having trouble getting this to work, but I am also wondering if this is recommended or not? Should I setup a mongodb database on a server without mup/meteor or can I use mup to set that up for me?

What I have tried and cannot get to work:

I am using two amazon ec2 instances (ubuntu 14.04). I have made the following security inbound rules for both instances:

Custom TCP Port: 27017 Source: instance-public-ip-1/32
Custom TCP Port: 27017 Source: instance-public-ip-2/32
HTTP Port: 80 Source: All Addresses
SSH Port: 22 Source: My IP

In the mup.json file for the app trying to access the remote database, I have added "MONGO_URL": "mongodb://db-instance-public-ip:27017/db" under "env" and db is the name of the app on that instance.

The meteor app hosting the database deploys successfully but the meteor app hosting the app does not. The deployment fails during the "Invoking deployment process" and here is the STDERR/STDOUT: (XXX.XXX.XXX.XXX is the public IP address of the instance)

x Invoking deployment process: FAILED

-----------------------------------STDERR-----------------------------------
npm WARN package.json [email protected] No description
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No README data

/usr/lib/node_modules/wait-for-mongo/bin/wait-for-mongo:14
    throw err;
          ^
Error: TIMEOUTED_WAIT_FOR_MONGO
    at null._onTimeout (/usr/lib/node_modules/wait-for-mongo/lib/waitForMongo.js:20:14)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
-----------------------------------STDOUT-----------------------------------
56.145:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
wait-for-mongo: failed to connect to [XXX.XXX.XXX.XXX:27017]
----------------------------------------------------------------------------

Let me know if more information is needed. Question is also posted here: https://github.com/arunoda/meteor-up/issues/450


Solution

  • Edit: I had let mongodb listen to all interfaces, but that is a security risk.

    By default when a deployed meteor creates a database, the mongodb configurations are set to only listen for connections on 127.0.0.1 (its the bind_ip option in mongodb). Changing this setting to listen to both 127.0.0.1 and the private ip of the instance has solved the issue, and it is now successfully deploying and using the remote database. Do NOT comment out the bind_ip option as allows anyone to connect to your database. Good security rules will still prevent people connecting but it is a good idea to follow the "principle of least privilege" in the mongodb configuration.

    In order to change this setting, edit the /etc/mongod.conf file on the instance with the database. Add the private ip of the instance hosting the database to the line with bind_ip = 127.0.0.1.

    lines 17-18 from /etc/mongod.conf after edit (XXX.XXX.XXX.XXX is the private ip address of the instance with the database):

    # Listen to local interface only. Comment out to listen on all interfaces. 
    bind_ip = 127.0.0.1,XXX.XXX.XXX.XXX
    

    More information about the mongo config file can be found here: http://docs.mongodb.org/manual/reference/configuration-options/

    I found this file by running db.serverCmdLineOpts() in the mongo shell, thanks to Adam Comerford's answer on this question: how can I see what ports mongo is listening on from mongo shell?.