Rails & Capistrano 3 - 预发布服务器尝试使用生产数据库

7

我使用NGINX和Passenger为我的staging环境设置了一个服务器。 我还设置了一个staging.rb文件,它是在environments下的production.rb文件的副本。 在我的database.yml文件中,我添加了一个staging配置:

  staging:
  adapter: mysql2
  database: myapp_staging
  username: root
  password: xxxxxxxxxxxxx
  port: 3306
  pool: 15
  timeout: 5000

Environments/staging.rb:

role :app, %w{deploy@111.111.111.111}
role :web, %w{deploy@111.111.111.111}
role :db,  %w{deploy@111.111.111.111}


# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server definition into the
# server list. The second argument is a, or duck-types, Hash and is
# used to set extended properties on the server.

server '111.111.111.111', user: 'deploy', roles: %w{web app db}, port: 0001

我使用cap staging deploy进行部署,但应用程序无法启动,在日志中显示:Unknown database 'myapp_production'

我该如何强制它使用staging数据库?

编辑

Deploy.rb:

set :application, 'dispatch'
set :repo_url, 'myapp'

set :deploy_to, '/home/deploy/myapp'

set :scm, :git
set :branch, 'master'
set :keep_releases, 5
set :format, :pretty
set :log_level, :debug
set :pty, true
set :passenger_restart_with_sudo, true

set :stages, ["staging", "production"]
set :default_stage, "staging"

set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set :rvm_map_bins, fetch(:rvm_map_bins, []).push('rvmsudo')

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      execute :touch, release_path.join('tmp/restart.txt')
    end
  end

  after :publishing, 'deploy:restart'
  after :finishing, 'deploy:cleanup'
end

看起来你的代码库中有一个 config/database.yml 文件,它指向了 myapp_production 数据库。请参考 http://capistranorb.com/documentation/getting-started/preparing-your-application/ 中的 2. Move secrets out of the repository. 部分,了解如何在使用 capistrano 部署时处理 database.yml。基本上,你需要在各自的服务器上保留一个 staging/production 版本的文件,并在部署时将其复制到所需位置。 - Prakash Murthy
4个回答

6

使用我的cap 3设置,我有一个config/deploy/production.rb文件来设置环境。您在暂存和生产环境中都这样做了吗?

set :stage, :staging
server 'example.com', user: 'aaron', roles: %w{web app db}
set :rails_env, :staging

需要确保设置正确的环境,这样数据库任务就会知道连接到哪个数据库。


我按照你说的添加了那些行,但它仍在寻找生产数据库,我还需要在我的deploy.rb文件中添加一行吗? - fatfrog
谢谢你的帮助,我会采纳你建议的更改。但是我的问题在于将nginx.conf设置为服务器的staging而不是production。 - fatfrog
哦,有趣。我不知道那会实际上干扰运行任务的环境。好知道,很高兴你发现了它 :)。 - agmcleod

3

您应该在staging.rb中设置环境变量

set :stage, :staging
set :rails_env, :staging

然后您还需要在Apache虚拟主机文件中使用“PassengerAppEnv”变量设置环境。
  <VirtualHost *:80>                                                           
    ServerName myserver.com                                                 
    # Tell Apache and Passenger where your app's 'public' directory is       
    DocumentRoot /var/www/your_app/current/public                         

    PassengerAppEnv staging
    <Directory /var/www/your_app/current/public>                          
      Allow from all                                                         
      Options -MultiViews                                                    
      Require all granted                                                    
    </Directory>                                                             
</VirtualHost> 

这个解决方法对我也有帮助,希望它能帮助大家面对同样的问题。


1

好的,我犯了一个很愚蠢的错误,但我没有将我的 NGINX 配置文件设置为“Staging”。

     server_name localhost;
     passenger_enabled on;
     rails_env    staging;
     root         /home/myapp;

0

我遇到了类似的问题。在我的情况下,问题与暂存服务器中的.bashrc文件中的ENV变量有关。在这个文件中设置了变量:

RAILS_ENV=production

我改成了:

RAILS_ENV=staging

在你需要重新加载变量:

source ~/.bashrc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接