前提・実現したいこと
・環境
docker 3
ruby 2.5.7
rails 5.2.4
・やりたいこと
rails アプリケーションをgithubにpushした際にcircleciでrspecのテストを実行したい
しかし下記のエラーが出ているためうまくいかずに困っています。
原因分かるかたがお見えになりましたら、ご教授いただければと思います。
発生している問題・エラーメッセージ
circleci上でbundle exec rake db:createした時にMysql2::Error::ConnectionError: が発生する
# circleci #!/bin/bash -eo pipefail bundle exec rake db:create Access denied for user 'root'@'127.0.0.1' (using password: YES) Couldn't create 'myapp_test' database. Please check your configuration. rake aborted! Mysql2::Error::ConnectionError: Access denied for user 'root'@'127.0.0.1' (using password: YES) /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `initialize' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `try_to_checkout_new_connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:814:in `acquire_connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:538:in `checkout' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1033:in `retrieve_connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_handling.rb:118:in `retrieve_connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_handling.rb:90:in `connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/tasks/mysql_database_tasks.rb:6:in `connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/tasks/mysql_database_tasks.rb:14:in `create' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/tasks/database_tasks.rb:119:in `create' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/tasks/database_tasks.rb:139:in `block in create_current' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/tasks/database_tasks.rb:316:in `block in each_current_configuration' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/tasks/database_tasks.rb:313:in `each' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/tasks/database_tasks.rb:313:in `each_current_configuration' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/tasks/database_tasks.rb:138:in `create_current' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/railties/databases.rake:29:in `block (2 levels) in <top (required)>' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/rake-13.0.1/exe/rake:27:in `<top (required)>' /usr/local/bin/bundle:30:in `block in <main>' /usr/local/bin/bundle:22:in `<main>' Tasks: TOP => db:create (See full trace by running task with --trace) Exited with code exit status 1
該当のソースコード
#dockerfile FROM ruby:2.5.7 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs WORKDIR /myapp COPY Gemfile /myapp/Gemfile COPY Gemfile.lock /myapp/Gemfile.lock RUN bundle install COPY . /myapp
#docker-compose.yml version: '3' services: db: image: mysql:5.7 ports: - 4306:3306 volumes: - db-volume:/var/lib/mysql environment: - MYSQL_ROOT_PASSWORD=password - MYSQL_HOST=127.0.0.1 web: build: . command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" volumes: - .:/myapp - .:/rspec_sample ports: - "3000:3000" depends_on: - db tty: true stdin_open: true volumes: db-volume:
#datebase.yml default: &default adapter: mysql2 encoding: utf8 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: root password: password port: 3306 host: db socket: /var/lib/mysql/mysql.sock development: <<: *default database: myapp_development # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default database: myapp_test username: <%= ENV['DB_USERNAME'] || 'root' %> password: <%= ENV['DB_PASSWORD'] || 'password' %> host: <%= ENV['DB_HOST'] || 'db' %>
#.circleci/config.yml version: 2 orbs: codecov: codecov/codecov@1.0.5 jobs: build: docker: # specify the version you desire here - image: circleci/ruby:2.5.7-node-browsers environment: RAILS_ENV: test DB_HOST: '127.0.0.1' DB_USERNAME: root DB_PASSWORD: password # Specify service dependencies here if necessary # CircleCI maintains a library of pre-built images # documented at https://circleci.com/docs/2.0/circleci-images/ - image: circleci/mysql:5.7 environment: RAILS_ENV: test DB_HOST: 127.0.0.1 DB_USERNAME: root DB_PASSWORD: password working_directory: ~/repo steps: - checkout # Download and cache dependencies - restore_cache: keys: - v1-dependencies-{{ checksum "Gemfile.lock" }} # fallback to using the latest cache if no exact match is found - v1-dependencies- - run: name: install dependencies command: | bundle install --jobs=4 --retry=3 --path vendor/bundle - save_cache: paths: - ./vendor/bundle key: v1-dependencies-{{ checksum "Gemfile.lock" }} - run: name: Wait for DB command: dockerize -wait tcp://127.0.0.1:3306 -timeout 120s # Database setup - run: bundle exec rake db:create - run: bundle exec rake db:schema:load # run tests! - run: name: run tests command: | mkdir /tmp/test-results TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)" bundle exec rspec --format progress \ --format RspecJunitFormatter \ --out /tmp/test-results/rspec.xml \ --format progress \ $TEST_FILES # collect reports - store_test_results: path: /tmp/test-results - store_artifacts: path: /tmp/test-results destination: test-results # run rubocop - run: name: run rubocop command: bundle exec rubocop
試したこと
環境変数をいじったり、ソケットやホストの見直しを行ったが上記のエラーが消えなかった
具体的に何でエラーが発生しているのか解らない状況なので質問させてください
あなたの回答
tips
プレビュー