Question

Can we connect to multiple development instances with a single source and a single database.yml file? I mean, I need to have 4 development instances from same source which connects 4 different MySQL databases. Can I start the servers by using different PORT IDs?

example:

development:
  adapter: mysql2
  database: TEST1_development
  host: localhost
  username: root
  password: password
  encoding: utf8
  pool: 30

development-2:
  adapter: mysql2
  database: TEST2_development
  host: localhost
  username: root
  password: password
  encoding: utf8
  pool: 30

development-3:
  adapter: mysql2
  database: TEST3_development
  host: localhost
  username: root
  password: password
  encoding: utf8
  pool: 30

development-4:
  adapter: mysql2
  database: TEST4_development
  host: localhost
  username: root
  password: password
  encoding: utf8
  pool: 30

Then, then how we can start 4 different servers (at localhost) connecting these 4 databases simultaneously? (we normally use rails server for single instance in development.)

Thanks :)-

Was it helpful?

Solution 2

It is super easy.

Just update your database.yml file as given in the question. Suppose our development instances are development, development-2, development-3 and development-4.

First you just create 3 copies of the environment file development.rb (can find insideconfig/environments/) and rename it to development-2.rb, development-3.rb and development-4.rb.

Then, just do run the below steps before starting the servers.

RAILS_ENV=development rake db:create (OR just rake db:create as it defaults to development)
RAILS_ENV=development-2 rake db:create
RAILS_ENV=development-3 rake db:create
RAILS_ENV=development-4 rake db:create

RAILS_ENV=development rake db:schema:load (OR just rake db:schema:load as it defaults to development)
RAILS_ENV=development-2 rake db:schema:load
RAILS_ENV=development-3 rake db:schema:load
RAILS_ENV=development-4 rake db:schema:load

RAILS_ENV=development rake db:seed (OR just rake db:seed as it defaults to development)
RAILS_ENV=development-2 rake db:seed
RAILS_ENV=development-3 rake db:seed
RAILS_ENV=development-4 rake db:seed

Then, start all instances with different server pid's as given below.

rails s -p 3000 -e development --pid tmp/pids/server.pid (OR rails s by default)
rails s -p 3002 -e development-2 --pid tmp/pids/server.2.pid
rails s -p 3003 -e development-3 --pid tmp/pids/server.3.pid
rails s -p 3004 -e development-4 --pid tmp/pids/server.4.pid

That will start all instances simultaneously from the same source and a single database.yml file. Thank you all for all your replies and help :)-

OTHER TIPS

  • You are not limited to the default rails environments. You could just have development-2, development-3 environments. Read this post on the 37signals blog where DHH talks about that

  • database.yml can read environment variables.

    development:
      adapter: postgresql
      host: localhost
      database: <%= ENV['POSTGRES_DATABASE'] %>
      username: <%= ENV['POSTGRES_USER'] %>
      password: <%= ENV['POSTGRES_PASSWORD'] %>
    

    then make sure these are set when you start your server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top