How to setup an mLab cloud based mongoDB on Heroku

If your deploying an application to Heroku and you want to setup and install MongoDB and/or Mongoose then there are several options. Below we describe one of the ways to setup MongoDB as a a Heroku bin will not come pre-installed with any databases. In this tutorial we will explain the steps needed to setup MongoDB in the cloud using mLab.

What happens when you deploy your application while pointing to your localhost installation of mongoDB?

2017-06-19T03:42:43.322811+00:00 app[web.1]: 
ERROR connecting to: mongodb://localhost/fetcher. MongoError: failed to connect to server [localhost:27017] on first connect 
[MongoError: connect ECONNREFUSED]

After pushing your code up to Heroku, if you watch the Heroku logs (via Heroku logs –tail) you will notice this message. You will still be able to serve up your web files but any subsequent calls to the database will simply timeout unless you have proper built in connection handling.

What if you add mongoDB as a dependency in your package.json?

When putting mongoDB as a dependency, Heroku will first attempt to perform an npm install mongoDB so that the files are located in node_modules. Next it will attempt to launch or run mongoDB via the package.json start script command npm run db-start which in this case simply calls mongodb with the database path. This too results in an error:

2017-06-19T04:06:01.862282+00:00 app[web.1]: > github-fetcher-fullstack-v2@1.0.0 db-start /app
2017-06-19T04:06:01.862284+00:00 app[web.1]: > mongod --dbpath ./database/data/db
2017-06-19T04:06:01.862284+00:00 app[web.1]:
2017-06-19T04:06:01.899859+00:00 app[web.1]: sh: 1: mongod: not found

How to setup an mLab account and create a mongoDB instance in the cloud for your app to use via Heroku?

When searching Heroku’s website for how to install mongoDB, it takes to you a page where it recommends installing an add-on, either mLab or Compose. We will walk you through the steps of setting up mLabs.

  1. Go to mLab’s website and create a free account
  2. You can either create a new mongoDB via mLab’s website or via Heroku as an add-on. We are going to create one via Heroku
  3. Go to terminal in the folder of where you setup you project (assuming you already performed Heroku init) and type
    1. $heroku addons:create mongolab
  4. Get your automatically generate database URI by typing
    heroku config:get MONGODB_URI

    The URI will look something like this:

    MONGODB_URI => mongodb://
  5. Change the database URI in your application to this URI which is now your cloud-based free MongoDB instance.
  6. Since we are no longer attempting to install a local instance of the database on Heroku you can remove the mongoDB dependency line in your package.json
  7. Locally you may want to run npm start to run your application and your local instance of the database but on Heroku you simply want to launch your node server.js. Therefore we recommend creating a Procfile.
  8. A Procfile is a way to tell Heroku what commands are run by your application’s dynos. Procfile’s can have different types such as workers, web and clocks. For the sake of a simple application you can create a file with a single line which will tell Heroku to launch your web command as follows web: node server/index.js
  9. Simply create a file named Procfile (not extension) in the root directory of your application
  10. When you do this Heroku will ignore your npm start script and use the Procfile instead to simply launch your website. Since you already changed your database connection file on step 5 to the cloud-based mLab database you should not be setup and good to go with using mongoDB on the cloud via Heroku!

Also published on Medium.

Leave a Reply

Your email address will not be published. Required fields are marked *