Docker MySQL ERROR 1049 (42000): Unknown database 'users' - mysql

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

Related

How to run sql scripts in GitHub Actions

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

Gitlab-ci running mysql docker and connect it with error ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (111)

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.

Grant Privileges to Root via Dockerfile

Is there some way to grant all privileges to root via Dockerfile ou docker-compose.yml?
I'm trying to boot up a mysql:latest image like this:
docker-compose.yml
version: "3.9"
services:
mysql_database:
container_name: mysql_test_server
restart: always
build: .
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
ports:
- 3306:3306
volumes:
- ./data:/var/lib/mysql
- ./config:/etc/mysql/conf.d
environment:
MYSQL_ROOT_PASSWORD: root
dockerfile (the build: . on .yml)
FROM mysql:latest
RUN mysql -u root -proot -h localhost -p 3306
RUN grant all privileges on *.* to 'root'#'%' identified by 'root';
RUN flush all privileges;
RUN exit;
EXPOSE 3306
TLDR; I just want to grant privileges for remote connections (SQLYog, DBeaver, etc) automatically... if docker-compose up, run this command so I don't need to do manually, like docker exet -it mysql_test_server mysql -u root ...
I awalys get this:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Your MySQL server is not running in this docker file, so you are getting errors.
The default docker image has root user and root user has all privileges.
You have to use environment variables to achieve what you try to do:
Run this command:
docker run --name some-mysql \
-v /my/own/datadir:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=rootPassword \
-e MYSQL_ROOT_HOST="%" \
-p 3306:3306 \
-d mysql:tag
You have to replace paths in the -v parameter.
Then you can login to MySQL with command:
mysql -uroot -prootPassword -h127.0.0.1
The parameter you should take a look is MYSQL_ROOT_HOST. That variable is used in the MySQL container entrypoint: https://github.com/docker-library/mysql/blob/master/8.0/docker-entrypoint.sh#L232-L239

Import data.sql MySQL Docker Container

If I have a data.sql, how I can import database to my mysql docker container? How I can import database data. In a dockerised world this adds a layer of complexity. some methods please.
Here my docker-compose.yml:
nginx:
build: ./nginx/
container_name: nginx-container
ports:
- 80:80
links:
- php
volumes_from:
- app-data
php:
build: ./php/
container_name: php-container
expose:
- 9000
links:
- mysql
volumes_from:
- app-data
app-data:
image: php:7.0-fpm
container_name: app-data-container
volumes:
- ./www/html/:/var/www/html/
command: "true"
mysql:
image: mysql:latest
container_name: mysql-container
ports:
- 3306:3306
volumes_from:
- mysql-data
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: name_db
MYSQL_USER: user
MYSQL_PASSWORD: password
mysql-data:
image: mysql:latest
container_name: mysql-data-container
volumes:
- /var/lib/mysql
command: "true"
You can import database afterwards:
docker exec -i mysql-container mysql -uuser -ppassword name_db < data.sql
Mount your sql-dump under/docker-entrypoint-initdb.d/yourdump.sql utilizing a volume mount
mysql:
image: mysql:latest
container_name: mysql-container
ports:
- 3306:3306
volumes:
- ./dump.sql:/docker-entrypoint-initdb.d/dump.sql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: name_db
MYSQL_USER: user
MYSQL_PASSWORD: password
This will trigger an import of the sql-dump during the start of the container, see
https://hub.docker.com/_/mysql/ under "Initializing a fresh instance"
I can't seem to make this work with the latest mysql or mysql:5.7. So I use mariaDB instead. Here is my docker-compose.yaml code.
version: '3'
services:
mysql:
image: mariadb:10.3
container_name: mariadb
volumes:
- container-volume:/var/lib/mysql
- ./dump.sql:/docker-entrypoint-initdb.d/dump.sql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: name_db
ports:
- "3306:3306"
volumes:
container-volume:
Another option if you don't wanna mount a volume, but wanna dump a file from your local machine, is to pipe cat yourdump.sql. Like so:
cat dump.sql | docker exec -i mysql-container mysql -uuser -ppassword db_name
See:
https://gist.github.com/spalladino/6d981f7b33f6e0afe6bb
Just write docker ps and get the container id and then write the following;
docker exec -i your_container_id mysql -u root -p123456 your_db_name < /Users/your_pc/your_project_folder/backup.sql
Import using docker-compose
cat dump.sql | docker-compose exec -T <mysql_container> mysql -u <db-username> -p<db-password> <db-name>
combine https://stackoverflow.com/a/51837876/1078784
and answers in this question, I think the best answer is:
cat {SQL FILE NAME} | docker exec -i {MYSQL CONTAINER NAME} {MYSQL PATH IN CONTAINER} --init-command="SET autocommit=0;"
for example in my system this command should look like:
cat temp.sql | docker exec -i mysql.master /bin/mysql --init-command="SET autocommit=0;"
also you can use pv to moniter progress:
cat temp.sql | pv | docker exec -i mysql.master /bin/mysql --init-command="SET autocommit=0;"
And the most important thing here is "--init-command" which will speed up the import progress 10 times fast.
I can import with this command
docker-compose exec -T mysql mysql -uroot -proot mydatabase < ~/Desktop/mydatabase_2019-10-05.sql
you can follow these simple steps:
FIRST WAY :
first copy the SQL dump file from your local directory to the mysql container. use docker cp command
docker cp [SRC-Local path to sql file] [container-name or container-id]:[DEST-path to copy to]
docker cp ./data.sql mysql-container:/home
and then execute the mysql-container using (NOTE: in case you are using alpine version you need to replace bash with sh in the given below command.)
docker exec -it -u root mysql-container bash
and then you can simply import this SQL dump file.
mysql [DB_NAME] < [SQL dump file path]
mysql movie_db < /home/data.sql
SECOND WAY : SIMPLE
docker cp ./data.sql mysql-container:/docker-entrypoint-initdb.d/
As mentioned in the mysql Docker hub official page.
Whenever a container starts for the first time, a new database is created with the specified name in MYSQL_DATABASE variable - which you can pass by setting up the environment variable see here how to set environment variables
By default container will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d folder. Files will be executed in alphabetical order. this way your SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.
for more details you can always visit the official page
do docker cp file.sql <CONTAINER NAME>:/file.sql first
then docker exec -it <CONTAINER NAME> mysql -u user -p
then inside mysql container execute source \file.sql
Trying "docker exec ... < data.sql" in Window PowerShell responses with:
The '<' operator is reserved for future use.
But one can wrap it out with cmd /c to eliminate the issue:
cmd /c "docker exec -i mysql-container mysql -uuser -ppassword name_db < data.sql"
This one work for me
$ docker exec -i NAME_CONTAINER_MYSQL mysql -u DB_USER -pPASSWORD DATABASE < /path/to/your/file.sql
First if do you want to know what is the NAME_CONTAINER_MYSQL, you should use
this command below :
$ docker ps
In the output column NAME you will see the NAME_CONTAINER_MYSQL that do you need to replace in the command above.
You can run a container setting a shared directory (-v volume), and then run bash in that container. After this, you can interactively use mysql-client to execute the .sql file, from inside the container. obs: /my-host-dir/shared-dir is the .sql location in the host system.
docker run --detach --name=test-mysql -p host-port:container-port --env="MYSQL_ROOT_PASSWORD=my-root-pswd" -v /my-host-dir/shared-dir:/container-dir mysql:latest
docker exec -it test-mysql bash
Inside the container...
mysql -p < /container-dir/file.sql
Custom parameters:
test-mysql (container name)
host-port and container-port
my-root-pswd (mysql root password)
/my-host-dir/shared-dir and /container-dir (the host directory that will be mounted in the container, and the container location of the shared directory)
you can copy the export file for e.g dump.sql using docker cp into the container and then import the db. if you need full instructions, let me know and I will provide

