Connection refused MySQL

Occasionally we’ll have a build fail with the following error:

#!/bin/bash -eo pipefail
php artisan migrate --database=mysql_test

In Connection.php line 664:
                                                                               
  SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_s  
  chema.tables where table_schema = homestead_test and table_name = migration  
  s)                                                                           
                                                                               

In PDOConnection.php line 50:
                                             
  SQLSTATE[HY000] [2002] Connection refused  
                                             

In PDOConnection.php line 46:
                                             
  SQLSTATE[HY000] [2002] Connection refused  
                                             

Exited with code 1

This is only an occasional issue and does not happen on every build, and I’m not able to track down any specific thing that triggers it as when we tell CircleCI to rerun the build it works fine without any changes whatsoever.

Here is our config.yml:

version: 2

jobs:
  build:
    docker:
      - image: circleci/php:7.1-node-browsers
      - image: circleci/mysql:5.7.21
        environment:
          MYSQL_DATABASE: homestead_test
          MYSQL_USER: homestead_test
          MYSQL_PASSWORD: secret
          MYSQL_ROOT_PASSWORD: root

    working_directory: ~/laravel
    steps:
      - run:
          name: Install PHP extensions
          command: sudo docker-php-ext-install pdo_mysql

      - checkout

      - run: chmod -R 777 storage
      - run: cp .env.testing.example .env

      # Download and cache dependencies
      # Composer
      - restore_cache:
          name: Restore composer cache
          keys:
            - composer-{{ checksum "composer.json" }}
            - composer-
      - run: composer install -n --prefer-dist
      - save_cache:
          name: Store composer cache
          key: composer-{{ checksum "composer.json" }}
          paths:
            - vendor

      - run: php artisan migrate --database=mysql_test
      - run: vendor/bin/phpunit
  deploy:
    working_directory: ~/laravel
    docker:
      - image: circleci/php:7.1-node-browsers
    steps:
      - checkout
      - run:
          name: Installing deployment dependencies
          working_directory: /
          command: |
            sudo apt-get -y -qq update
            sudo apt-get install python-pip python-dev build-essential
            sudo pip install awsebcli --upgrade
      - run:
          name: Initializing AWS config
          command: sh ./.circleci/setup-eb.sh
      - run:
          name: Deploying
          command: eb deploy platform-backend-$CIRCLE_BRANCH --timeout 60
workflows:
  version: 2
  build_deploy:
    jobs:
      - build
      - deploy:
          requires:
            - build
          filters:
            branches:
              only:
                - develop
                - staging

If anyone has any ideas please do let me know :slight_smile:

Yep, it’s probably a race condition - the race is between your build container getting to your tests, and MySQL starting up in the secondary container. See here:

https://discuss.circleci.com/t/prevent-race-conditions-by-waiting-for-services-with-dockerize/11215

1 Like

Yep, that was it! Thanks

1 Like

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