Unable to connect to Mysql server -- No connection could be made because the target machine actively refused it

Please see the following config.yml.
The job db-setup can be run successfully. When it runs build-and-test, it failed at “Run tests” because it cannot connect to the mysql database. The error is

MySql.Data.MySqlClient.MySqlException : Unable to connect to any of the specified MySQL hosts.
-------- System.Net.Sockets.SocketException : No connection could be made because the target machine actively refused it.

The test tried to connect to the mysql server using the ip address 127.0.0.1.

I am not sure if one container can connect to the MySQL server which is in another container.
I searched from the web but still cannot find the solution.
Can anyone help?

version: 2.1

orbs:
  win: circleci/windows@5.0.0

jobs:
  db-setup:
    working_directory: /home/circleci/code/projectxxx
    docker:
      - image: cimg/base:2022.12
      - image: cimg/mysql:8.0
        environment:
          MYSQL_ROOT_PASSWORD: rootpwd
          MYSQL_DATABASE: testdb
          MYSQL_USER: user
          MYSQL_PASSWORD: pwd
    steps:
      # Checkout the code as the first step.
      - checkout
      - run:
      # Our primary container isn't MYSQL so run a sleep command until it's ready.
          name: Waiting for MySQL to be ready
          command: |
            for i in `seq 1 10`;
            do
              nc -z 127.0.0.1 3306 && echo Success && exit 0
              echo -n .
              sleep 1
            done
            echo Failed waiting for MySQL && exit 1
      - run:
          name: Install MySQL CLI
          command: |
            sudo apt update
            sudo apt-get install mysql-client
      - run:
          name: "Run migrations"
          command: |
            cd db/migration
            for FILE in *; do mysql -h 127.0.0.1 -u user -ppwd testdb < $FILE; done;
            

  build-and-test:
    executor:
      name: win/default
    working_directory: C:\Users\circleci\code\projectxxx\app

    steps:
      # Checkout the code as the first step.
      - checkout:
          path: C:\Users\circleci\code\projectxxx
      - run:
          name: "Set environment variables"
          command: echo APP_ENV=Test > ./../.env
      - restore_cache:
          keys:
            - dotnet-packages-v1-{{ checksum "projectxxx.Core\\projectxxx.Core.csproj" }}
      - run:
          name: "Install project dependencies"
          command: dotnet.exe restore
      - save_cache:
          paths:
            - C:\Users\circleci\code\projectxxx\.nuget\packages
          key: dotnet-packages-v1-{{ checksum "projectxxx.Core\\projectxxx.Core.csproj" }}
      - run:
          name: "Run Build step"
          command: dotnet.exe publish -c Release -r win10-x64
      - store_artifacts:
          path: ./projectxxx.ConsoleApp/bin/Release/netcoreapp6/win10-x64/publish/projectxxx.ConsoleApp.exe
      - run:
          name: "Create config file"
          command: ./../circleCiScripts/createConfigFile.sh Test
      - run:
          name: "Show appsetting.json"
          command: |
            tail "./projectxxx.Tests/bin/debug/netcoreapp6/appsettings.test.json"
      - run:
          name: "Run tests"
          command: |
            dotnet test

workflows:
  projectxxx: 
    jobs:
      - db-setup
      - build-and-test:
          requires:
            - db-setup
1 Like

It seems like the issue lies in the connectivity between the two containers, specifically when the build-and-test job is attempting to connect to the MySQL server set up in the db-setup job. Containers within the same Docker network should be able to communicate with each other.

To resolve the issue, here are some steps you can take:

  1. Use the Same Docker Network:
    Ensure that both jobs are part of the same Docker network, which is the default behavior for containers within a CircleCI job.

    jobs:
      db-setup:
        docker:
          - image: cimg/base:2022.12
          - image: cimg/mysql:8.0
            network_mode: bridge  # Ensure both containers are on the same network
        steps:
          # ... rest of the steps
    
      build-and-test:
        executor:
          name: win/default
        working_directory: C:\Users\circleci\code\projectxxx\app
        docker:
          - image: cimg/base:2022.12
          - image: cimg/mysql:8.0
            network_mode: bridge  # Ensure both containers are on the same network
        steps:
          # ... rest of the steps
    
  2. Update MySQL Connection Host:
    Instead of using 127.0.0.1 as the MySQL host in the db-setup job, use the service name or container name.

    # In db-setup job
    - run:
        name: Waiting for MySQL to be ready
        command: |
          for i in `seq 1 10`;
          do
            nc -z mysql 3306 && echo Success && exit 0  # Use the service name 'mysql'
            echo -n .
            sleep 1
          done
          echo Failed waiting for MySQL && exit 1
    

    And in your migration and any other places where you connect to MySQL, replace 127.0.0.1 with mysql.

  3. Debugging:
    Add some debugging steps to your build-and-test job to check the network connectivity. You can use tools like telnet or nc to check if the MySQL port is reachable.

    - run:
        name: "Check MySQL port"
        command: nc -z mysql 3306
    

    If the port is not reachable, it might indicate a network or connectivity issue.

By ensuring both containers are on the same Docker network and using the service name or container name for MySQL host, you should be able to establish the connection between the two containers. Additionally, the debugging steps will help you identify any issues in the network connectivity.

Below are the links to the finest learning platforms

  1. MySQL SQL
  2. https://iqratechnology.com/academy/sql-training/
  3. Learn MySQL Tutorial - javatpoint