Getting Started with MongoDB Shell on OpenShift

Last week, we announced and demonstrated MongoDB on OpenShift. One thing that experienced Mongo users immediately asked was "How do I get to the Mongo Shell?" In this post, I will show you how.

First, let's create an application. I'm going to name this application "zarkov", in honor of the scientist who explored the planet Mongo.

Run the command

  $ rhc-create-app -a zarkov -t wsgi-3.2

which displays

Application Info
================
zarkov
    Framework: wsgi-3.2
     Creation: 2011-12-16T18:45:34-05:00
         UUID: 4036e1eb9f114c278d5f9aaf92dda2d9
      Git URL: ssh://4036e1eb9f114c278d5f9aaf92dda2d9& @ \
                     zarkov-matwood.rhcloud.com/~/git/zarkov.git/
 
 Embedded: 
      None

Make a note of the UUID and host, because we will need it later. In this case they are "4036e1eb9f114c278d5f9aaf92dda2d9" and "zarkov-matwood.rhcloud.com".

And now embed MongoDB in it.

  $ rhc-ctl-app -a zarkov -e add-mongodb-2.0

which gives us

MongoDB 2.0 database added.  Please make note of these credentials:
 
       Root User: admin
   Root Password: c3XBw52WlM5n
   Database Name: zarkov
  Connection URL: mongodb://127.1.40.1:27017/
 
You can manage your new MongoDB by also embedding rockmongo-1.1

Save that password, we will need it later. In this case, it's "c3XBw52WlM5n", but it will be different each time you create a new application.

Now we connect to the OpenShift shell and have it run the Mongo shell

  ssh -t \
    -l 4036e1eb9f114c278d5f9aaf92dda2d9 \
    zarkov-matwood.rhcloud.com \
      rhcsh mongo

The uuid based "username" and hostname were what we saved earlier after creating the app. The "-t" option to ssh forces the far side to create a tty, so features such as command line editing work correctly.

Now we're at the Mongo shell prompt, and you can issue any Mongo commands or JavaScript code you care to enter.

First we have to authenticate

use admin
db.auth("admin", "c3XBw52WlM5n");

That was the MongoDB admin password we saved earlier.

Now switch to the application database. The OpenShift Mongo cartridge creates a database for the application, and gives it the same name as the application. So we enter

use zarkov

To look at the statistics for this database, now enter

  db.stats()

which will display

{
"db" : "zarkov",
"collections" : 4,
"objects" : 9,
"avgObjSize" : 52.888888888888886,
"dataSize" : 476,
"storageSize" : 20480,
"numExtents" : 4,
"indexes" : 2,
"indexSize" : 16352,
"fileSize" : 16777216,
"nsSizeMB" : 16,
"ok" : 1
}

What if we want to access the Mongo Shell in a batch manner, as part of a UNIX pipeline, instead of interactively?

Create in input script named "input.text" that looks like this

use admin
db.auth("admin", "c3XBw52WlM5n");
use zarkov
db.stats()

And then run the following

ssh \
  -l 4036e1eb9f114c278d5f9aaf92dda2d9 \
  zarkov-matwood.rhcloud.com \
  rhcsh mongo \
    <input.text

and the database stats JSON will be output. Notice that there is no "-t" option to ssh.

Here is an advanced trick: What if we lose the MongoDB admin password?

Run the following command

  ssh \
    -l 16edf15b6456415aac02007ec84eedf6 \
    zarkov-matwood.stg.rhcloud.com rhcsh \
      printenv OPENSHIFT_NOSQL_DB_PASSWORD

and it will be displayed.

Want to see all this goodness in action? Then sign up for our FREE webinar with 10Gen on Dec 20th.

As always, if you have any questions, feel free to ask them on the OpenShift Forums or come to IRC at freenode.net and ask on the #openshift channel.

Tags:

I had better luck with this command:

ssh -t UUID@mydomain.rhcloud.com rhcsh mongo < mongo-input.text

Not sure why I don't have any similar data in the JSON response???

MongoDB shell version: 2.0.2
connecting to: 127.2.50.1:27017/admin
switched to db admin
1
switched to db zarkov
{
    "db" : "zarkov",
    "collections" : 0,
    "objects" : 0,
    "avgObjSize" : 0,
    "dataSize" : 0,
    "storageSize" : 0,
    "numExtents" : 0,
    "indexes" : 0,
    "indexSize" : 0,
    "fileSize" : 0,
    "nsSizeMB" : 0,
    "ok" : 1
}

That's just cause Mark's example probably had some data in his mongodb instance.

Great article! Extremely helpful

Thank You