NoSQL Zone is brought to you in partnership with:

Eric is an artist and storyteller who boasts his first computing experience juggling drivers into memory to run as many DOS games as possible without rebooting. Since then he has become a creative, patient reverse engineer who operates on the basic premise that something never has to be the way that it is. Eric has a B.S. in Computer Science from the Georgia Institute of Technology and his experience in the ETL layer across a broad range of industries has instilled in him an appreciation for semantics and the belief that all of mankind's problems are related to our adherence to inaccurate representations of reality. Eric is a DZone MVB and is not an employee of DZone and has posted 8 posts at DZone. You can read more from them at their website. View Full User Profile

MongoDB on RedHat OpenShift with MongoLab

04.12.2013
| 3453 views |
  • submit to reddit

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 http://openshift.redhat.com and install the rhc command-line tool on your development machine. For more info about rhc, see https://openshift.redhat.com/community/developers/rhc-client-tools-install.

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
--from-code https://github.com/mongolab/mongolab-openshift-quickstart
% 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 http://www.mongolab.com. 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 http://openshift.redhat.com
  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>.rhcloud.com

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 support@mongolab.com.

Published at DZone with permission of Eric Sedor, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)