【问题标题】:Gitlab CI Pipeline SQLSTATE[HY000] [2002] No such file or directory (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')Gitlab CI Pipeline SQLSTATE [HY000] [2002]没有这样的文件或目录(SQL:SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
【发布时间】:2021-07-18 20:04:08
【问题描述】:

所有迁移均已成功运行。但是在执行 phpunit 时出现错误“SQLSTATE[HY000] [2002] No such file or directory (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')”。

我什至尝试用 DB_HOST=127.0.0.1 和 localhost 替换 DB_HOST=mysql。迁移未成功运行并遇到错误“SQLSTATE[HY000] [2002] Connection denied (SQL: select * from information_schema.tables where table_schema = my_db and table_name = migrations and table_type = 'BASE TABLE')”

我正在解决这个问题 2 天。任何帮助将不胜感激。

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

【问题讨论】:

    标签: php mysql laravel phpunit gitlab-ci


    【解决方案1】:

    在您的 .env 文件中使用这些更改:

    DB_CONNECTION=mysql
    DB_HOST=localhost
    DB_PORT=3306
    

    然后在Database.php添加文件夹位置:

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

    最后,试试:

    php artisan config:clear
    php artisan migrate:install
    

    希望这能解决您的问题。

    【讨论】:

    • 我试过这个。正如我所提到的,如果我更改 DB_HOST=localhost 或 127.0.0.1,我会收到连接被拒绝,如果 DB_HOST=mysql,则正在创建迁移,但是在执行 PHPUnit 时,出现错误为“SQLSTATE [HY000] [2002] 没有这样的文件或目录(SQL:SHOW FULL TABLES WHERE table_type = 'BASE TABLE')”。而且我使用的是 Linux(不是用于 unix_socket 的 mac)。我尝试使用 unix_socket=/var/run/mysqld/mysqld.sock 但在没有创建迁移的情况下拒绝连接。
    • 我已经编辑了答案,你现在可以看到了。
    【解决方案2】:

    最后,这个东西解决了这个问题(在 phpunit 阶段的脚本中添加了cp .env .env.testing)。 还更新了这些

    1. 'unix_socket' => env('DB_SOCKET', '/var/run/mysqld/mysqld.sock') 在 数据库.php。
    2. DB_PORT:变量中的 3306

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-07
      相关资源
      最近更新 更多