将Rails 5应用程序部署到EC2-RDS数据库实例连接错误

时间:2018-05-02 07:23:28

标签: ruby-on-rails postgresql amazon-web-services amazon-rds

我已经创建了数据库实例,并按照文档完成了aws rds实例的配置过程。 成功创建了db的实例。 还相应地更改了database.yml中的连接参数。 我在aws上安装在ubuntu 16.04上的ngnix服务器上运行了以下命令,并且还多次重启了数据库实例。

RAILS_ENV=production rake db:create

并且db已成功创建 然后跑

RAILS_ENV=production rake db:migrate

并且数据库已成功迁移。 然后运行命令 sudo服务nginx重启 经过所有的过程 当我访问我的应用程序时,它说

PG::ConnectionBad
could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP
connections on port 5432?

这是我的database.yml

development:
  host: localhost
  database: ups_developement_db
  adapter: postgresql
  encoding: unicode
  port: 5432
  username: postgres
  password: postgres
production:
  host: dbcustom.cjocvmnblsa4.us-east-2.rds.amazonaws.com
  database: ups_db
  adapter: postgresql
  encoding: unicode
  port: 5432
  username: dbmasteruser
  password: dbmasterPassword

我愿意提供所需的任何信息。 非常感谢您提前花时间和解决方案

2 个答案:

答案 0 :(得分:0)

几乎可以肯定您没有正确配置RDS实例上的安全组。

登录您的EC2并发出命令

telnet <rds hostname> 5432

您应该看到类似

的内容
Trying 10.1.1.2...
Connected to 10.1.1.2.
Escape character is '^]'.

这意味着TCP可以在您的EC2和您的RDS之间流动。

如果没有,您需要通过添加正确的安全组配置来允许EC2连接到您的RDS实例。

答案 1 :(得分:0)

您应该在AWS RDS实例中创建Environment变量,应该在rails应用程序配置文件中使用它们。 Check here

database.yml应该看起来像这样

production:
  adapter: mysql2
  encoding: utf8
  database: <%= ENV['RDS_DB_NAME'] %>
  username: <%= ENV['RDS_USERNAME'] %>
  password: <%= ENV['RDS_PASSWORD'] %>
  host: <%= ENV['RDS_HOSTNAME'] %>
  port: <%= ENV['RDS_PORT'] %>