0
votes

All migrations ran successfully. But when executing phpunit I am getting error "SQLSTATE[HY000] [2002] No such file or directory (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')".

I even tried replacing DB_HOST=mysql with DB_HOST=127.0.0.1 and localhost. Migrations didn't run successfully and faced an errro "SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = my_db and table_name = migrations and table_type = 'BASE TABLE')"

I am working around with this for 2 days. Any help would be highly appreciated.

gitlab-ci.yml

stages:
  - build
  - test

variables:
  MYSQL_DATABASE: $MYSQL_DATABASE
  MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD
  MYSQL_PASSWORD: $MYSQL_ROOT_PASSWORD
  DB_HOST: mysql
  DB_DATABASE: $MYSQL_DATABASE
  DB_USERNAME: $MYSQL_USER
  DB_PASSWORD: $MYSQL_ROOT_PASSWORD

cache:
  paths:
    - vendor/
    - .env

image: lorisleiva/laravel-docker:latest

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

phpunit:
  stage: test
  services:
    - mysql:5.7
  dependencies:
    - composer
  script:
    - composer dump-autoload
    - php artisan config:clear
    - php artisan migrate
    - php ./vendor/bin/phpunit --testsuite=Feature
  artifacts:
    paths:
      - ./storage/logs # for debugging
    expire_in: 1 days
    when: on_failure
2

2 Answers

0
votes

Use these changes in your .env files:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306

Then in Database.php add folder location:

'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'),

Finally, try:

php artisan config:clear
php artisan migrate:install

Hope this solves your problem.

0
votes

Finally, this thing solved the issue (added cp .env .env.testing to the scripts in phpunit stage). Also updated these

  1. 'unix_socket' => env('DB_SOCKET', '/var/run/mysqld/mysqld.sock') in database.php.
  2. DB_PORT: 3306 in variables

gitlab-ci.yml

stages:
  - build
  - codequality
  - test

variables:
  MYSQL_DATABASE: $MYSQL_DATABASE
  MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD
  MYSQL_PASSWORD: $MYSQL_ROOT_PASSWORD
  DB_HOST: mysql
  DB_DATABASE: $MYSQL_DATABASE
  DB_USERNAME: $MYSQL_USER
  DB_PASSWORD: $MYSQL_ROOT_PASSWORD
  DB_PORT: 3306

cache:
  paths:
    - vendor/
    - .env

image: lorisleiva/laravel-docker:latest

composer:
  stage: build
  script:
    - composer update --prefer-dist --no-ansi --no-interaction --no-progress --no-scripts
    - cp .env.example .env
    - php artisan key:generate
  artifacts:
    paths:
      - vendor/
      - .env
    expire_in: 2 days
    when: always
  cache:
    key: ${CI_COMMIT_REF_SLUG}-composer
    paths:
      - vendor/
psalm:
  stage: codequality
  dependencies:
    - composer
  script:
    - ./vendor/bin/psalm
  artifacts:
    paths:
      - ./storage/logs
    expire_in: 1 days
    when: on_failure

codestyle:
  stage: codequality
  dependencies: []
  script:
    - phpcs --standard=PSR2 --extensions=php app

phpunit:
  stage: test
  services:
    - mysql:5.7
  dependencies:
    - composer
  script:
    - cp .env .env.testing
    - php artisan config:clear
    - php artisan migrate
    - php ./vendor/bin/phpunit --testsuite=Feature
  artifacts:
    paths:
      - ./storage/logs # for debugging
    expire_in: 1 days
    when: on_failure