Can't connect to local MySQL server when using the CircleCI MySQL image

mysql

#1

I’m building a Ruby gem that uses a MySQL database in its tests. I’m also using one of the CircleCI MySQL images. This image should have all the environment variables setup to allow connecting to localhost as root without password. I should only need to override the database name.

You can view the code at https://github.com/crzrcn/pageturner/pull/5.

Ideas on how to debug this?

.circleci/config.yml:

# Ruby CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
#
version: 2
jobs:
  build:
    docker:
      # specify the version you desire here
       - image: circleci/ruby:2.4.1-node-browsers

      # 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:
           MYSQL_DATABASE: "pageturner_test"

    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: gem install bundler
      - run: bundle install --jobs=4 --retry=3 --path vendor/bundle

      - save_cache:
          paths:
            - ./vendor/bundle
          key: v1-dependencies-{{ checksum "Gemfile.lock" }}

      # 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

Error:

#!/bin/bash -eo pipefail
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}"
-- create_table(:plates, {:force=>true})
bundler: failed to load command: rspec (/home/circleci/repo/vendor/bundle/ruby/2.4.0/bin/rspec)
Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/mysql2-0.4.10/lib/mysql2/client.rb:89:in `connect'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/mysql2-0.4.10/lib/mysql2/client.rb:89:in `initialize'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:21:in `new'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:21:in `mysql2_connection'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:759:in `new_connection'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:803:in `checkout_new_connection'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:782:in `try_to_checkout_new_connection'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:743:in `acquire_connection'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:500:in `checkout'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `connection'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `retrieve_connection'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/connection_handling.rb:116:in `retrieve_connection'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/connection_handling.rb:88:in `connection'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/migration.rb:832:in `connection'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/migration.rb:839:in `block in method_missing'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/migration.rb:818:in `block in say_with_time'
  /usr/local/lib/ruby/2.4.0/benchmark.rb:293:in `measure'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/migration.rb:818:in `say_with_time'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/migration.rb:838:in `method_missing'
  /home/circleci/repo/spec/schema.rb:2:in `block in <top (required)>'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/schema.rb:48:in `instance_eval'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/schema.rb:48:in `define'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.0/lib/active_record/schema.rb:44:in `define'
  /home/circleci/repo/spec/schema.rb:1:in `<top (required)>'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `load'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `block in load'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:258:in `load_dependency'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `load'
  /home/circleci/repo/spec/spec_helper.rb:7:in `<top (required)>'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration.rb:1455:in `require'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration.rb:1455:in `block in requires='
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration.rb:1455:in `each'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration.rb:1455:in `requires='
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration_options.rb:112:in `block in process_options_into'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration_options.rb:111:in `each'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration_options.rb:111:in `process_options_into'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration_options.rb:21:in `configure'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:99:in `setup'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:86:in `run'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:71:in `run'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:45:in `invoke'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.7.0/exe/rspec:4:in `<top (required)>'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/bin/rspec:23:in `load'
  /home/circleci/repo/vendor/bundle/ruby/2.4.0/bin/rspec:23:in `<top (required)>'
Exited with code 1

#2

My solution was to explicitly set the host when ActiveRecord attempts to establish the database connection. I assumed that not setting the host would translate as trying to login localhost, but guess not.


#3

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.