MySQL的CircleCI配置问题

时间:2019-04-24 14:59:45

标签: ruby-on-rails circleci

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

在构建应用程序时,出现错误

window.location = 'http://example.com/getFirstFile';

setTimeout(function() {
   window.location = 'http://example.com/getSecondFile';
}, 1000);

config.yml

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")

database.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

1 个答案:

答案 0 :(得分:1)

根据此What is Socket Declaration in RoR for?,似乎有两种连接到DB的方法,一种是通过套接字,另一种是通过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

我希望这对您有用