简体   繁体   English

Rails 5 和 AWS Elastic Beanstalk 错误的数据库主机

[英]Rails 5 and AWS Elastic Beanstalk wrong database host

I've configured all the way eb for rails and try to deploy my rails application.我已经为 rails 配置了所有 eb 并尝试部署我的 rails 应用程序。

The first time I deploy my application everything seems to work fine.我第一次部署我的应用程序时,一切似乎都正常。 but my application gets the wrong db host.但是我的应用程序获取了错误的数据库主机。 it was the current ec2 instance ip address.这是当前的 ec2 实例 IP 地址。 the second time I try to deploy, migration didn't work.我第二次尝试部署时,迁移没有奏效。 The database host was this time the ec2 instance ip address.数据库主机这次是 ec2 实例的 IP 地址。

I checked on the instance itself and echoed my RDS_HOSTNAME and it was the correct value.我检查了实例本身并回显了我的 RDS_HOSTNAME,这是正确的值。

Does any one has an idea, why my db host ip is the current ec2 ip address instead the value of RDS_HOSTNAME ?有没有人知道,为什么我的数据库主机 ip 是当前的 ec2 ip 地址而不是 RDS_HOSTNAME 的值?

The Plattform I am using is:我使用的平台是:

Puma with Ruby 2.6 running on 64bit Amazon Linux/2.11.3在 64 位 Amazon Linux/2.11.3 上运行带有 Ruby 2.6 的 Puma

Here is my db configuration:这是我的数据库配置:

 development: adapter: mysql2 encoding: utf8 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: root password: socket: /tmp/mysql.sock database: f_development test: adapter: mysql2 encoding: utf8 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: root password: socket: /tmp/mysql.sock database: f_test production: adapter: mysql2 encoding: utf8 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> database: <%= ENV.fetch('RDS_DB_NAME') %> username: <%= ENV.fetch('RDS_USERNAME') %> password: <%= ENV.fetch('RDS_PASSWORD') %> host: <%= ENV.fetch('RDS_HOSTNAME') %> port: <%= ENV.fetch('RDS_PORT') %>

Here are my eb extensions:这是我的 eb 扩展:

 packages: yum: git: [] openssl-devel: [] ImageMagick-devel: []

 files: # Runs before `./10_bundle_install.sh`: "/opt/elasticbeanstalk/hooks/appdeploy/pre/09_gem_install_bundler.sh" : mode: "000775" owner: root group: users content: | #!/usr/bin/env bash EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir) EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir) # Source the application's ruby, ie 2.6. Otherwise it will be 2.3, which will give this error: `bundler requires Ruby version >= 2.3.0` . $EB_SCRIPT_DIR/use-app-ruby.sh cd $EB_APP_STAGING_DIR echo "Installing compatible bundler" gem install bundler -v 2.1.4

 commands: 01_install_node: command: | sudo curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash - sudo yum -y install nodejs 02_install_yarn: # don't run the command if yarn is already installed (file /usr/bin/yarn exists) test: '[ ! -f /usr/bin/yarn ] && echo "Yarn not found, installing..."' command: | sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo sudo yum -y install yarn