docker compose yaml - command

I want to run a creation of a new database using mysql
this is the snippet I have in my docker-compose.yml file
mysql:
image: mysql
container_name: mysql-machine
ports:
- 3306:3306
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_DATABASE: mxdb
MYSQL_USER: mxdb
MYSQL_PASSWORD: mxdb
command: mysqladmin create testing_db
Now when i run docker-compose up
I watch the console, and it says
mysql_1 | mysqladmin: connect to server at 'localhost' failed
mysql_1 | error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
mysql_1 | Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!
So how do I re-write the command piece, so i get the service working properly?
I want to create more than one database. So manually using commands is the easiest way.
I think you should run the mysql container and then have another container do the data import. Example:
.credentials
MYSQL_ALLOW_EMPTY_PASSWORD=true
MYSQL_USER=mxdb
MYSQL_PASSWORD=mxdb
MYSQL_DATABASE=mxdb
Run the container:
docker run --name mydb -d --env-file .credentials mysql
If you want to import data from file, create a new container, link to the one that is already running and do the import:
docker run --rm -t --link mydb:DB -v /path/to/dump.sql:/dump.sql mysql bash -c "mysql -h DB -u mxdb -pmxdb mxdb < /dump.sql"
If you just want to run a command, use:
docker run --rm -t --link mydb:DB mysql mysql -h DB -u mxdb -pmxdb -e "CREATE DATABASE bar"
or
docker exec -t mydb mysql -u mxdb -pmxdb -e "CREATE DATABASE bar"
I don't think you should override the command to create the database.
In docker-compose, the command should be the command to start the given service in the docker image. In your case, the service is a MySQL server. If you gives a command for the mysql service in your docker_compose.yml, the MySQL server will never start.
What you should do is start the mysql service, and then run commands in it.
mysql:
image: mysql
container_name: mysql-machine
ports:
- 3306:3306
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_DATABASE: mxdb
MYSQL_USER: mxdb
MYSQL_PASSWORD: mxdb
Start the service:
docker-compose up
Connect to it:
mysql -umxdb -pmxdb
Then create the database:
create database testing_db;
If you need to automatize this database creation, you could put these SQL commands in a file, and do when needed:
cat init_db.sql | mysql -umxdb -pmxdb
If you use this image of mysql: tutum/mysql
You can add the name of the database you want to create at startup as an environment variable:
environment:
-ON_CREATE_DB="newdatabase"
Another solution is to put a shell script on the command part. In the script you start mysql and then create databases and add users:
command : run.sh
And on you script:
/usr/bin/mysqld_safe &
mysqladmin create testing_db