GitLab-ci MySQL service connect via jdbc

Viewed 167

In gitlab-ci CI/CD, during my test, I created a MySQL service and I populate it. After this first configuration step, I want to start a set of test scripts, using a JDBC connection. I have no idea which ip address and port are used by gitlab-ci mysql services.

This is my gitlab-ci:

  variables:
    MYSQL_ALLOW_EMPTY_PASSWORD: "true"
    MYSQL_DATABASE: ticketsDB
  services:
    - name: mysql:5.7
      alias: ticketDBalias
  script:
     - apt-get update && apt-get install -y git curl libmcrypt-dev default-mysql-client
     - mysql --version
     - sleep 20
     - mysql --protocol=tcp -u root -h mysql -e "create database ticketOnline; use ticketOnline;"
     

And I tried with configuration jdbc below, without any result:

url="jdbc:mysql://localhost:3306/ticketOnline?useTimezone=true&serverTimezone=UTC&requireSSL=true"

url="jdbc:mysql://ticketDBalias:3306/ticketOnline?useTimezone=true&serverTimezone=UTC&requireSSL=true"

Configuration error[Cannot connect to database [db]], took 0.0 sec

How can I solve it?

1 Answers

Use the hostname mysql

url="jdbc:mysql://mysql:3306/ticketOnline?useTimezone=true&serverTimezone=UTC&requireSSL=true"

By default, services receive a network alias that is the same name as the image used for the service. In this case, mysql

You can also set an explicit hostname alias for the service in your configuration:

services:
  - name: "mysql:5.7"
    alias: "my-db-hostname"

In which case you would use my-db-hostname as the hostname in your connection string.


As an aside... it's technically possible to set the alias as localhost, but that would be very confusing and potentially cause issues, so best to avoid doing that!

Related