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"
精彩评论