I have see that are other post about these issue, but nothing for the specific case described below. In my gitlab-ci test pipeline configuration I want to run a mysql docker, and connect to it directly from my runner. But I have difficult to connect to the database. This is my gitlab-cy-yml test step:
services:
- docker:dind
- mysql:5.7
script:
- apt-get update && apt-get install -y git curl libmcrypt-dev default-mysql-client
- mysql --version
- sleep 20
- docker login -u XXXXXXXX -p XXXXXXXXX
- docker pull mysql:5.7
- docker run --name ticketsDB -d -p 3304:3306 -it -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql:5.7
- mysql --protocol=tcp -u root -P 3304
- create database ticketOnline;
- use ticketOnline;
The error is during mysql --protocol=tcp -u root -P 3304
connections:
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (111)
Where am I doing wrong?
CodePudding user response:
There is no need for docker:dind
service in you setup.
job:
variables:
MYSQL_ALLOW_EMPTY_PASSWORD: "true"
MYSQL_DATABASE: ticketsDB
services:
- mysql:5.7
script:
- apt-get update && apt-get install -y git curl libmcrypt-dev default-mysql-client
- mysql --version
- sleep 20
- mysql --protocol=tcp -u root -P 3304 -h mysql -e "create database ticketOnline; use ticketOnline;"
# -h to specify the host and -e to run a SQL query
Edit:
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (111)
The error occurs because you are trying to connect to localhost
instead of the mysql service. By default services have aliases that are created by GitLab by default. Learn More in the docs.
If you need multiple mysql instance you can use multiple aliases like so:
services:
- name: mysql:5.7
alias: mysql-1
- name: mysql:5.7
alias: mysql-2
script:
- mysql mysql --protocol=tcp -u root -P 3304 -h mysql-1 # to connect to the first
- mysql --protocol=tcp -u root -P 3304 -h mysql-2 # to connect to the second.