Rails db:migrate在生产环境中默认为本地套接字连接

时间:2019-01-21 00:32:52

标签: mysql ruby-on-rails amazon-web-services amazon-ec2 rails-migrations

我需要一些帮助来解决此问题。我之前在AWS上安装过几次导轨,但并未遇到此问题。为我指明正确方向的任何帮助将不胜感激。

我有一个运行Ruby 2.5.3和RDS MySQL的AWS EB / EC2实例。部署一旦进入数据库迁移,便会继续笨拙,抱怨如下:

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,以便可以从命令行手动运行命令。 SSH进入EC2实例时,我可以使用mysql -u dbadmin -p -h xxx

成功连接到RDS服务器。

[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

以下是gem版本:

[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

以下是来自printenv的相关ENV变量:

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

这是我的数据库。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'] %>

1 个答案:

答案 0 :(得分:0)

可以解决一些问题,而不是数据库连接问题,这是环境变量问题。

即使部署是使用EB控制台集变量(例如secret_key_base和skip迁移,并且在printenv中也清晰可见),数据库变量也完全不会被rails接收。我通过创建一个静态database.yml来解决问题,该数据库存储在S3存储桶中,并在运行数据库迁移脚本之前与.ebextensions一起复制。

我怀疑puma正在读取错误的环境变量-但我找不到发生这种情况的地方,而且我的解决方法无论如何可能是一种有效的方法。