[SOLVED] Gitlab-CI : ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock'

Issue

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/run/mysqld/mysqld.sock’ (2)

Hi guys !
I have a little (big) issue with my gitlab-ci.yml 🙁

I’m trying to run my tests PHPUnit on the pipeline but when I want to create a mysql database and to connect it, it fails.

This is my .gitlab-ci.yml :

image: jakzal/phpqa:php8.0

before_script:
        - composer install
                
stages: 
    - SecurityChecker
    - UnitTests

cache:
  paths:
    - vendor/
    - ~/.composer/cache/files    

#On teste qu'il n'y a pas de faille de sécurité dans les différentes librairies
security-checker:
    stage: SecurityChecker
    script:
        - local-php-security-checker composer.lock
    allow_failure: false

#On lance les tests unitaires PHP UNIT    
phpunit:
    stage: UnitTests
    services:
        - name: mysql:latest
    before_script:
        - apt-get update && apt-get dist-upgrade && apt-get install -y git libzip-dev autoconf build-essential pkg-config libmariadb-dev-compat libmariadb-dev default-mysql-client mariadb-client mariadb-server
        - curl -sSk https://getcomposer.org/installer | php -- --disable-tls && mv composer.phar /usr/local/bin/composer
        - docker-php-ext-install mysqli pdo pdo_mysql zip
        # Connect MySQL
        - echo "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('password');" | mysql -u root
        - echo "SELECT 'OK';" | mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -h mysql "$MYSQL_DATABASE"
        # Import BDD tests
        - mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -h localhost < "database.sql"
    script:
        - vendor/bin/phpunit --testdox
    allow_failure: false

This is the result :

Pipeline error

Do you have any ideas fo my issue ?
I didn’t found anything on the different forums for now… :sleepy:

Thanks a lot !

Solution

The answer to my issue is this code !
It works for me 🙂

#On lance les tests unitaires PHP UNIT    
phpunit:
    stage: UnitTests
    services:
        - name: mysql:8.0.11
    variables: 
        MYSQL_DATABASE: 'database-name' 
        MYSQL_ROOT_PASSWORD: ''
        MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        DB_USERNAME: 'runner'
        DB_PASSWORD: 'password'
        DB_HOST: 'mysql'  
    before_script:
        # Install packages and docker
        - apt-get update -y && apt-get dist-upgrade -y && apt-get install -y git libzip-dev autoconf curl libmcrypt-dev build-essential pkg-config libmariadb-dev-compat libmariadb-dev default-mysql-client mariadb-client mariadb-server
        - docker-php-ext-install mysqli pdo pdo_mysql zip
        # Config MySQL
        - rm -f /tmp/mysql.sock.lock
        - sleep 10 
        # Import BDD for tests units
        - mysql -u root -h "$DB_HOST" < "web/files/database-test.sql"
        # Check BDD
        - echo "SHOW tables;" | mysql -u root -h "$DB_HOST" -D "$MYSQL_DATABASE"
    script:
        - vendor/bin/phpunit --testdox
    allow_failure: false

Answered By – Christopher CARVALHO

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.