And here is one example of the console output from my rails app:这是我的 rails 应用程序控制台输出的一个示例:

 F, [2020-03-06T07:57:35.544473 #18438] FATAL -- : [f8a1cc9e-57df-409b-a504-a8592168d91c] Mysql2::Error::ConnectionError (Access denied for user 'admin'@'10.0.101.120' (using password: YES)): F, [2020-03-06T07:57:35.544501 #18438] FATAL -- : [f8a1cc9e-57df-409b-a504-a8592168d91c] F, [2020-03-06T07:57:35.544541 #18438] FATAL -- : [f8a1cc9e-57df-409b-a504-a8592168d91c] mysql2 (0.5.3) lib/mysql2/client.rb:90:in `connect' [f8a1cc9e-57df-409b-a504-a8592168d91c] mysql2 (0.5.3) lib/mysql2/client.rb:90:in `initialize' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `try_to_checkout_new_connection' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:814:in `acquire_connection' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:538:in `checkout' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:1033:in `retrieve_connection' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/connection_handling.rb:118:in `retrieve_connection' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/connection_handling.rb:90:in `connection' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/model_schema.rb:466:in `load_schema!' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/attributes.rb:234:in `load_schema!' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/attribute_decorators.rb:51:in `load_schema!' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/model_schema.rb:459:in `block in load_schema' [f8a1cc9e-57df-409b-a504-a8592168d91c] /opt/rubies/ruby-2.6.5/lib/ruby/2.6.0/monitor.rb:235:in `mon_synchronize' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/model_schema.rb:456:in `load_schema' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/model_schema.rb:346:in `attribute_types' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/attribute_methods.rb:230:in `has_attribute?' [f8a1cc9e-57df-409b-a504-a8592168d91c] activerecord (5.2.4.1) lib/active_record/inheritance.rb:55:in `new' [f8a1cc9e-57df-409b-a504-a8592168d91c] devise (4.7.1) app/controllers/devise/sessions_controller.rb:11:in `new' [f8a1cc9e-57df-409b-a504-a8592168d91c] actionpack (5.2.4.1) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action' [f8a1cc9e-57df-409b-a504-a8592168d91c] actionpack (5.2.4.1) lib/abstract_controller/base.rb:194:in `process_action' [f8a1cc9e-57df-409b-a504-a8592168d91c] actionpack (5.2.4.1) lib/action_controller/metal/rendering.rb:30:in `process_action' [f8a1cc9e-57df-409b-a504-a8592168d91c] actionpack (5.2.4.1) lib/abstract_controller/callbacks.rb:42:in `block in process_action' [f8a1cc9e-57df-409b-a504-a8592168d91c] activesupport (5.2.4.1) lib/active_support/callbacks.rb:109:in `block in run_callbacks' [f8a1cc9e-57df-409b-a504-a8592168d91c] audited (4.9.0) lib/audited/sweeper.rb:14:in `around' [f8a1cc9e-57df-409b-a504-a8592168d91c] activesupport (5.2.4.1) lib/active_support/callbacks.rb:118:in `block in run_callbacks' [f8a1cc9e-57df-409b-a504-a8592168d91c] audited (4.9.0) lib/audited/sweeper.rb:14:in `around' [f8a1

Update: Here is the log file when I try to deploy to elastic beanstalk the second time:更新:这是我第二次尝试部署到弹性 beantalk 时的日志文件:

 2020-03-06 08:32:36 INFO Environment update is starting. 2020-03-06 08:32:39 INFO Deploying new version to instance(s). 2020-03-06 08:34:18 ERROR [Instance: i-020c0153964e91b93] Command failed on instance. Return code: 1 Output: (TRUNCATED)...or::ConnectionError: Access denied for user 'admin'@'10.0.101.120' (using password: YES) /opt/rubies/ruby-2.6.5/bin/bundle:23:in `load' /opt/rubies/ruby-2.6.5/bin/bundle:23:in `<main>'

Access denied for user 'admin'@'10.0.101.120' (using password: YES)

this issue relates to mysql users' permission.这个问题与mysql用户的权限有关。 Are you sure that you have a user 'admin' in RDS instance and this user has granted permission ?您确定您在 RDS 实例中有一个用户 'admin' 并且该用户已授予权限吗?

Does any one has an idea, why my db host ip is the current ec2 ip address instead the value of RDS_HOSTNAME ?

how about config : url: <%= ENV['DATABASE_URL'] %> in production and add a configuration environment property: config : url: <%= ENV['DATABASE_URL'] %>在生产中怎么样,并添加一个配置环境属性:

DATABASE_URL=mysql2://myuser:mypass@your_rds_instance.rds.amazonaws.com:3306/your_database

in your eb environment to point to the exactly rds instance you want.在您的 eb 环境中指向您想要的确切 rds 实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM