我创建了与数据库相关的测试。我为Laravel设置了gitlab-ci.yml。当文件在GitLab中运行时,连接数据库出现错误。 (在测试阶段运行迁移时会出现错误)
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", [])
阅读MySql docker documentation以了解我需要使用的变量。
这是我的gitlab-ci.yml文件
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 # <-- the error is triggered at that time
- phpunit --coverage-text --colors=never
...
我看不出我的错误可能来自哪里。