Gitlab CI Symfony:SQLSTATE[HY000] [2002] 连接被拒绝
Gitlab CI Symfony : SQLSTATE[HY000] [2002] Connection refused
每次有人推送代码时,我都会使用 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/
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 中对此进行了介绍:
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.
此错误的一个可能问题是您在数据库仍在初始化时尝试访问它。 Docker HUB 的 MySQL 的 Caveats 部分对此进行了介绍。
If there is no database initialized when the container starts, then a default database will be created. While this is the expected behavior, this means that it will not accept incoming connections until such initialization completes. This may cause issues when using automation tools...
一个简单的解决方案是在启动任何访问数据库的进程之前使用sleep
命令。您可以将其添加到 before_script
部分:
before_script:
- sleep 60s
更好、更复杂的解决方案是反复探测 MySQL 服务器,检查它是否已经接受连接。
每次有人推送代码时,我都会使用 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/
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 中对此进行了介绍:
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.
此错误的一个可能问题是您在数据库仍在初始化时尝试访问它。 Docker HUB 的 MySQL 的 Caveats 部分对此进行了介绍。
If there is no database initialized when the container starts, then a default database will be created. While this is the expected behavior, this means that it will not accept incoming connections until such initialization completes. This may cause issues when using automation tools...
一个简单的解决方案是在启动任何访问数据库的进程之前使用sleep
命令。您可以将其添加到 before_script
部分:
before_script:
- sleep 60s
更好、更复杂的解决方案是反复探测 MySQL 服务器,检查它是否已经接受连接。