I'm currently building application and want to test on GitHub actions.
I'm using mysql for database and need to insert test data(csv format) into GitHub action's container, but it's not working well.
Here is the steps.
Create mysql container. → OK
Run script to create table. → NG.
Insert test data into table created on step2. → NG.
In every step, there are no errors, but it seems I can't run scripts in step2 and step3.
Please tell me which part of my code is wrong.
Codes here.
ci.yml
name: sample-ci
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
job-with-mysql-8_0:
runs-on: ubuntu-latest
services:
db:
image: mysql:8
ports:
- 3306:3306
env:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: sampleDB
options: >-
--health-cmd "mysqladmin ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: setup-go
uses: actions/setup-go#v3
with:
go-version: 1.16
- name: checkout
uses: actions/checkout#v3
- name: Show Docker containers
run: docker ps -a
- name: Show databases for root user
run: mysql --protocol=tcp -h localhost -P 3306 -u root -ppassword -e "SHOW DATABASES"
- name: Set up MySQL
run: sudo /lib/systemd/systemd-sysv-install enable mysql
sudo systemctl enable mysql.service
sudo systemctl start mysql.service
- name: Run sample.sql
run: |
mysql --protocol=tcp -h localhost -P 3306 -u root -ppassword -e "$(cat $(find ./ -name sample.sql))"
- name: Show created tables
run: mysql --protocol=tcp -h localhost -P 3306 -u root -ppassword -e "USE sampleDB"
mysql --protocol=tcp -h localhost -P 3306 -u root -ppassword -e "SHOW TABLES"
- name: Insert data into user table
run: sudo /lib/systemd/systemd-sysv-install enable mysql
sudo systemctl enable mysql.service
sudo systemctl start mysql.service
sh ./.github/scripts/sample.sh
- name: Check inserted data
run: mysql --protocol=tcp -h localhost -P 3306 -u root -ppassword -e "USE sampleDB"
mysql --protocol=tcp -h localhost -P 3306 -u root -ppassword -e "SELECT * FROM user"
sample.sql
USE sampleDB;
CREATE TABLE user(
id INT,
name VARCHAR(100) NOT NULL,
PRIMARY KEY(id),
UNIQUE(name)
);
sample.sh
mysql -uroot -ppassword --local-infile=1 sampleDB -e "LOAD DATA LOCAL INFILE '/docker-entrypoint-initdb.d/user.csv' INTO TABLE user FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 LINES"
user.csv
id,name
1,aaa
2,bbb
3,ccc
Related
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?
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.
I have a Job that connects to a running MySQL database and executes some commands:
apiVersion: batch/v1
kind: Job
metadata:
name: do-db-stuff
spec:
template:
spec:
containers:
- name: do-db-stuff
image: mysql:5
command: ["/bin/sh"]
args: ["-c", "mysql -h db -uroot -pmypassword mydb -Bse 'show tables;'"]
restartPolicy: Never
backoffLimit: 10
This job spins on a crash loop, with each pod reporting: ERROR 2005 (HY000): Unknown MySQL server host 'db' (11). If I change the command to ["/bin/sh", "-c", "sleep 3600"] and log in, I can connect from the Job's pod to the database with the command mysql -h db -uroot -pmypassword mydb -Bse 'show tables;' just fine. Based on this, I changed the command in the job to
args: ["-c", "sleep 3 && mysql -h db -uroot -pmypassword mydb -Bse 'show tables;'"]`
and now it works (i.e. correctly prints out the db tables instead of the unknown host error above). My question is why does the sleep 3 do anything? The service and database are not changing in the background at all, so why does waiting in the Job's pod make it connect?
I am using a Github Actions pipeline and starting a mysql server using docker within that pipeline. I am attemtping to run a show tables command, however the output is being suppressed.
In the below pipeline i have a docker-compose file which runs a mysql server. Then i am attempting to connect to it and output the tables. However i never receive the output.
pipeline.yml
name: test-pipeline
on: [ push ]
jobs:
test:
name: Test Migration
runs-on: self-hosted
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: '12'
- name: Setup
run: |
docker-compose -f CI/docker-compose.yml up -d
npm install
- name: Check tables
run: |
mysql --host=127.0.0.1 --protocol=tcp --user=root --password=password testDB -e "show tables;"
echo "here"
If anyone else comes across this problem i fixed it by not specifying the db upfront in the options, instead specifying the database in the -e command.
mysql --host=127.0.0.1 --protocol=tcp --user=root --password=password -e "show databases; use testDB; show tables;"
With docker, I creating a network and a mysql image using:
sudo docker network create --driver bridge mynet_nw
sudo docker run --network=mynet_nw --name=mydb -v /opt/mysql_data:/var/lib/mysql --env="MYSQL_ROOT_PASSWORD=mypassword" mysql:5.7
I want to run:
mysql -uroot -pmypassword -h ????? -P 3306
to be able to run commands in the mydb databse. Instead of ????? I tried mydb but it is giving me errors. Am I missing something?
Unfortunately it doesn't work this way. The networking options (like the --link option) are used to allow the communication among containers.
If you want to connect from the host, you can do it by using the following example:
MYSQL_IPADD=$( docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mydb )
mysql -uroot -pmypassword -h $MYSQL_IPADD -P 3306
Otherwise you can map the port 3306 to the host:
docker run --network=mynet_nw -p 3306:3306 --name=mydb -v /opt/mysql_data:/var/lib/mysql --env="MYSQL_ROOT_PASSWORD=mypassword" mysql:5.7
mysql -uroot -pmypassword -h localhost -P 3306
Another option would be to use the container mysql command to communicate with the server:
docker run -it --network mynet_nw mysql mysql -uroot -pmypassword -h mydb -P 3306
Hi I'm trying to create a MySQL database in Docker, here's my code
in start.sh
#!/bin/sh
echo "Starting DB..."
docker run --name db -d \
-e MYSQL_ROOT_PASSWORD=123 \
-e MYSQL_DATABASE=users -e MYSQL_USER=root -e MYSQL_PASSWORD=123 \
-p 3306:3306 \
mysql:latest
echo "Waiting for DB to start up..."
docker exec db mysqladmin --silent --wait=30 -uroot ping || exit 1
echo "Setting up initial data..."
docker exec -i db mysql -uroot users < setup.sql
and in setup.sql
create table users (
user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100) NOT NULL,
department VARCHAR(100),
name VARCHAR(100),
access_token VARCHAR(30)
);
insert into users (username, department, name, access_token) values ('bzh01', 'Research and Development One', 'Mario1', NULL);
in the terminal, this is the result:
$ bash start.sh
Starting DB...
c7a066b70fa4ad3e02acc3165cfe34ce6a48964cb1190df399404b728cb6059f
Waiting for DB to start up...
mysqld is alive
Setting up initial data...
ERROR 1049 (42000): Unknown database 'users'
Okay, i've finally got this solved, but in a slightly different way.
in my Dockerfile for the database:
FROM mysql
ENV MYSQL_ROOT_PASSWORD root
ENV MYSQL_DATABASE users
ENV MYSQL_USER root_user
ENV MYSQL_PASSWORD abcd123
ADD setup.sql /docker-entrypoint-initdb.d
and then run docker-compose.yml to build
version: '2'
services:
app:
build: ./app
volumes:
- "./app:/src/app"
ports:
- "3030:3000"
depends_on:
- db
environment:
- DATABASE_HOST=db
db:
build: ./test-database