Import data.sql MySQL Docker Container - mysql

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

Related

Can't change credentials (root, neither other user) via environment variables using docker compose command

This is how I start MySQL docker container from docker-compose.yml:
version: '3.9'
services:
mysql_db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root123
MYSQL_USER: my_user_1
MYSQL_PASSWORD: pwd_1
restart: always
volumes:
- db_vol:/var/lib/mysql
ports:
- "3306:3306"
- "33060:33060"
volumes:
db_vol:
The password for root is NOT changed => the default pwd: "root" for user root works.
Leave alone creating the other MYSQL_USER: user_1 with his password.
I can view the server status via for instance MySQL Workbench using default creds: root/root
The server status is green - running and when I click the Users and Privileged pane, this is what I see:
So there is another user1, instead of my_user_1 which I have created.
HOWEVER: When I run the container from cmd line like this:
docker run -d --rm -p 3306:3306 -p 33060:33060 -e
MYSQL_ROOT_PASSWORD=root123 --name=mysql_db mysql:latest
This also works even for user deli1
docker run -d --rm -p 3306:3306 -p 33060:33060 -e
MYSQL_ROOT_PASSWORD=root123 -e MYSQL_USER=deli1 -e
MYSQL_PASSWORD=deli_pwd --name=mysql_db mysql:latest
I do not want to use config files (shared via docker volumes to the container) to override default parameters of MySQL container. Instead, I'd like to use handy environment variables, which I read all around that works nice.. What could be I doing wrong ?
Ubuntu 22.04.1 using VSCode Version: 1.75.1
Date: 2023-02-08T21:35:30.018Z
Electron: 19.1.9
Chromium: 102.0.5005.194
Node.js: 16.14.2
V8: 10.2.154.23-electron.0
OS: Linux x64 5.15.0-60-generic
Sandboxed: No

Docker mysql change password

My docker-compose look like:
version: '3.4'
services:
mysql:
image: mysql:8.0
command:
- --default-authentication-plugin=mysql_native_password
volumes:
- ./mysql-data:/var/lib/mysql
env_file:
- .env
ports:
- 3306:3306
My .env file look like:
MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=db
MYSQL_USER=user
MYSQL_PASSWORD=user
Now if I change the password in .env file, it does not change the password in the docker container after the restart.
I suppose that happens because I have mapped volume from host to container and I think there are all settings saved.
My question is: How can I change the password for mysql, without modyfing or deleting mysql-data folder?
You can log in to the MySQL shell inside the container and then manually change the password following any standard MySQL guide.
To log in to the container, execute docker exec -it <container-id> mysql -u root -p and provide the root password when prompted. After logging in, you can follow this guide to change the password: How to reset the root password in MySQL 8.0.11?
To log in to the container shell (not MySQL shell), execute docker exec -it <container-id> bash.

Running mysqldump for daily backup from a separate Docker container

Is it a bad idea to create a separate docker container to run mysqldump using a cron job for daily backups?
Most people are using either the host machine's cron job or a separate cron container to run mysqldump from inside the container which is being backed up.
I would find it nicer to install mysql and execute mysqldump in the containder dedicated for backups. It would make the entire setup more segregated.
Are there any disadvantages or this approach?
Example of such a docker-compose-yml:
mysql:
image: mysql:latest
environment:
MYSQL_DATABASE: mydb
MYSQL_USER: myuser
MYSQL_PASSWORD: mypassword
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- ./mysql/mysql-data:/var/lib/mysql
mysql-cron:
image: mysql:latest
build: .
environment:
MYSQL_HOST: mysql
MYSQL_DATABASE: mydb
MYSQL_USER: myuser
MYSQL_PASSWORD: mypassword
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- ./backup:/var/backup
The Dockerfile for mysql-cron would install 'cron' and setup crontab (not ready yet).
Referring to the official mysql image's documentation you can docker exec into the running container to dump the database to a path on the host: docker exec some-mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /some/path/on/your/host/all-databases.sql
Assuming that you're using docker on Linux, chances are that you already have cron installed (you don't need to build and maintain your own image with cron).
You can add a cron job on the host where docker is running to exec into the container periodically and dump the database(s) (crontab -e) i.e. every day at 01:00 A.M.: 0 1 * * * docker exec some-mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /some/path/on/your/host/all-databases.sql
Note: cron runs with a specific environment (e.g. ensure docker is in the $PATH available to cron, etc).

TeamCity failed to connect MySQL which is run in Docker

I start MySQL in docker by using below command:
docker run --name mysql-for-teamcity \
-e MYSQL_ROOT_PASSWORD=FAKE-ROOT-PW\
-v ~/MySQL/var_lib_mysql:/var/lib/mysql \
-p 3306:3306 \
-p 33060:33060 \
-it mysql
But TeamCity failed to connect the MySQL, the error message is :
I can connect to the MySQL in Terminal using below command:
mysql -u root --protocol=tcp -p
And database "teamcity" has also been created.
My Environment:
Mac OS X 10.14.1
Docker Desktop 2.0.0.0-mac81(29211)
TeamCity and MySQL are running in seperated Docker containers
Both Docker images tag is latest
The reason is TeamCity and MySQL are running in the separate containers, so when I specified "127.0.0.1" for TeamCity, it is not possible for it to connect to MySQL. Because they are simply not running in the same host.
The solution is using Docker Compose which set up a local network for containers by default.
Step 1: create a docker-compose.yml in an empty directory you want to place your TeamCity:
version: '3'
services:
TeamCity:
image: jetbrains/teamcity-server
ports:
- "8111:8111"
volumes:
- <your TeamCity dir>/data:/data/teamcity_server/datadir
- <your TeamCity dir>log:/opt/teamcity/logs
MySQL:
image: mysql
ports:
- "3306:3306"
volumes:
- <your TeamCity dir>/mysql:/var/lib/mysql
env_file:
- mysql.env
Step 2: create a mysql.env in the same directory:
MYSQL_ROOT_PASSWORD=YOUR-MYSQL-PASSWD
Step 3: run docker-compose up -d in the terminal in
Step 4: open "http://127.0.0.1:8111" in browser
Step 5: input "MySQL:3306" in the DataBase Host field.

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