我无法使用 Elastic Beanstalk 将我的(第二个)Rails 5.2 应用程序部署到 AWS。代码部署正常,但 PostgreSQL 9.4.18 数据库出现了问题。
EB 正确创建了 RDS 实例。我的 database.yml 的相关部分如下:
production:
<<: *default
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>
这些 ENV 变量都填充在 EB 网络界面中。
我能使用 pgAdmin III 连接到 RDS 服务器,并添加入站规则以允许我的 IP 地址访问。
但问题是:通过 SSH 进入 EC2 后我无法创建数据库。
当我输入
rails db:seed
或者
rails db:reset
我收到此错误:
Traceback (most recent call last):
10: from bin/rails:4:in `<main>'
9: from /opt/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `require'
8: from /opt/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:253:in `load_dependency'
7: from /opt/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `block in require'
6: from /opt/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
5: from /opt/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
4: from /opt/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
3: from /opt/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
2: from /opt/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
1: from /opt/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.1/lib/bootsnap/compile_cache/iseq.rb:37:in `load_iseq'
/opt/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.1/lib/bootsnap/compile_cache/iseq.rb:37:in `fetch': Permission denied - bs_fetch:atomic_write_cache_file:open (Errno::EACCES)
当我尝试
sudo env PATH=$PATH RAILS_ENV=production bundle exec rake db:seed
我收到这个错误
rake aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
/opt/rubies/ruby-2.5.1/bin/bundle:23:in `load'
/opt/rubies/ruby-2.5.1/bin/bundle:23:in `<main>'
Tasks: TOP => db:abort_if_pending_migrations
(See full trace by running task with --trace)
对我来说相当不透明,因为幸运的是我不需要经常这样做。
由于我之前的应用程序可以运行(尽管是在 Ruby 的早期版本上),因此我比较了 RDS、配置和我能想到的所有其他 AWS 设置。我遗漏了什么?
答案1
遇到了同样的问题,以下是最终对我有用的方法。
除了使用 benchwarmer 在上面这个答案中提供的命令之外:
https://stackoverflow.com/a/17232607/1216245
我必须运行种子命令,为主密钥和所有 rds 设置提供环境变量。
bundle exec rake db:seed RAILS_ENV=production RAILS_MASTER_KEY=<your master key> RDS_HOSTNAME=<your rds hostname> RDS_PASSWORD=<...> RDS_USERNAME=<...> RDS_DB_NAME=<...> RDS_PORT=<...>
您可以在 AWS 控制台(仪表板)中您环境的配置面板中检查所有这些内容。