【问题标题】:CircleCI config issue with MySQLMySQL 的 CircleCI 配置问题
【发布时间】:2019-09-13 22:20:48
【问题描述】:

我正在使用 CircleCI 构建我的 Ruby on Rails 应用程序。

在构建应用程序时,我收到错误

rails aborted!
Mysql2::Error::ConnectionError: Can't connect to local MySQL server through
socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")

config.yml

version: 2
jobs:
  build:
    parallelism: 3
    docker:
      - image: circleci/ruby:2.6.2-stretch
        environment:
          BUNDLE_JOBS: 3
          BUNDLE_RETRY: 3
          BUNDLE_PATH: vendor/bundle
          PGHOST: 127.0.0.1
          PGUSER: circleci-demo-ruby
          RAILS_ENV: test
          MYSQL_ROOT_PASSWORD: password
          MYSQL_HOST: 127.0.0.1
          MYSQL_DB: rails_chat_tutorial
          MYSQL_USER: root
          MYSQL_ALLOW_EMPTY_PASSWORD: true
          MYSQL_PASSWORD:
      - image: mysql:5.7
        command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_bin --innodb-large-prefix=true --innodb-file-format=Barracuda
        environment:
          MYSQL_USER: root
          MYSQL_ALLOW_EMPTY_PASSWORD: true
    steps:
      - checkout
      - run: sudo apt-get install mysql-client
     
      
      # Which version of bundler?
      - run:
          name: Which bundler?
          command: bundle -v

      # Restore bundle cache
      - restore_cache:
          keys:
            - rails-demo-bundle-v2-{{ checksum "Gemfile.lock" }}
            - rails-demo-bundle-v2-

      - run:
          name: Bundle Install
          command: bundle check || bundle install

      # Store bundle cache
      - save_cache:
          key: rails-demo-bundle-v2-{{ checksum "Gemfile.lock" }}
          paths:
            - vendor/bundle

      - run:
          name: Wait for DB
          command: dockerize -wait tcp://localhost:3306 -timeout 1m

      - run:
          name: Mysql database
          command: mysql  -h 127.0.0.1 -u root  -e "create database rails_chat_tutorial;"    

      - run:
          name: Database setup
          command: bin/rails db:migrate

database.yml

default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: 

development:
  <<: *default
  database: rails_chat_tutorial

【问题讨论】:

  • 我注意到大多数环境变量都是为您的构建容器设置的,但几乎没有为您的数据库容器设置任何环境变量。例如,数据库容器如何知道您想要一个空密码?
  • 另外,我会从 MySQL command 开始,以防万一。开始使用普通的默认数据库,然后进行自定义调整。
  • 你解决过这个问题吗?我肯定没有
  • 我改成postgres

标签: ruby-on-rails circleci


【解决方案1】:

根据What is Socket Declaration in RoR for?,似乎有两种方法可以连接到数据库,一种是通过套接字,另一种是通过 TCP/IP。当一切都在一台计算机本地机器上时,套接字工作正常。由于 CircleCI 对 Ruby 和 Mysql 使用不同的图像,因此请改用 TCP 连接。

我必须将database.yml 更改如下:

database.yml

  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  host: <%= ENV['DB_HOST'] || 'localhost' %>

test:
  <<: *default
  database: app_test

然后是 CircleCI 配置:

circleci/config.yml

jobs:
  build:
    docker:
      - image: circleci/ruby:2.6.4
        environment:
          DB_HOST: 127.0.0.1
      - image: circleci/mysql:8.0.4
        command: [--default-authentication-plugin=mysql_native_password]
        environment:
          MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
          MYSQL_ROOT_HOST: '%'
          MYSQL_DATABASE: app_test
          MYSQL_USER: root

我希望这对你有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-17
    • 2018-05-03
    • 1970-01-01
    • 2015-06-21
    • 2016-06-26
    • 2015-01-08
    • 2022-08-11
    • 2011-01-07
    相关资源
    最近更新 更多