I'm trying to use a mysql service in my CI jobs but I keep getting connection refused errors.
My .gitlab-ci.yml
config:
variables:
MYSQL_DATABASE: mysql_test
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
mysql-test:
stage: test
needs: []
image: mysql:8.0.28
variables:
MYSQL_DATABASE: mysql_test
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
services:
- name: mysql:8.0.28
alias: mysql
variables:
MYSQL_DATABASE: mysql_test
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
script:
- echo SELECT 'OK' | mysql mysql_test --host='mysql' --user=root --password=''
Despite having set MYSQL_ALLOW_EMPTY_PASSWORD
in 3 different places, the service container logs are giving me:
2022-08-14 00:11:04 00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started.
2022-08-14 00:11:04 00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2022-08-14 00:11:04 00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started.
2022-08-14 00:11:04 00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of the following:
- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD
GitLab and its runners are on a helm installation (chart gitlab-5.7.0, app version 14.7.0)
CodePudding user response:
I try to run your script it works. you can look in mysql-test #2879743403 Jobs and My Example .gitlab-ci.yml source code
you should echo Variable in your script to verify variable setting is correct.
mysql-test:
tags:
- docker
stage: test
needs: []
image: mysql:8.0.28
variables:
MYSQL_DATABASE: mysql_test
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
services:
- name: mysql:8.0.28
alias: mysql
script:
- echo $MYSQL_DATABASE
- echo $MYSQL_ALLOW_EMPTY_PASSWORD
- |
mysql mysql_test --host='mysql' --user=root --password='' <<SQL
SELECT version();
exit
SQL
CodePudding user response:
The variables weren't being set property for the services because services:variables:
is only supported in GitLab runner 14.8 and onward, having the variables set in the job but not the service seem to work.
Secondly, the MySQL service takes a minute to load. adding a long enough sleep
to the script, along with the variables set correctly has it connecting happily.