Gitlab Ci Laravel PDOException:SQLSTATE [HY000] [2002]没有这样的文件或目录

时间:2018-11-04 12:59:25

标签: php laravel continuous-integration gitlab

我的laravel测试在我的机器上运行得很好,但是当通过docker在gitlab-runner上运行时,我在所有测试中都看到了这一点:

...
Caused by
PDOException: SQLSTATE[HY000] [2002] No such file or directory

/builds/project-0/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
/builds/project-0/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:46
/builds/project-0/vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php:24
/builds/project-0/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php:182
/builds/project-0/vendor/laravel/framework/src/Illuminate/Database/Connection.php:916
/builds/project-0/vendor/laravel/framework/src/Illuminate/Database/Connection.php:941
/builds/project-0/vendor/laravel/framework/src/Illuminate/Database/Connection.php:399
/builds/project-0/vendor/laravel/framework/src/Illuminate/Database/Connection.php:325
/builds/project-0/vendor/laravel/framework/src/Illuminate/Database/Connection.php:657
/builds/project-0/vendor/laravel/framework/src/Illuminate/Database/Connection.php:624
/builds/project-0/vendor/laravel/framework/src/Illuminate/Database/Connection.php:333
/builds/project-0/vendor/laravel/framework/src/Illuminate/Database/Schema/MySqlBuilder.php:99
/builds/project-0/vendor/laravel/framework/src/Illuminate/Database/Schema/MySqlBuilder.php:48
/builds/project-0/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/FreshCommand.php:73
/builds/project-0/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/FreshCommand.php:46
/builds/project-0/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29
/builds/project-0/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:87
/builds/project-0/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:31
/builds/project-0/vendor/laravel/framework/src/Illuminate/Container/Container.php:572
/builds/project-0/vendor/laravel/framework/src/Illuminate/Console/Command.php:183
/builds/project-0/vendor/symfony/console/Command/Command.php:255
/builds/project-0/vendor/laravel/framework/src/Illuminate/Console/Command.php:170
/builds/project-0/vendor/symfony/console/Application.php:886
/builds/project-0/vendor/symfony/console/Application.php:262
/builds/project-0/vendor/symfony/console/Application.php:145
/builds/project-0/vendor/laravel/framework/src/Illuminate/Console/Application.php:89
/builds/project-0/vendor/laravel/framework/src/Illuminate/Console/Application.php:188
/builds/project-0/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:250
/builds/project-0/vendor/laravel/framework/src/Illuminate/Foundation/Testing/PendingCommand.php:136
/builds/project-0/vendor/laravel/framework/src/Illuminate/Foundation/Testing/PendingCommand.php:218
/builds/project-0/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php:55
/builds/project-0/tests/RefreshAndSeedDatabase.php:61
/builds/project-0/tests/RefreshAndSeedDatabase.php:20
/builds/project-0/tests/TestCase.php:21
/builds/project-0/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:71
...

这是我的.env.testing

...
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=secret
...

和我的.gitlab-ci.yml

image: composer

stages:
- setup
- test
- deploy

phpunit:
  stage: test
  image: epcallan/php7-testing-phpunit:7.2-phpunit6

  services:
  - mysql:5.7

  variables:
    MYSQL_DATABASE: homestead
    MYSQL_ROOT_PASSWORD: secret

  before_script:
  - apt-get install -y libpng-dev libfreetype6-dev libjpeg62-turbo-dev
  - docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
  - docker-php-ext-install gd

  script:
  - cp .env.testing .env
  - composer install
  - php artisan key:generate
  - php artisan passport:key
  - php vendor/bin/phpunit --coverage-text --colors=never --coverage-html=public/tests/coverage

我曾尝试将DB_HOST更改为127.0.0.1,就像建议的here一样,但这没有帮助。

在我的其他laravel项目中使用的解决方案是将DB_HOST设置为mysql,通常可以,但是这次不行。我不知道为什么。

1 个答案:

答案 0 :(得分:0)

通过在DB_HOST=mysql中设置.gitlab-ci.yml可以解决此问题:

变量:

    MYSQL_DATABASE: homestead

    MYSQL_ROOT_PASSWORD: secret

    DB_HOST: mysql

这将覆盖.env.testing中设置的变量,以允许运行测试而无需在主机上进行任何进一步配置。