连接到错误的数据库

连接到错误的数据库

本文介绍了ActiveRecord 尝试使用 rbenv 连接到错误的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在站点根目录上执行 get 请求时 /log/production.log 中出现错误:

Getting an error in /log/production.log when performing get request on site root:

I, [2016-03-21T02:21:38.485274 #12750]  INFO -- :
    Started GET "/" for 174.xx.xxx.xxx at 2016-03-21 02:21:38 -0600
F, [2016-03-21T02:21:38.493250 #12750] FATAL -- :
    ActiveRecord::NoDatabaseError (FATAL:  database "y" does not exist)

它说数据库y"不存在.首先,y 不是数据库,所以我知道它不存在.

It says database "y" does not exist. First off, y is not a database, so I know it doesn't exist.

其次,database.ymlydb 指定为应用程序应连接到的数据库 - 而不是 y.

Secondly, database.yml specifies ydb as the database that the app should connect to - not y.

config/database.yml:

production:
  adapter: postgresql
  encoding: utf8
  host: <%= ENV['Y_PG_HOST'] %>
  database: <%= ENV['Y_PG_DB'] %>
  username: <%= ENV['Y_PG_USER'] %>
  password: <%= ENV['Y_PG_PASS'] %>

使用 rbenv 声明环境变量:

Using rbenv to declare the env vars:

.rbenv-vars

  Y_PG_HOST=localhost
  Y_PG_DB=ydb
  Y_PG_USER=y
  Y_PG_PASS=*********

更新

感谢@Meshpi,看来rbenv 是问题发生的地方.当环境变量直接放在 database.yml 中时,服务器会按预期加载站点.

Thanks to @Meshpi, it appears that rbenv is where the issue is occurring. When the env vars are placed directly in database.yml, the server loads the site as expected.

令人费解的是,从 echo $Y_PG_DB 中,shell 返回的是 ydb,而不是 y.

What is puzzling is that from echo $Y_PG_DB, the shell returns ydb, not y.

推荐答案

开发工作正常.甚至服务器上的 rails 控制台生产 也与 app.get '/' 一起使用,没有数据库错误.然而,当通过浏览器提出请求时,一切都崩溃了.

Development worked fine. Even rails console production on the server worked with app.get '/' with no database errors. However, when putting the request through the browser, all hell broke loose.

问题是 Phusion Passenger 在安装结束时给出的说明与 rbenv-vars 不完全兼容.

The problem is that the instructions that Phusion Passenger gives at the end of its installation are not exactly compatible with rbenv-vars.

在Passenger 安装结束时,系统会指示您在nginx.conf 中添加以下内容:

At the end of the Passenger installation, you are instructed to add the following to nginx.conf:

passenger_root /home/user/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/passenger-5.0.26;
passenger_ruby /home/user/.rbenv/versions/2.3.0/bin/ruby;

然而,正如@mislav 指出,Ruby 脚本将是直接启动 ruby​​ 进程,而不是通过 rbenv.

However, then as @mislav pointed out, the Ruby scripts would be spinning up the ruby processes directly instead of through rbenv.

要解决这个问题,nginx.conf 中需要的是:

To fix this, what needs to be in nginx.conf instead is:

passenger_root /home/user/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/passenger-5.0.26;
passenger_ruby /home/user/.rbenv/shims/ruby;

希望这有助于拯救另一个毫无戒心的灵魂:D

Hope this helps spare another unsuspecting soul :D

这篇关于ActiveRecord 尝试使用 rbenv 连接到错误的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:36