CircleCI unable to connect to database

I am unable to do a simple migration in CircleCI. It keep saying it cannot connect to database

#!/bin/bash -eo pipefail
bin/cake migrations migrate
using migration paths 
 - /var/www/html/config/Migrations
using seed paths 
 - /var/www/html/config/Seeds
Exception: There was a problem connecting to the database: SQLSTATE[HY000] [2002] Connection refused
In [/var/www/html/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php, line 83]

2020-10-27 16:14:26 Error: [InvalidArgumentException] There was a problem connecting to the database: SQLSTATE[HY000] [2002] Connection refused in /var/www/html/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php on line 83
Stack Trace:

What could be the problem? the following is my config.yml

version: 2
jobs:
  build:
    docker:
      - image: mosesliao/php:timesheet
        environment:
          DEBUG: true
          DATABASE_URL: "mysql://root:password@127.0.0.1/timesheet?encoding=utf8mb4&timezone=UTC&cacheMetadata=true"
          DATABASE_TEST_URL: "mysql://root:password@127.0.0.1/timesheet?encoding=utf8mb4&timezone=UTC&cacheMetadata=true"
          SECURITY_SALT: aAEfxtXyx66ATQ09dSAs2dRXd5njiORl
      - image: mysql:5.6.36
        command: mysqld  --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci
        environment:
          MYSQL_DATABASE: timesheet
          MYSQL_USER: root
          MYSQL_ROOT_PASSWORD: password
    working_directory: /var/www/html
    steps:
      - checkout

      - run:
          name: composer setup
          command: |
            composer self-update
            composer install --no-interaction --ignore-platform-reqs

      - run:
          name: Initialize Database
          command: bin/cake migrations migrate

      - run:
          name: phpunit testing
          command: vendor/bin/phpunit --coverage-clover=coverage.xml

      - run:
          name: codecov upload
          command: bash <(curl -s https://codecov.io/bash)

workflows:
  version: 2
  build-and-deploy:
    jobs:
      - build

Hello @liaogz82 - Welcome back to our CircleCI community!

Looking at the DATABASE_URL: I see there is not a port number specified. Also, taking a quick look at your image - image: mosesliao/php:timesheet, it does not appear that netcat is present in the image which will not allow for nc to work.

Installing netcat above the migrate command will allow for nc to function.

while ! nc -z 127.0.0.1 3306 ; do sleep 1 ; done

 - run:
          name: composer setup
          command: |
            composer self-update
            composer install --no-interaction --ignore-platform-reqs
      - run:
          name: Wait for Database Port
          command: while ! nc -z 127.0.0.1 3306 ; do sleep 1 ; done
      - run:
          name: Initialize Database
          command: bin/cake migrations migrate

Give this a try at your earliest convenience and let us know if you have any further questions!

Ok it works! thanks!

@liaogz82 I’m glad to hear that and thank you for confirming!

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