1

I have a rails app of which I am running two server instances (different ports, different PID).

rails s -p 8081 -P 12345
rails s -p 8082 -P 54321

However, I would like each of them to connect to a different mongodb database (for example: db12345 and db54321, respectively).

The problem is that I don't know whether this is possible and if so, how to specify it in my mongoid.yml configuration file.

The respective section of my mongoid.yml file, allowing me to use the db12345 database, currently looks like this:

development:
  options:
    raise_not_found_error: false
  sessions:
    default:
      database: **db12345**
      hosts:
        - localhost:27017
      options:
        max_retries: 30
        retry_interval: 1
        timeout: 15
        refresh_interval: 10

What I am having trouble with is finding a way to link each instance of the same rails app to the (different) database it should use. Is this even meant to be dealt with in mongoid.yml?

Thank you!

1 Answer 1

1

You can control this via environment variables. Have your apps launched in this manner:

DBNAME=db12345 rails s -p 8081 -P 12345
DBNAME=db54321 rails s -p 8082 -P 54321

Then just refer to this var in mongoid.yml, with a sane default:

  sessions:
    default:
      database: <%= ENV['DBNAME'] || 'db12345' %>
Sign up to request clarification or add additional context in comments.

3 Comments

This is exactly what I was looking for! Thank you so much! There is also a dba stackexchange question related to this. I don't know how this works, but if you want you can answer this question over there as well. Here's the link: dba.stackexchange.com/questions/142445/…
@MT: crossposting is discouraged on stackexchange. But this question does look like a valid DBA stuff, so I'm going to post the answer there too.
Thank you! Good to know! I was thinking others might be looking for an answer over there. In any case, thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.