Rails - 具有单个源和database.yml文件的多个开发实例

时间:2014-01-29 09:33:43

标签: mysql ruby-on-rails ruby development-environment

我们可以使用单个源和单个multiple development instances文件连接到database.yml吗?我的意思是,我需要有来自同一来源的4个开发实例,它连接4个不同的MySQL数据库。我可以使用不同的PORT ID s启动服务器吗?

示例:

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

然后,我们如何启动4个不同的服务器(在localhost)同时连接这4个数据库? (我们通常在开发中使用rails server作为单个实例。)

谢谢:) -

2 个答案:

答案 0 :(得分:2)

  • 您不仅限于默认的rails环境。您可以拥有development-2development-3个环境。阅读this post on the 37signals blog where DHH talks about that

  • database.yml可以读取环境变量。

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

    然后确保在启动服务器时设置了这些内容。

答案 1 :(得分:2)

这非常容易。

只需更新问题中提供的database.yml文件即可。假设我们的开发实例是developmentdevelopment-2development-3development-4

首先,您只需创建3个环境文件development.rb(可在config/environments/内找到)并将其重命名为development-2.rbdevelopment-3.rbdevelopment-4.rb

然后,在启动服务器之前,请执行以下步骤。

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

然后,使用下面给出的不同服务器pid启动所有实例。

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

这将同时从同一个source和一个database.yml文件启动所有实例。感谢大家的所有回复和帮助:) -