简体   繁体   中英

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.

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. the second time I try to deploy, migration didn't work. The database host was this time the ec2 instance ip address.

I checked on the instance itself and echoed my RDS_HOSTNAME and it was the correct value.

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

The Plattform I am using is:

Puma with Ruby 2.6 running on 64bit Amazon Linux/2.11.3

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:

 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:

 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:

 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. Are you sure that you have a user 'admin' in RDS instance and this user has granted permission ?

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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