How to use MongoDB on RedHat OpenShift with MongoLab

Hey RedHat fans - we've got your MongoDB hosting needs covered!

In today's post we'll be presenting a quick-start guide on how to connect OpenShift, the free RedHat auto-scaling Platform-as-a-Service (PaaS), with our popular MongoDB Database-as-a-Service (DBaaS), MongoLab.

For demonstration purposes, we'll be using a Node.js application that we've written (available for download here). All it takes to connect your OpenShift application is five easy steps!

Step 1. Create an OpenShift account and application

Create an account at and install the rhc command-line tool on your development machine. For more info about rhc, see

Once rhc is installed, create a nodejs-0.6 application using the path to this repository as the --from-code argument and by replacing with your desired application name:

% rhc app create  nodejs-0.6
% cd

rhc initializes your application using this repository as a baseline.

Step 2. Create a MongoLab account and database

  1. Sign up for an account at After you've successfully created your MongoLab account, you'll see "Databases" header and a "Create new" button.
  2. Click on the "Create new" button to create a database. Be sure to specify a database user name and password. These credentials are not the same as your MongoLab account credentials.
  3. Click on your database. The database landing page provides a mongodb URI connection string of the form:
    mongodb://<db user>:<db password>@<host>:<port>/<db name>
  4. Copy this value somewhere helpful and replace placeholders with your database user credentials.

Step 3. Commit and deploy the app

When you've created your app, the rhc command line client automatically initialized a git repo with a remote link to OpenShift. The code is also already deployed to your app gear.

If you don't make any changes, you can skip this step. However, if you make any modifications (now or later), perform the following to update the code on the gear:

% git add .
% git commit -m "my first commmit" -a
% git push

Step 4. Configure environment variables on the app gear

The example code uses mongodb://localhost:27017/test when the MONGOLAB_URI environment variable is not available. This is sufficient for testing with a MongoDB database running on your local machine, but not for production.

Note: We find that configuring this value outside of the code (and not storing it in a repository) allows for maximum security and flexibility. However, there are repository-driven alternatives for configuring this environment variable that may meet your requirements. See how to create and use environment variables on the server for more information.

To configure your environment variable without placing credentials in a repository:

  1. Login to
  2. Click My Apps.
  3. Click the > next to your app name to reach your application page.
  4. Click Want to log in to your application?
  5. Copy the provided ssh shell command to a shell window and press enter to ssh to your app gear.
  6. Open your gear's .bash_profile in your text editor of choice. It is located at ~/app-root/data/.bash_profile.
  7. Add the line export MONGOLAB_URI=<db uri> where db uri is the mongodb URI you obtained in Step 2, with your database user credentials added.
  8. After editing the file, run source ~/app-root/data/.bash_profile
  9. Use echo $MONGOLAB_URI to confirm success. The value you added should be displayed at the console.
  10. Restart your app by running ctl_all stop then ctl_all start.

Step 5. View the app

Visit your deployed app at:

    http://<app name>-<app namespace>

And there you have it - just five steps to get your OpenShift deployed application connected to your MongoLab database!

If you ever have any questions, don't hesitate to get in touch with us at

7 Responses to How to use MongoDB on RedHat OpenShift with MongoLab

  1. Ruslan 2013/04/14 at 1:13 am #

    Hi Eric! Thanks for the useful article. Any plans to write similar “how to” for Jelastic fans? MongoDB is one of the most popular DBs in Jelastic community.

  2. Dave Green 2013/04/18 at 11:04 pm #

    This is great – very succinct and readable. Any idea when you guys might be supporting sharding of MongoDB instances? And we’re not restricted to 0.6 Node.js, right? We can utilise newer releases?

  3. Eric Sedor 2013/04/19 at 12:07 am #

    Thank you!

    You’re not technically required to use Node.js 0.6 but it is the easiest to use because it is available “out of the box” as an OpenShift cartridge. Check out for a means of controlling your own node version. If you run into trouble or need more assistance there, OpenShift support is your best bet for more information.

    I don’t have information handy regarding our sharding roadmap, but please email to discuss.

  4. Eric Sedor 2013/04/19 at 12:10 am #

    You’re most welcome, and thanks for the suggestion! I’ll definitely take a look and see.

  5. niki 2013/10/12 at 7:32 pm #

    That was perfect but still I can not see anything.

    I added $MONGOLAB_URI and it works also added “server.js” in the php directory but still can not see anything. I have not change the default index.php but when I go to the “server.js” it shows me the code.

    Can you help me please?

  6. Eric Sedor 2013/10/14 at 4:51 pm #

    Hi Niki! Can you email with as much detail on this issue as you can? We’d be happy to help.

  7. Mark Baird 2015/04/24 at 8:02 pm #

    Adding environment variables to .bash_profile on OpenShift no longer seems to work. There is a new way to set environment variables now via the “rhc set-env” command.

Leave a Reply