Mysql2::Error: Can't connect to MySQL server on '127.0.0.1' (111)

I’m trying to upgrade circleCI from 1.0 to 2.0.

I changed the code referring to the document, but I got the unknown error,

#!/bin/bash -eo pipefail
RAILS_ENV=test bundle exec rake db:schema:load
...
rake aborted!
Mysql2::Error: Can't connect to MySQL server on '127.0.0.1' (111)
...

What I want to do is run Rspec test.
The code for ver. 2.0 is this.

version: 2
jobs:
  build:
    machine: true

    ruby:
      version: 2.1.4

    steps:
      - checkout

      # Restore bundle cache
      - restore_cache:
          key: rails-demo-{{ checksum "Gemfile.lock" }}

      # Bundle install dependencies
      - run: bundle install --path vendor/bundle

      # Store bundle cache
      - save_cache:
          key: rails-demo-{{ checksum "Gemfile.lock" }}
          paths:
            - vendor/bundle

      # Database setup
      - run: mv config/database.yml.sample config/database.yml
      - run: RAILS_ENV=test bundle exec rake db:create
      - run: RAILS_ENV=test bundle exec rake db:schema:load

      # Run rspec in parallel
      - type: shell
        command: |
          bundle exec rspec --profile 10 \
                            --format RspecJunitFormatter \
                            --out test_results/rspec.xml \
                            --format progress \
                            $(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)

      # Save test results for timing analysis
      - store_test_results:
          path: test_results

The code ver.1.0 is this, this worked well.

machine:
  timezone:
    Asia/Tokyo
  ruby:
    version: 2.1.4
jobs:
  database:
    pre:
      - mv config/database.yml.sample config/database.yml
    override:
      - RAILS_ENV=test bundle exec rake db:create db:schema:load
      - RAILS_ENV=test bundle exec rake db:migrate

  test:
    override:
      - bundle exec rspec

In the document, most of the topic of ‘mysql’ is for docker. My project doesn’t use docker.

What I want to know is, 1. Reason of this error, 2. Way to solve this error.

Thanks,

MySQL is not running by default. In order to use it you should add it to your config as shown here: https://circleci.com/docs/2.0/language-ruby/#sample-configuration

Replace PostgreSQL with the version of MySQL that you want to use.

Sorry, I see that you are using the Machine executor. In this case, I still think that MySQL is not being started by default. Could you add sudo service mysql start to the beginning of your build?