How do you access a MongoDB database from two Openshift apps?

前端 未结 2 807
慢半拍i
慢半拍i 2021-01-07 12:14

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 ap

相关标签:
2条回答
  • 2021-01-07 12:48

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

    0 讨论(0)
  • 2021-01-07 13:04

    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.

    0 讨论(0)
提交回复
热议问题