Rails & Capistrano 3 - 登台服务器尝试使用生产数据库

Rails & Capistrano 3 - staging server trying to user production database

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

  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'

如何强制它使用登台数据库?

编辑

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

在我的 cap 3 设置中,我有一个 config/deploy/production.rb 文件用于设置环境。您是否对分期和制作都做同样的事情?

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

需要确保它设置了正确的环境,以便数据库任务知道要连接到哪个数据库。

好的,这是一个非常愚蠢的错误,但我没有将我的 NGINX 配置文件设置为 "Staging"

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

您应该在 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> 

这为我解决了同样的问题。我希望它也能帮助每个人面对它。

我遇到过类似的问题。在我的例子中,问题与登台服务器中 .bashrc 文件中的 ENV 变量有关。在此文件中设置了变量:

RAILS_ENV=production

我改为:

RAILS_ENV=staging

需要重新加载变量后:

source ~/.bashrc