Rails 5 и AWS Elasti c Beanstalk неверный хост базы данных - PullRequest
0 голосов
/ 06 марта 2020

Я полностью настроил eb для rails и пытаюсь развернуть приложение rails.

При первом развертывании приложения все работает нормально. но мое приложение получает неправильный хост БД. это был текущий IP-адрес экземпляра ec2. во второй раз я пытаюсь развернуть, миграция не сработала. На этот раз хостом базы данных был IP-адрес экземпляра ec2.

Я проверил сам экземпляр и повторил мой RDS_HOSTNAME, и это было правильное значение.

Есть ли у кого-нибудь идея, почему мой ip хоста db является текущим ip-адресом ec2 вместо значения RDS_HOSTNAME?

Используемая мной платформа:

Puma с Ruby 2.6 на 64-битной Amazon Linux / 2.11.3

Вот моя конфигурация БД:

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') %>

Вот мои расширения 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, i.e. 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

И вот один пример вывода консоли из моего приложения 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

Обновление: вот файл журнала, когда я пытаюсь развернуть elasti c beanstalk во второй раз:

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>'

1 Ответ

1 голос
/ 07 марта 2020

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

эта проблема относится к mysql разрешению пользователей. Вы уверены, что у вас есть пользователь 'admin' в экземпляре RDS, и этот пользователь предоставил разрешение?

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

как насчет конфигурации: url: <%= ENV['DATABASE_URL'] %> в рабочей среде и добавление среды конфигурации свойство:

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

в вашей среде eb для указания именно того экземпляра rds, который вы хотите.

...