Problem waiting for MySQL Database

I am trying to setup a test for an API which includes a MySQL Database.

version: 2.1

jobs:
  linux:
    docker:
      - image: circleci/buildpack-deps:stretch
      - image: swift:4.1
      - image: circleci/mysql:5.7
        environment:
          MYSQL_HOST: 127.0.0.1
          MYSQL_ROOT_PASSWORD: XXX
          MYSQL_USER: XXX
          MYSQL_PASSWORD: XXX
        entrypoint: |
          sh -c "
            echo 'CREATE DATABASE IF NOT EXISTS xxx; CREATE DATABASE IF NOT EXISTS xxx;' > /docker-entrypoint-initdb.d/init.sql;
            /usr/local/bin/docker-entrypoint.sh
          "
        command: |
          --character-set-server=utf8mb4
          --collation-server=utf8mb4_unicode_ci
          --skip-character-set-client-handshake
          --general_log
          --general_log_file=/var/log/mysql/queries.log
    environment:
      IMAGE_NAME: XXX/XXX
    steps:
      - checkout
      - run:
        name: Wait for db
        command: dockerize -wait tcp://127.0.0.1:3306 -timeout 240s
      - run: 
        name: Compile code
        working_directory: ./api
        command: swift build
      - run: 
        name: Run unit tests
        working_directory: ./api
        command: swift test
      - run: 
        name: Compile code with optimizations
        working_directory: ./api
        command: swift build -c release
      - setup_remote_docker
      - run:
        name: Build Docker Image
        command: docker build -t $IMAGE_NAME:latest .
      - run:
        name: Push Docker Image
        command: |
          echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
          docker tag $IMAGE_NAME:latest $IMAGE_NAME:$CIRCLE_SHA1
          docker push $IMAGE_NAME:latest
          docker push $IMAGE_NAME:$CIRCLE_SHA1

workflows:
  version: 2
  build-master:
    jobs:
      - linux:
        filters:
          branches:
            only: master

Every time I run it, it fails waiting for the database with dockerize.

2019/05/29 13:34:42 Waiting for: tcp://127.0.0.1:3306
2019/05/29 13:34:42 Problem with dial: dial tcp 127.0.0.1:3306: connect: connection refused. Sleeping 1s
2019/05/29 13:34:43 Problem with dial: dial tcp 127.0.0.1:3306: connect: connection refused. Sleeping 1s

2019/05/29 13:38:42 Timeout after 4m0s waiting on dependencies to become available: [tcp://127.0.0.1:3306]
Exited with code 1

I have no clue why it is constantly failing.
Is there something I am missing?

I would guess your entrypoint is overwriting the default one for MySQL and it is not starting up. I would suggest removing this line and running the SQL from the mysql client on the build host instead.

I’d also suggest making your dockerize timeout no more than 10s - if it has not started in that timeframe, it probably won’t start at all.

2 Likes

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