首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gitlab : SQLSTATE[HY000] [2002]拒绝连接

问Gitlab : SQLSTATE[HY000] [2002]拒绝连接
EN

Stack Overflow用户
提问于 2016-08-23 13:23:29
回答 2查看 3.5K关注 0票数 6

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

代码语言:javascript
复制
> 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文件

代码语言:javascript
复制
    # 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

代码语言:javascript
复制
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网站的指示。也许我的错误是显而易见的,但我看不见。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-23 14:12:01

在使用运行在另一个容器中的MySQL时,必须使用它的主机名,而不是127.0.0.1。正确的数据库主机应该是"mysql"。GitLab的文档中的其中一节介绍了这一点:

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

票数 12
EN

Stack Overflow用户

发布于 2016-08-23 14:13:18

造成此错误的可能原因之一是您试图在数据库初始化时访问它。这将在MySQL关于Docker的注意事项部分中进行讨论。

如果容器启动时没有初始化数据库,则将创建默认数据库。虽然这是预期的行为,这意味着它不会接受传入的连接,直到这种初始化完成。这可能会在使用自动化工具时引起问题..。

粗解决方案是在启动任何访问数据库的进程之前使用sleep命令。您可以将其添加到before_script部分:

代码语言:javascript
复制
before_script:
  - sleep 60s

一个更好和更复杂的解决方案是探测MySQL服务器,反复检查它是否已经接受连接。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39102481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档