How do you access a MongoDB database from two Open

2019-02-15 20:15发布

I want to be able to access my MongoDB database from 2 Openshift apps- one app is an interactive database maintenance app via the browser, the other is the principle web application which runs on mobile devices via an Openshift app. As I see it in Openshift, MongoDB gets set up within a particular app's folder space, not independent of that space.

What would be the method to accomplish this multiple app access to the database ?

It's not ideal but is my only choice to merge the functionality of both Openshift apps into one ? That's tastes like a bad plate of spaghetti.

2条回答
该账号已被封号
2楼-- · 2019-02-15 20:41

Please read the following article from the open shift blog: https://blog.openshift.com/sharing-database-across-applications/

查看更多
三岁会撩人
3楼-- · 2019-02-15 20:47

2018 update: this applies to Openshift 2. Version 3 is very different, and however the general rules of linux and scaling apply, the details got obsolete.

Although @MartinB answer was timely and correct, it's just a link, so let me put the essentials here.

Assuming that setting up a non-shared DB is already done, you need to find it's host and port. You can ssh to your app (the one with the DB) or use the rhc:

rhc ssh -a appwithdb
env | grep MONGODB 

env brings all the environment variables, and grep filters them to show only Mongo-related ones. You should see something like:

OPENSHIFT_MONGODB_DB_HOST=xxxxx-yyyyy.apps.osecloud.com
OPENSHIFT_MONGODB_DB_PORT=zzzzz

xxxxx is the ID of the gear that Mongo sits on
yyyyy is your domain/namespace
zzzzz is MongoDB port

Now, you can use these to create a connection to the DB from anywhere in your Openshift environment. Another application has to use the xxxxx-yyyyy:zzzzz URL. You can store them in custom variables to make maintenance easier.

$ rhc env-set \
MYOWN_DB_HOST=xxxxx-yyyyy \
MYOWN_DB_PORT=zzzzz \
MYOWN_DB_PASSWORD=****** \
MYOWN_DB_USERNAME=admin..... \
MYOWN_DB_NAME=dbname...

And then use the environment variables instead of the standard ones. Just remember they don't get updated automatically when the DB moves away.

查看更多
登录 后发表回答