How to setup 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?
ERROR connecting to: mongodb://localhost/fetcher. MongoError: failed to connect to server [localhost:27017] on first connect
[MongoError: connect ECONNREFUSED 127.0.0.1:27017]
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]: > firstname.lastname@example.org db-start /app
2017-06-19T04:06:01.862284+00:00 app[web.1]: > mongod --dbpath ./database/data/db
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.
- Go to mLab’s website and create a free account
- 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
- Go to terminal in the folder of where you setup you project (assuming you already performed Heroku init) and type
- $heroku addons:create mongolab
- Get your automatically generate database URI by typing
heroku config:get MONGODB_URI
- The URI will look something like this:
MONGODB_URI => mongodb://heroku_12345678:email@example.comLab.com:29017/heroku_12345678
- Change the database URI in your application to this URI which is now your cloud-based free MongoDB instance.
- 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
- 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.
- 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
- Simply create a file named Procfile (not extension) in the root directory of your application
- 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!
Originally published at DECODING SOFTWARE.