Running MySQL inside GitLab Runner

Hi,

My goal is to run database tests inside the CI pipeline for my Node.js application. I imagine it being that when I push code, the .yml script tells the runner to create a database and then when npm test script is executed the database is already set up and waiting to be used for testing purposes. What I get instead is just mysql: command not found with the given .yml script. I did set up the environment variables in the settings already.
I am quite inexperienced with the whole GitLab Runner environment so any pointers or tips on how can I fix or solve my problem is highly appreciated.

image: node:latest

services:
  - mysql:5.7

variables:
  # Configure mysql service (https://hub.docker.com/_/mysql/)
  MYSQL_DATABASE: $MYSQL_DB
  MYSQL_ROOT_PASSWORD: $MYSQL_PASS

stages:
  - install
  - db-setup
  - test

install:
  stage: install
  script:
    - echo "NPM SET UP"
    - npm install

db-setup:
  stage: db-setup
  script:
    - echo "DB SET UP:"
    - mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql --execute "CREATE DATABASE "$MYSQL_DATABASE";"

test:
  stage: test
  script:
    - echo "TEST RUN:"
    - npm test

Hi @bmorkunas

I’m assuming that you’re either using GitLab shared runners, or your own Docker runners?

I suspect your main problem here is that the node:latest image doesn’t come with a MySQL client. Can you try the config below and see if that gets you a bit closer?

Hi @snim2,

Thank you very much for your suggestion, it has indeed pushed me further. I am using a stock GitLab Shared Runner.

With this the mysql client is found and successfully initialized, but when tests are run I get Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'runner'@'127.0.0.1' (using password: YES). I initially assumed that this runner will have a user with all privileges granted already. But that seems to not be the case. May I ask for a suggestion on the further steps I should take to overcome this? Thank you very much!

Hi @bmorkunas
you need to configure your mysql container using variables. Here is readme for mysql container on Docker Hub where you can find available variables.

Add variables keyword with variables to configure the mysql service. Example from official example.

variables:
  # Configure mysql environment variables (https://hub.docker.com/_/mysql/)
  MYSQL_DATABASE: $MYSQL_DB
  MYSQL_ROOT_PASSWORD: $MYSQL_PASS