加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails控制台无法连接到数据库,但rake任务可以

发布时间:2020-12-17 03:50:10 所属栏目:百科 来源:网络整理
导读:我有一个使用单击DO图像的rails应用程序. 我可以运行任何rake db:*成功但是当我运行rails console时我无法连接到db. 我该如何解决?该应用程序运行正常.所以我认为是一个rails控制台问题. 控制台输出: deployer:/home/rails$RAILS_ENV=production bundle e
我有一个使用单击DO图像的rails应用程序.

我可以运行任何rake db:*成功但是当我运行rails console时我无法连接到db.

我该如何解决?该应用程序运行正常.所以我认为是一个rails控制台问题.

控制台输出:

deployer:/home/rails$RAILS_ENV=production bundle exec rake db:migrate
  ActiveRecord::SchemaMigration Load (3.2ms)  SELECT "schema_migrations".* FROM "schema_migrations"

deployer:/home/rails$echo $APP_DATABASE_PASSWORD
[redacted database password]

deployer:/home/rails$RAILS_ENV=production bundle exec rails console
Running via Spring preloader in process 25038
Loading production environment (Rails 4.2.6)
2.3.0 :001 > User.connection
PG::ConnectionBad: fe_sendauth: no password supplied

database.yml的

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  host: localhost
  username: rails
  password: <%= ENV['APP_DATABASE_PASSWORD'] %>

production:
  <<: *default
  database: production
  username: rails
  password: <%= ENV['APP_DATABASE_PASSWORD'] %>

编辑

环顾四周,我发现ActiveRecord :: Base.configurations的密码为:nil.为什么服务器进程会读取密码,但控制台却没有?

我将ActiveRecord :: Base.configurations添加到初始化程序中.初始化时,密码存在.

在控制台上ActiveRecord :: Base.configurations按预期返回所有信息,密码除外.我甚至已经重新加载默认变量.的/ etc /默认/麒麟

编辑2

RAILS_ENV =生产包exec rails console上的ActiveRecord :: Base.configurations

{"default"=>
  {"adapter"=>"postgresql","encoding"=>"unicode","pool"=>5,"host"=>"localhost","username"=>"rails","password"=>nil},"development"=>
  {"adapter"=>"postgresql","password"=>nil,"database"=>"development"},"test"=>
  {"adapter"=>"postgresql","database"=>"test"},"production"=>
  {"adapter"=>"postgresql","database"=>"production"}}

App_DATABASE_PASSWORD上的ActiveRecord :: Base.configurations =密码RAILS_ENV =生产包exec rails console

{"default"=>
  {"adapter"=>"postgresql","database"=>"production"}}

RAILS_ENV =生产包exec rails服务器上的ActiveRecord :: Base.configurations

{"default"=>
  {"adapter"=>"postgresql","password"=>[password]},"password"=>[password],"database"=>"production"}}

解决方法

事实证明,重要的区别是Spring预加载器.在定义ENV变量之前,您似乎已经启动了Spring.

尝试杀死Spring并重新启动控制台.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读