lundi 11 novembre 2019

Connection refused for mysql in gitlab-ci for Laravel 6

I created tests with relation the database. I setting up the gitlab-ci.yml for Laravel. When the file run in GitLab , I have the error with the connection database. (the error appears when run the migration in the test stage)

   Illuminate\Database\QueryException  : SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = gitlabDb and table_name = migrations and table_type = 'BASE TABLE')  at /builds/... /.../vendor/laravel/framework/src/Illuminate/Database/Connection.php:665
    661|         // If an exception occurs when attempting to run a query, we'll format the error
    662|         // message to include the bindings with SQL, which will make this exception a
    663|         // lot more helpful to the developer instead of just the database's errors.
    664|         catch (Exception $e) {
  > 665|             throw new QueryException(
    666|                 $query, $this->prepareBindings($bindings), $e
    667|             );
    668|         }
    669|

  Exception trace:

  1   PDOException::("SQLSTATE[HY000] [2002] Connection refused")
      /builds/.../.../vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  2   PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=gitlabDb", "user", "password", [])
      /builds/.../.../vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  3   Illuminate\Database\Connectors\Connector::createPdoConnection("mysql:host=127.0.0.1;port=3306;dbname=gitlabDb", "user", "password", [])

read the MySql docker documentation to know the variables I need to use.

here my gitlab-ci.yml file

image: lorisleiva/laravel-docker:latest

services:
  - mysql:5.7

variables:
  MYSQL_ROOT_PASSWORD: password
  MYSQL_DATABASE: gitlabDb
  MYSQL_ROOT_HOST: mysql
  MYSQL_USER: user
  MYSQL_PASSWORD: password

  DB_DATABASE: gitlabDb
  DB_HOST: mysql
  DB_USERNAME: user
  DB_PASSWORD: password

composer:
  stage: build
  script:
    - composer install --prefer-dist --no-ansi --no-interaction --no-progress --no-scripts
    - cp .env.example .env
    - php artisan key:generate
  artifacts:
    expire_in: 30 days
    paths:
      - vendor/
      - .env
  cache:
    key: ${CI_COMMIT_REF_SLUG}-composer
    paths:
      - vendor/

phpunit:
  stage: test
  dependencies:
    - composer
  script:
    - php artisan migrate -v
    - phpunit --coverage-text --colors=never
...

I don't see where my mistake can come from.

Aucun commentaire:

Enregistrer un commentaire