Gitlab CI Symfony:SQLSTATE [HY000] [2002]连接被拒绝 [英] Gitlab CI Symfony : SQLSTATE[HY000] [2002] Connection refused

查看:123
本文介绍了Gitlab CI Symfony:SQLSTATE [HY000] [2002]连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当有人推送代码时,我都会使用gitlab运行单元测试.在Composer安装过程中出现此错误.

I use gitlab to run unit tests each time someone push the code. I get this error during composer installation.

> 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文件

.gitlab-ci.yml file

    # 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/

parameters.yml.dist

parameters.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网站的说明.也许我的错误很明显,但我看不到.

I have read and follow the instruction of the gitlab website. Maybe my mistake is obvious, but I can't see it.

推荐答案

在使用在另一个容器中运行的MySQL时,必须使用其主机名,而不是 127.0.0.1 . 正确的数据库主机应为"mysql" .这在一个GitLab文档的部分内容:

As you are using MySQL that is running in another container, you have to use its hostname, not 127.0.0.1. The correct database host should be "mysql". This is covered in one of the sections of the GitLab's documentation:

可以通过主机名mysql访问MySQL的服务容器.因此,为了访问数据库服务,您必须连接到名为mysql的主机,而不是套接字或本地主机.

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.

这篇关于Gitlab CI Symfony:SQLSTATE [HY000] [2002]连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