How to access your OpenShift MongoDB database remotely on OS-X

I recently started playing around with Redhat’s Openshift PaaS and installed the MongoDB and RockMongo cartridges on my application. My use case was just to leverage the Openshift platform to run my MongoDB instance for me, and I really was ready (nor needing) to push an actual application out to the application running @ openshift; instead I just wanted my local Java program to leverage the remote MongoDB instance, pump some data into it and then view it in Rockmongo (also running on the app at openshift).

Turns out you can enable this by enabling port forwarding locally on the computer you want to connect from. This is on OS-X:

  • First ensure that your SSH key is available for use by the command we will run: “ssh-add /path/to/your/openshift/ssh.key” This should be the key that you created when you initially signed up for Openshift
  • Attempt to run the port forwarding enable command “rhc port-forward -a [yourAppName] -l [yourOpenShiftLoginId]“, it will prompt you for your openshift credentials, then will likely output something like the below:

Checking available ports...

Binding httpd -> 127.5.198.2:8080…
Binding mongod -> 127.5.198.1:27017…

Use ctl + c to stop

bind: Can’t assign requested address
channel_setup_fwd_listener: cannot listen to port: 27017
bind: Can’t assign requested address
channel_setup_fwd_listener: cannot listen to port: 8080
Could not request local forwarding.

  • The above error is because we need to now enable loopback aliases for each failed 127.x address listed above (those addresses will be different in your output above). To do this run the following for EACH loopback that failed: “sudo ifconfig lo0 alias 127.x.x.x“. Note that even after running this, make sure you already don’t have something else local bound on those ports (i.e. a local dev mongodb instance or something else on 8080)
  • After you add the loopback aliases re-run the port forwarding command above, this time it should successfully complete. If you go to https://127.x.x.x:8080 your request will be going directly to your openshift instance.
  • You can also now connect to your mongodb instance programmatically as well just using the new address:ports forwarded above plus the appropriate credentials
  • Interestingly enough the port forwarding goes over an SSH tunnel to your app there, and it appears that Openshift itself uses AWS!
Some relevant links to help you out which also helped me:
About these ads

3 Comments

  1. Posted July 21, 2012 at 8:15 pm | Permalink | Reply

    Many thanks for this! Saved me hours. The “sudo ifconfig lo0 alias 127.x.x.x“ should have been in the OpenShift documentation.

  2. Posted September 25, 2012 at 12:24 pm | Permalink | Reply

    my node-app is connecting to the mongodb but whenever i try to insert something, i get a “not authorized” error. any clue what this is about?

  3. Posted November 9, 2012 at 6:43 am | Permalink | Reply

    You save me, thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 28 other followers

%d bloggers like this: