Home>

Rails db: create on circleCI gives an error.

Excerpt from the relevant part

Can't connect to MySQL server on'db' (115)
Couldn't create'db/test' database. Please check your configuration.
rails aborted!
Mysql2 :: Error :: ConnectionError: Can't connect to MySQL server on'db' (115)


Details

/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.
  from cryptography.hazmat.backends import default_backend
Starting project_redis_1 ...
Starting project_db_1 ...

Starting project_redis_1 ... done
Starting project_db_1 ... done
^ @ ^ @ Can't connect to MySQL server on'db' (115)
Couldn't create'db/test' database. Please check your configuration.
rails aborted!
Mysql2 :: Error :: ConnectionError: Can't connect to MySQL server on'db' (115)
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect'
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in ʻinitialize'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `new'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `mysql2_connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:889:in `new_connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:933:in `checkout_new_connection'
/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'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:873: in ʻacquire_connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:595:in `checkout'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:439:in `connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb11212: in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_handling.rb:238:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_handling.rb:206:in `connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/mysql_database_tasks.rb:8:in `connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/mysql_database_tasks.rb:16:in `create'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:126:in `create'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:185: in `block in create_current'
/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'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:476:in ʻeach'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:476:in `block in each_current_configuration'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:475:in ʻeach'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:475:in ʻeach_current_configuration'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:184:in `create_current'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/railties/databases.rake:39:in `block (2 levels) in<main>'
/usr/local/bundle/gems/railties-6.0.2.2/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/usr/local/bundle/gems/railties-6.0.2.2/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/usr/local/bundle/gems/railties-6.0.2.2/lib/rails/command.rb:48:in ʻinvoke'
/usr/local/bundle/gems/railties-6.0.2.2/lib/rails/commands.rb:18:in `<main>'
/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/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'
/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
/usr/local/bundle/gems/activesupport-6.0.2.2/lib/active_support/dependencies.rb:325:in `block in require'
/usr/local/bundle/gems/activesupport-6.0.2.2/lib/active_support/dependencies.rb:291:in `load_dependency'
/usr/local/bundle/gems/activesupport-6.0.2.2/lib/active_support/dependencies.rb:325:in `require'
bin/rails: 4: in `<main>'
Tasks: TOP =>db: create
(See full trace by running task with --trace)
Exited with code exit status 1
Corresponding source code
# circleci/config.yml
version: 2.1
orbs:
  ruby: circleci/ruby ​​@ 0.1.2

jobs: jobs:
  build: build:
    machine:
      image: circleci/classic: edge
    environment: environment:
      RAILS_ENV: test
      COMPOSE_FILE: docker-compose-dev.yml
    steps: steps:
      --checkout
      --run:
          name: pip upgrade
          command: pip install --upgrade pip
      --run:
          name: update d-c
          command: pip install docker-compose == 1.26.2
      --run:
          name: docker version
          command: docker-compose --version
      --run:
          name: d-c build
          command: docker-compose build
      --run:
          name:'d-c up'
          command: docker-compose up -d
      --run: # Stop at db: create here.
          name: db create
          command: docker-compose run --rm back rails db: create&&docker-compose run --rm back rails db: schema: load
      --run:
          name: Rspec
          command: docker-compose run --rm back bundle exec rspec
# docker-compose-dev.yml
version: "3"
services: services:
  front: front:
    build: build:
      context: front
    volumes:
      --./front:/usr/src/app
      --node_modules:/usr/src/app/node_modules
    ports: ports:
      -"3000: 3000"
  back:
    build: build:
      context: back
    environment: environment:
      --RAILS_ENV = ${RAILS_ENV}
      --EDITOR = vi
    volumes:
      --./back:/usr/src/app:delegated
    ports: ports:
      -"3001: 3001"
    depends_on:
      --db
      --redis
  db:
    image: mariadb: 10.5.1
    ports: ports:-"3306"
    environment: environment:
      MYSQL_ROOT_PASSWORD:'pass'
    volumes:
      --mariadb-data:/var/lib/mysql
  redis:
    image: redis
    ports: ports:
      -"6379: 6379"
    volumes:
      -"./ redis:/data"
    command: redis-server --appendonly yes
volumes:
  mariadb-data:
  node_modules:
# database.yml
default:&default
  adapter: mysql2
  encording: utf8
  port: 3306
  pool:<% = ENV.fetch ("RAILS_MAX_THREADS") {5}%>timeout: 5000
  url:<% = ENV.fetch ("DATABASE_URL") {"mysql2: // root: pass @ db: 3306"}%>host: localhost
development:
<<: * default
  database: db/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: test:
<<: * default
  database: db/test
production:
<<: * default
  database: db/production
  username:<% = ENV ["DB_USER"]%>password:<% = ENV ["DB_PASSWORD"]%>
What I tried

In database.yml, I decomposed the specification in url and made it as follows, but the same error.

adapter: mysql2
  encording: utf8
  port: 3306
  pool:<% = ENV.fetch ("RAILS_MAX_THREADS") {5}%>timeout: 5000
  username: root
  password: pass
  host:'db' # db, localhost,'localhost' all the same error


In the local environment
RAILS_ENV = development, test
I can connect to db without any problem in both cases.

Supplementary information (FW/tool version, etc.)

Development environment

ruby 2.6.3
Rails 6.0.2.2
docker-compose 1.26.2
mysql2 0.5.3


We apologize for the inconvenience, and thanks for your cooperation.

  • Answer # 1

    Self-solving. As shown below, after starting the server, I waited for 30 seconds and it worked fine.
    I also waited with dockerize, but that didn't work.

    --run:
              name: d-c build
              command: docker-compose build
          --run:
              name:'d-c up'
              command: docker-compose up -d
          --run: sleep 30 # Add this
          --run: # Stop at db: create here.
              name: db create
              command: docker-compose run --rm back rails db: create&&docker-compose run --rm back rails db: schema: load