开发者

What is the correct way to use mongodb on heroku

开发者 https://www.devze.com 2023-03-17 16:59 出处:网络
I have an app which is hosted on Heroku as well as on a VPS. I am using mongodb 开发者_JS百科on this app with mongoid. I want to know what is the correct way to define database connection in mongoid.y

I have an app which is hosted on Heroku as well as on a VPS. I am using mongodb 开发者_JS百科on this app with mongoid. I want to know what is the correct way to define database connection in mongoid.yml so that the same file works on both heroku and VPS. Heroku expects MONGOHQ_URL in the production database while my VPS needs default db mapping:

Correct for heroku: 
    production:
          uri: <%= ENV['MONGOHQ_URL'] %>

Correct for VPS:
     production:
        <<: *defaults
            host: localhost
            database: grbr_production

Pushing mongoid.yml separately for Heroku and VPS is a real pain. Is there a way I can create one unified entry which works


It might be easiest to just add a MONGOHQ_URL environment variable to the VPS that points to the mongodb instance on the localhost:

mongodb://localhost/grbr_production


What the Heroku instance expects is for the environment variable MONGOHQ_URL to be defined. The variable name could be anything, and if you are not using MongoHQ, it makes sense to rename it.

To answer your question, you could have the following config file for both:

# mongoid config file
production:
    uri: <%= ENV['MONGODB_URI'] %>

Then on your VPS, assuming a bash environment:

export MONGODB_URI="mongodb://username:password@localhost:10010/db-name"

Make sure to change all values to the appropriate ones.

You can either run that in the console, or better yet, add it to the ~/.bashrc file of the user running the mongodb instance so that it persists on restarts.

Then on heroku you define it using the heroku toolbelt command:

heroku config:set MONGODB_URI="mongodb://username:password@VPS-IP-ADDRESS:10010/db-name"
0

精彩评论

暂无评论...
验证码 换一张
取 消