skip to Main Content

I’m trying to deploy a MySQL docker image, create a database, and test-run my migrations and seeders although its complianing it just can’t access the MySQL server running on 127.0.0.1:3306, is there something I’m missing?

I get:

#!/bin/bash -eo pipefail
mysql -h 127.0.0.1 -u root -psecret dbname < output/migrate.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)

Exited with code exit status 1
CircleCI received exit code 1

Here is my circleci config:

version: 2.1

jobs:
  migrate-seed:
    docker:
      - image: cimg/base:2022.10
      - image: cimg/mysql:8.0
        environment:
          MYSQL_ROOT_PASSWORD: secret
          MYSQL_DATABASE: dbname
    steps:
      - checkout
      - run: sudo chmod +x scripts/dump-all
      - run: mkdir output
      - run: scripts/dump-all
      - run: sudo apt-get update
      - run: sudo apt-get install -y mysql-client
      - run:
          name: Run Migrations
          command: mysql -h 127.0.0.1 -u root -psecret dbname < output/migrate.sql
      - run:
          name: Run Seeders
          command: mysql -h 127.0.0.1 -u root -psecret dbname < output/seed.sql
workflows:
  database:
    jobs:
      - migrate-seed

2

Answers


  1. Try adding a step that ensures the DB is ready before attempting to connect to it:

      - run:
          name: Waiting for MySQL to be ready
          command: dockerize -wait tcp://localhost:3306 -timeout 1m
    

    Place it anywhere before your "Run Migrations" step.

    Login or Signup to reply.
  2. It is probably not related to your problem but in some cases you need to reinstall docker in you container to make it work:

        steps:
          - run:
              name: Reinstall Docker to fix an issue with port sharing
              command: apt update && apt install --reinstall docker.io --yes
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search