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
- 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.
- 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.
- 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>
- 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/testwhen 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:
- Login to http://openshift.redhat.com
- Click My Apps.
- Click the > next to your app name to reach your application page.
- Click Want to log in to your application?
- Copy the provided ssh shell command to a shell window and press enter to ssh to your app gear.
- Open your gear’s .bash_profile in your text editor of choice. It is located at
- 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.
- After editing the file, run
echo $MONGOLAB_URIto confirm success. The value you added should be displayed at the console.
- Restart your app by running
Step 5. View the app
Visit your deployed app athttp://<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 email@example.com.