简体   繁体   English

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

[英]Gitlab CI Symfony : SQLSTATE[HY000] [2002] Connection refused

I use gitlab to run unit tests each time someone push the code.每次有人推送代码时,我都会使用 gitlab 运行单元测试。 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

Here is my configuration :这是我的配置:

.gitlab-ci.yml file .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/

parameters.yml.dist参数.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

I have read and follow the instruction of the gitlab website.我已阅读并遵循 gitlab 网站的说明。 Maybe my mistake is obvious, but I can't see it.也许我的错误很明显,但我看不到。

As you are using MySQL that is running in another container, you have to use its hostname, not 127.0.0.1 .当您使用在另一个容器中运行的 MySQL 时,您必须使用它的主机名,而不是127.0.0.1 The correct database host should be "mysql" .正确的数据库主机应该是 "mysql" This is covered in one of the sections of the GitLab's documentation: GitLab 文档的其中一个部分对此进行了介绍:

The service container for MySQL will be accessible under the hostname mysql.可以在主机名 mysql 下访问 MySQL 的服务容器。 So, in order to access your database service you have to connect to the host named mysql instead of a socket or localhost.因此,为了访问您的数据库服务,您必须连接到名为 mysql 的主机,而不是套接字或 localhost。

One of the possible reasons for this error is that you attempt to access the database while it still initialises.此错误的可能原因之一是您尝试在数据库仍在初始化时访问它。 This is covered in the MySQL's caveats section on the Docker HUB.这在 Docker HUB 上的 MySQL警告部分中有介绍。

If there is no database initialised when the container starts, then a default database will be created.如果容器启动时没有初始化数据库,则将创建一个默认数据库。 While this is the expected behaviour, this means that it will not accept incoming connections until such initialisation completes.虽然这是预期的行为,但这意味着在初始化完成之前它不会接受传入的连接。 This may cause issues when using automation tools...这可能会在使用自动化工具时导致问题...

A crude solution would be to use sleep command before you start any process that accesses database.一个粗略的解决方案是在启动任何访问数据库的进程之前使用sleep命令。 You can add it to the before_script section:您可以将其添加到before_script部分:

before_script:
  - sleep 60s

A better and more complex solution would be to probe the MySQL server, repeatedly checking whether it already accepts connections.更好和更复杂的解决方案是探测 MySQL 服务器,反复检查它是否已经接受连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM