Gitlab CI Symfony:SQLSTATE [HY000] [2002]连接被拒绝
我使用gitlab在每次有人推送代码时运行单元测试。在作曲家安装过程中出现此错误。Gitlab CI Symfony:SQLSTATE [HY000] [2002]连接被拒绝
> 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/
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网站。也许我的错误是显而易见的,但我看不到它。
由于您使用的是在另一个容器中运行的MySQL,因此您必须使用其主机名,而不是127.0.0.1。 正确的数据库主机应该是“mysql”。这包括在GitLab文档的one of the sections中:
MySQL的服务容器可以在主机名mysql下访问。因此,为了访问数据库服务,您必须连接到名为mysql的主机,而不是套接字或本地主机。
正如我预测的那样,错误很明显!谢谢 ! – barden
此错误的一个可能的问题是您尝试访问数据库,同时它仍然初始化。这在Docker集线器的MySQL的Caveats部分中有介绍。
如果在容器启动时没有初始化数据库,则会创建一个默认数据库。虽然这是预期的行为,但这意味着在这种初始化完成之前它不会接受传入连接。使用自动化工具时,这可能会导致问题......
的古朴解决方案是使用sleep
命令你开始访问数据库中的任何过程之前。你可以把它添加到before_script
部分:
before_script:
- sleep 60s
一个更好的和更复杂的解决方案是将探测MySQL服务器反复检查其是否已经接受连接。
是否安装了MySQL服务器? – tkausl
MySQL应该作为服务安装(服务: - mysql:latest) 我配置了密码(MYSQL_ROOT_PASSWORD:root),默认用户是root。 – barden
嗨@barden,我面临同样的问题,但我认为我的conf不好,你能分享你的docker_install.sh吗? TY – Aximem