Rails db: переход по умолчанию к локальному сокету в производственной среде - PullRequest
0 голосов
/ 21 января 2019

Мне нужна помощь, чтобы решить эту проблему.Я установил рельсы на AWS несколько раз и не сталкивался с этим.Буду признателен за любую помощь, чтобы указать мне правильное направление.

У меня есть экземпляр AWS EB / EC2, работающий на Ruby 2.5.3 с RDS MySQL.Развертывание продолжает сбиваться, как только он попадает в миграцию базы данных, жалуясь:

Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
bin/rails:9:in `<main>'
Tasks: TOP => db:migrate

Я добавил RAILS_SKIP_MIGRATIONS=true, чтобы я мог запустить команду вручную из командной строки.Когда я подключаюсь к экземпляру EC2, я могу успешно подключиться к серверу RDS с помощью mysql -u dbadmin -p -h xxx

[ec2-user@rails]$ RAILS_ENV=production bundle exec rails db:migrate, который дает мне ту же ошибку результата, что и выше.

[ec2-user@rails]$ RAILS_ENV=production bundle exec rails db:migrate --trace Дает большето же самое:

** Invoke db:migrate (first_time)
** Invoke db:load_config (first_time)
** Invoke environment (first_time)
** Execute environment
Rails Error: Unable to access log file. Please ensure that /var/app/current/log/production.log exists and is writable (ie, make it writable for user and group: chmod 0664 /var/app/current/log/production.log). The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
** Execute db:load_config
** Execute db:migrate
rails aborted!
Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `connect'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `initialize'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:811:in `new_connection'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:855:in `checkout_new_connection'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:834:in `try_to_checkout_new_connection'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:795:in `acquire_connection'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:523:in `checkout'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:1010:in `retrieve_connection'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_handling.rb:90:in `connection'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/tasks/database_tasks.rb:172:in `migrate'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/railties/databases.rake:60:in `block (2 levels) in <main>'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/rake-12.3.2/lib/rake/task.rb:273:in `block in execute'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/rake-12.3.2/lib/rake/task.rb:273:in `each'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/rake-12.3.2/lib/rake/task.rb:273:in `execute'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/rake-12.3.2/lib/rake/task.rb:214:in `block in invoke_with_call_chain'
/opt/rubies/ruby-2.5.3/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/rake-12.3.2/lib/rake/task.rb:194:in `invoke_with_call_chain'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/rake-12.3.2/lib/rake/task.rb:183:in `invoke'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/rake-12.3.2/lib/rake/application.rb:160:in `invoke_task'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/rake-12.3.2/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/rake-12.3.2/lib/rake/application.rb:116:in `each'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/rake-12.3.2/lib/rake/application.rb:116:in `block in top_level'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/rake-12.3.2/lib/rake/application.rb:125:in `run_with_threads'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/rake-12.3.2/lib/rake/application.rb:110:in `top_level'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.2/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/rake-12.3.2/lib/rake/application.rb:186:in `standard_exception_handling'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.2/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.2/lib/rails/command.rb:48:in `invoke'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.2/lib/rails/commands.rb:18:in `<main>'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `block in require'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:257:in `load_dependency'
/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `require'
bin/rails:9:in `<main>'
Tasks: TOP => db:migrate

Вот версия гема:

[ec2-user@rails]$ bundle info mysql2
  * mysql2 (0.5.2)
    Summary: A simple, fast Mysql library for Ruby, binding to libmysql
    Homepage: https://github.com/brianmario/mysql2
    Path: /opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2

Вот соответствующие переменные ENV от printenv:

RDS_HOSTNAME=nework.XXXXXXXXXXXXXXXX.ap-southeast-1.rds.amazonaws.com
HOSTNAME=ip-172-31-XX-XXX
RDS_DB_NAME=nework
GEM_HOME=/home/ec2-user/.gem/ruby/2.5.3
EC2_AMITOOL_HOME=/opt/aws/amitools/ec2
SSH_CLIENT=117.2.XXX.XXX 36150 22
BUNDLE_WITHOUT=test:development
USER=ec2-user
RDS_PASSWORD=XXXXXXXXXXXXXX
RDS_USERNAME=dbadmin
RAILS_SKIP_MIGRATIONS=true
RACK_ENV=production
PATH=/home/ec2-user/.gem/ruby/2.5.3/bin:/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/bin:/opt/rubies/ruby-2.5.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/.local/bin:/home/ec2-user/bin
SECRET_KEY_BASE=XXXXX
RUBY_ENGINE=ruby
RAILS_ENV=production
AWS_PATH=/opt/aws
GEM_ROOT=/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0
GEM_PATH=/home/ec2-user/.gem/ruby/2.5.3:/opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0
SSH_CONNECTION=117.2.XXX.XXX 36150 172.31.XX.XX 22
RUBY_ROOT=/opt/rubies/ruby-2.5.3
RAILS_SKIP_ASSET_COMPILATION=false
RUBY_VERSION=2.5.3
_=/usr/bin/printenv

Это мой database.yml:

#  
#  
  default: &default
    adapter: mysql2
    encoding: utf8
    host: localhost  
    pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
    username: <%= ENV.fetch('RDS_USERNAME') { 'dbadmin'} %>
    password: <%= ENV.fetch('RDS_PASSWORD') { 'password'}%>

  development:
    <<: *default
    socket: <%= ENV.fetch('DB_SOCKET') { '/tmp/mysqld.sock'}%>
    database: <%= ENV.fetch('RDS_DB_NAME') { 'neuework'}%>

  test:
    <<: *default
    database: neuwork_test
    socket: <%= ENV.fetch('DB_SOCKET') { '/tmp/mysqld.sock'}%>

  production:
    <<: *default
    host: <%= ENV['RDS_HOSTNAME'] %>
    database: <%= ENV['RDS_DB_NAME'] %>
    password: <%= ENV['RDS_PASSWORD'] %>
...