barden barden -4 years ago 447
PHP Question

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

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

An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused

SQLSTATE[HY000] [2002] Connection refused

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

# Select image from
image: php:5.6

# Select what we should cache
- vendor/

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

# Install composer
- curl -sS | php

- mysql:latest

# Configure mysql service (

# We test PHP5.6 (the default) with MySQL
# Install all project dependencies
- php composer.phar install
- phpunit --coverage-text --colors=never -c app/


database_port: ~
database_name: symfony
database_user: root
database_password: root

mailer_transport: smtp
mailer_user: ~
mailer_password: ~

# A secret key that's used to generate certain security-related tokens
secret: ThisTokenIsNotSoSecretChangeIt

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. Maybe my mistake is obvious, but I can't see it.

Answer Source

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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download