Why can't I connect to MySQL server?

circle.yml

#1

Trying to run a rails mysql app, but keep getting the following error:
Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

If I ssh into the box I cannot even access the mysql service:

sudo service mysql status
mysql: unrecognized service

Here is my config.yml, I am clearly including the mysql image, and all env vars I found when reading similar forum posts.

version: 2
jobs:
  build:
    docker:
      - image: circleci/ruby:2.3-node-browsers
        environment:
          BUNDLE_PATH: vendor/bundle
          RAILS_ENV: test

      - image: circleci/mysql:5.7
        environment:
          MYSQL_ALLOW_EMPTY_PASSWORD: yes
          MYSQL_ROOT_PASSWORD: ''
          MYSQL_DATABASE: AmazonMWS_test
          MYSQL_ROOT_HOST: 127.0.0.1
          MYSQL_USER: root

    working_directory: ~/AmazonMWS

    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: Bundle Install
          command: bundle check || bundle install

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

      # Database setup
      - run:
          name: Wait for DB
          command: dockerize -wait tcp://127.0.0.1:3306 -timeout 120s

      - run:
          name: Database setup
          command: bin/rails db:schema:load --trace

      # run tests!
      - run:
          name: Run rspec in parallel
          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)

      # collect reports
      - store_test_results:
          path: /tmp/test-results
      - store_artifacts:
          path: /tmp/test-results
          destination: test-results

#2

Yep, the service is not running on the container you are running this command on, so that console command won’t work. :smile_cat:

What happens is your secondary/database container is spun up, and runs entirely separately from your build container. The only way you can talk to it is via TCP. CircleCI does some networking magic so the containers network stacks are merged, which is why the service is accessible via localhost.

However, the error is here:

Mysql2::Error::ConnectionError: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

You’re trying to access the server via a Unix socket, which only works locally. You need to configure your driver to work over TCP.


How can I create multiple MySQL databases in the same Docker image?
#3

@halfer, thanks for your response!

Could you explain a bit more about how to configure my driver? Reading your comments led me to believe I could utilize that CircleCI magic mentioned by switching my host variable to localhost. I switched both instances of 127.0.0.1 to access via localhost instead.

I am not quite understanding something as this is still yielding the same result.


#4

Sadly, no; it will be a Ruby setting, and I don’t use/know Ruby. I know in PHP that this would be set in php.ini, so you’ll have to figure out what/where the config system is. I suggest a search for “Ruby MySQL use tcp connection” - there should be something on Stack Overflow.

I don’t think that is enough. PHP does this as well - specifying localhost as the host does not force TCP, and in fact can enable a socket connection instead. I think that is what is happening here.


#5

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