php - Gitlab CI Symfony : SQLSTATE[HY000] [2002] Connection refused

标签 php symfony gitlab gitlab-ci

每次有人推送代码时,我都会使用 gitlab 运行单元测试。我在 composer 安装过程中收到此错误。

> Incenteev\ParameterHandler\ScriptHandler::buildParameters
Creating the "app/config/parameters.yml" file
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache


  [Doctrine\DBAL\Exception\ConnectionException]                              
  An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused  



  [Doctrine\DBAL\Driver\PDOException]        
  SQLSTATE[HY000] [2002] Connection refused  



  [PDOException]                             
  SQLSTATE[HY000] [2002] Connection refused  


Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception

这是我的配置:

.gitlab-ci.yml文件

    # Select image from https://hub.docker.com/_/php/
    image: php:5.6

    # Select what we should cache
    cache:
      paths:
      - vendor/

    before_script:
    # Install ssh-agent if not already installed, it is required by Docker.
    # (change apt-get to yum if you use a CentOS-based image)
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'

    #

 Run ssh-agent (inside the build environment)
- eval $(ssh-agent -s)

# Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
- ssh-add <(echo "$SSH_PRIVATE_KEY")

# For Docker builds disable host key checking. Be aware that by adding that
# you are suspectible to man-in-the-middle attacks.
# WARNING: Use this only with the Docker executor, if you use it with shell
# you will overwrite your user's SSH config.
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

- cp ci/custom.ini /usr/local/etc/php/conf.d/custom.ini
- bash ci/docker_install.sh > /dev/null

# Install composer
- curl -sS https://getcomposer.org/installer | php

services:
- mysql:latest

variables:
  # Configure mysql service (https://hub.docker.com/_/mysql/)
  MYSQL_DATABASE: symfony
  MYSQL_ROOT_PASSWORD: root

# We test PHP5.6 (the default) with MySQL
test:mysql:
  script:
  # Install all project dependencies
  - php composer.phar install
  - phpunit --coverage-text --colors=never -c app/

参数.yml.dist

parameters:
    database_host:     127.0.0.1
    database_port:     ~
    database_name:     symfony
    database_user:     root
    database_password: root

    mailer_transport:  smtp
    mailer_host:       127.0.0.1
    mailer_user:       ~
    mailer_password:   ~

    # A secret key that's used to generate certain security-related tokens
    secret:            ThisTokenIsNotSoSecretChangeIt

    database_slave1_host: 127.0.0.1
    database_slave1_port: ~
    database_slave1_name: symfony
    database_slave1_user: root
    database_slave1_password: root

我已阅读并遵循 gitlab 网站的说明。也许我的错误很明显,但我看不到。

最佳答案

当您使用在另一个容器中运行的 MySQL 时,您必须使用其主机名,而不是 127.0.0.1正确的数据库主机应该是“mysql”。这包含在 one of the sections 中GitLab 的文档:

The service container for MySQL will be accessible under the hostname mysql. So, in order to access your database service you have to connect to the host named mysql instead of a socket or localhost.

关于php - Gitlab CI Symfony : SQLSTATE[HY000] [2002] Connection refused,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39102481/

相关文章:

symfony - 通过 Symfony 组件使用 unoconv 将 DOC 转换为 PDF

gitlab - 当提供看似有效的 YAML 时,Gitlab 中的连接逻辑失败

caching - Gitlab CI - 如果缓存存在,如何跳过作业

php - 如何将 JS/CSS 文件包含到 Slim 框架的模板中?

php 全文搜索无法正常工作,有时会搜索单词,有时不会

symfony - 在 Symfony2 中,如何在 Controller 内重定向到带有 anchor 标记/哈希的 URL

iOS : gitlab-runner stuck on CI

php - 通过 HTTP URL 查询 Gmail API

php - laravel 5 中的 Ajax post 请求返回错误 500(内部服务器错误)

symfony - 从 Symfony 中的现有实体文件生成 orm.yml 映射文件