【问题标题】:Cannot connect to local MySQL server using socket (2) in Elastic beanstalk using RDS database无法使用 RDS 数据库在 Elastic beanstalk 中使用套接字 (2) 连接到本地 MySQL 服务器
【发布时间】:2026-01-01 09:00:01
【问题描述】:

我正在使用代码管道为在 Elastic Beanstalk 中运行的 Ruby on Rails 应用程序创建 CI\CD。我的代码构建成功,但部署到 EBS 提供程序失败并出现以下错误

+ cd /var/app/ondeck
  + su -s /bin/bash -c 'bundle exec /opt/elasticbeanstalk/support/scripts/check-for-rake-task.rb db:migrate' webapp
  `/home/webapp` is not a directory.
  Bundler will use `/tmp/bundler20211229-11836-pzl6dq11836' as your home directory temporarily.
  + '[' false = true ']'
  + su -s /bin/bash -c 'leader_only bundle exec rake db:migrate' webapp
  `/home/webapp` is not a directory.
  Bundler will use `/tmp/bundler20211229-11840-9hxmcz11840' as your home directory temporarily.
  rake aborted!
  Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
  /opt/rubies/ruby-2.6.7/bin/bundle:23:in `load'
  /opt/rubies/ruby-2.6.7/bin/bundle:23:in `<main>'
  Tasks: TOP => db:migrate
  (See full trace by running task with --trace) (Executor::NonZeroExitStatus)

我有

mysql --version
mysql  Ver 14.14 Distrib 5.5.62, for Linux (x86_64) using readline 5.1 

并且可以连接到外部 RDS MySQL Aurora 数据库。

我的 Buildspec.yml:

version: 0.2
    phases:
      install:
        runtime-versions:
            ruby: 2.6
        commands:
          - echo Installing Bundler...
          - gem install bundler
          - bundle install
      pre_build:
        commands:
          - echo Preparing database to run tests... 
          - bundle exec rails db:create --trace RAILS_ENV=production
      build:
        commands:
          - echo Running tests...
          - bundle exec rails test/test_helper.rb
    artifacts:
      files:
        - '**/*'

我的数据库.yml:

default: &default
  adapter: mysql2
  encoding: utf8mb4
  collation: utf8mb4_unicode_ci
  pool: 5
  timeout: 5000
  username: <%= ENV['DB_USER'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: <%= ENV['DB_HOST'] %>
  port: <%= ENV['DB_PORT'] %>

development:
  <<: *default
  database: <%= ENV['DB'] %>

test:
  <<: *default
  database: <%= ENV['DB_TEST'] %>

production:
  <<: *default
  database: <%= ENV['DB'] %>`

【问题讨论】:

  • 如果是外部数据库,为什么要连接到本地?将您的应用设置为连接到远程主机,而不是本地主机。
  • 是的..我可以理解,但是当我运行我的代码管道以部署到 aws elastic beanstalk 时,它会因提到的错误而失败。
  • 我在 ec2 实例中尝试了以下配置,但问题仍然存在。 [ec2-user@ip-xx.xx.xx 等]$ cat my.cnf [mysqld] datadir=/var/lib/mysql #socket=/var/lib/mysql/mysql.sock

标签: mysql ruby-on-rails amazon-web-services amazon-elastic-beanstalk amazon-rds


【解决方案1】:

我找到了从 Rails EB SSH 连接到 mysql 服务器的类似问题的答案,它可能仍然对您有所帮助。基本上,Elastic Beanstalk 没有成功使用我的环境的 envars 在eb ssh 实例访问中运行我的bundle exec rails console 命令。

我通过在 eb ssh 实例访问中运行的任何 rails 命令显式添加所有必需的 envar 来解决我的问题。因此,例如,为了运行您的 su -s /bin/bash -c 'leader_only bundle exec rake db:migrate' webapp 命令,您可能必须更改它以运行类似以下内容:

su -s /bin/bash -c 'leader_only RAILS_MASTER_KEY=xxxxxxx RAILS_ENV=production DB_HOST=xxxxxxx DB_PASSWORD=xxxxxxx DB_USER=xxxxxxx DB=xxxxxx AWS_REGION=xxxxxxx AWS_BUCKET=xxxxxxx bundle exec rake db:migrate' webapp

将上面的xxxxxxxs 替换为您的 EB > 配置 > 软件选项卡中相应变量的值,您应该能够以这种方式连接到远程数据库。之后,您将能够运行迁移命令、rake 任务和其他依赖于数据库的功能。我不熟悉 CI/CD,但我希望此解决方案的原理适用于您的项目。

【讨论】:

    【解决方案2】:

    感谢您的回答。但是我遇到了这个问题,因为 git 中的 master 分支没有正确更新。 在使用正确的应用程序代码更新主分支后,CI\CD 按预期启动,但由于 env 和凭证文件不在 git 中而卡在两者之间。 我创建了一个 s3 存储桶来存储 env 文件,并在将其打包以进行 EBS 部署时引用了这些文件。 希望这对将来的人有所帮助:)

    【讨论】: