前提・実現したいこと
circleCIでのrails db:createがエラーになる。
発生している問題・エラーメッセージ
該当部抜粋
log
1Can't connect to MySQL server on 'db' (115) 2Couldn't create 'db/test' database. Please check your configuration. 3rails aborted! 4Mysql2::Error::ConnectionError: Can't connect to MySQL server on 'db' (115)
詳細
log
1/opt/circleci/.pyenv/versions/2.7.12/lib/python2.7/site-packages/paramiko/transport.py:33: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in a future release. 2 from cryptography.hazmat.backends import default_backend 3Starting project_redis_1 ... 4 5Starting project_db_1 ... 6 7 8Starting project_redis_1 ... done 9 10Starting project_db_1 ... done 11^@^@Can't connect to MySQL server on 'db' (115) 12Couldn't create 'db/test' database. Please check your configuration. 13rails aborted! 14Mysql2::Error::ConnectionError: Can't connect to MySQL server on 'db' (115) 15/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect' 16/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `initialize' 17/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `new' 18/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `mysql2_connection' 19/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:889:in `new_connection' 20/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:933:in `checkout_new_connection' 21/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:912:in `try_to_checkout_new_connection' 22/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:873:in `acquire_connection' 23/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:595:in `checkout' 24/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:439:in `connection' 25/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:1121:in `retrieve_connection' 26/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_handling.rb:238:in `retrieve_connection' 27/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_handling.rb:206:in `connection' 28/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/mysql_database_tasks.rb:8:in `connection' 29/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/mysql_database_tasks.rb:16:in `create' 30/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:126:in `create' 31/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:185:in `block in create_current' 32/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:479:in `block (2 levels) in each_current_configuration' 33/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:476:in `each' 34/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:476:in `block in each_current_configuration' 35/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:475:in `each' 36/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:475:in `each_current_configuration' 37/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:184:in `create_current' 38/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/railties/databases.rake:39:in `block (2 levels) in <main>' 39/usr/local/bundle/gems/railties-6.0.2.2/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' 40/usr/local/bundle/gems/railties-6.0.2.2/lib/rails/commands/rake/rake_command.rb:20:in `perform' 41/usr/local/bundle/gems/railties-6.0.2.2/lib/rails/command.rb:48:in `invoke' 42/usr/local/bundle/gems/railties-6.0.2.2/lib/rails/commands.rb:18:in `<main>' 43/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' 44/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' 45/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' 46/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' 47/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' 48/usr/local/bundle/gems/activesupport-6.0.2.2/lib/active_support/dependencies.rb:325:in `block in require' 49/usr/local/bundle/gems/activesupport-6.0.2.2/lib/active_support/dependencies.rb:291:in `load_dependency' 50/usr/local/bundle/gems/activesupport-6.0.2.2/lib/active_support/dependencies.rb:325:in `require' 51bin/rails:4:in `<main>' 52Tasks: TOP => db:create 53(See full trace by running task with --trace) 54 55Exited with code exit status 1
該当のソースコード
yml
1# circleci/config.yml 2version: 2.1 3orbs: 4 ruby: circleci/ruby@0.1.2 5 6jobs: 7 build: 8 machine: 9 image: circleci/classic:edge 10 environment: 11 RAILS_ENV: test 12 COMPOSE_FILE: docker-compose-dev.yml 13 steps: 14 - checkout 15 - run: 16 name: pip upgrade 17 command: pip install --upgrade pip 18 - run: 19 name: update d-c 20 command: pip install docker-compose==1.26.2 21 - run: 22 name: docker version 23 command: docker-compose --version 24 - run: 25 name: d-c build 26 command: docker-compose build 27 - run: 28 name: 'd-c up' 29 command: docker-compose up -d 30 - run: # ここのdb: createで止まる。 31 name: db create 32 command: docker-compose run --rm back rails db:create && docker-compose run --rm back rails db:schema:load 33 - run: 34 name: Rspec 35 command: docker-compose run --rm back bundle exec rspec
yml
1# docker-compose-dev.yml 2version: "3" 3services: 4 front: 5 build: 6 context: front 7 volumes: 8 - ./front:/usr/src/app 9 - node_modules:/usr/src/app/node_modules 10 ports: 11 - "3000:3000" 12 back: 13 build: 14 context: back 15 environment: 16 - RAILS_ENV=${RAILS_ENV} 17 - EDITOR=vi 18 volumes: 19 - ./back:/usr/src/app:delegated 20 ports: 21 - "3001:3001" 22 depends_on: 23 - db 24 - redis 25 db: 26 image: mariadb:10.5.1 27 ports: 28 - "3306" 29 environment: 30 MYSQL_ROOT_PASSWORD: 'pass' 31 volumes: 32 - mariadb-data:/var/lib/mysql 33 redis: 34 image: redis 35 ports: 36 - "6379:6379" 37 volumes: 38 - "./redis:/data" 39 command: redis-server --appendonly yes 40 41volumes: 42 mariadb-data: 43 node_modules:
yml
1# database.yml 2default: &default 3 adapter: mysql2 4 encording: utf8 5 port: 3306 6 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 7 timeout: 5000 8 url: <%= ENV.fetch("DATABASE_URL") { "mysql2://root:pass@db:3306" } %> 9 host: localhost 10 11development: 12 <<: *default 13 database: db/development 14 15# Warning: The database defined as "test" will be erased and 16# re-generated from your development database when you run "rake". 17# Do not set this db to the same as development or production. 18test: 19 <<: *default 20 database: db/test 21 22production: 23 <<: *default 24 database: db/production 25 username: <%= ENV["DB_USER"] %> 26 password: <%= ENV["DB_PASSWORD"] %> 27
試したこと
database.ymlで、urlでの指定を分解して以下のようにしたが、同じエラー。
yml
1adapter: mysql2 2 encording: utf8 3 port: 3306 4 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 5 timeout: 5000 6 username: root 7 password: pass 8 host: 'db' # db, localhost, 'localhost'すべて同じエラー
local環境では、
RAILS_ENV=development, test
両方において問題なくdbと接続できています。
補足情報(FW/ツールのバージョンなど)
開発環境
ruby 2.6.3 Rails 6.0.2.2 docker-compose 1.26.2 mysql2 0.5.3
お手数おかけしますが、よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。