How can i mysqldump from running container on https://hub.docker.com/_/mariadb/ ?
I cant find any useful documentation or data?
Any method for backup and restore database.
This is my my continaer run command :
docker run --name myaapp-mariadb -v /databases/maria:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password -d mariadb:10
If we assume you created the mariadb server container this way:
docker run --name some-mariadb -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mariadb:latest
Then you access it from another client container:
docker run -it --link some-mariadb:mysql \
--rm mariadb:latest \
sh -c 'exec mysqldump -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD" database_name' > database_name_dump.sql
There's lots more helpful usage tip in the mysql official image page.
Accepted answer stands accepted & correct in all its sense. Adding, this for the case where we have mapped the database to an external volume.
So, for example if the container was created using below command
docker run --name mysqldb -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -v /dir_path_on_your_machine/mysql-data:/var/lib/mysql -d mariadb:latest
then, we can execute the below command from cmd line or terminal
docker exec mysqldb mysqldump --user=root --password=password dbname > /dir_path_on_your_machine/mysql-data/dump/db.sql
However, the dump created using above commands will not dump stored procedures, functions and events. We would need extra params with the in order to do that
--triggers Dump triggers for each dumped table.
--routines Dump stored routines (functions and procedures).
--events Dump events.
So, we can modify our command to include the above params for desired result.
Sample update command
docker exec mysqldb mysqldump --routines --triggers --user=root --password=password dbname > /dir_path_on_your_machine/mysql-data/dump/db1.sql
In case, you encounter any import related error ,, check if this helps.
Related
I want to run and open a mysql Cli in docker just with one command . Something like this is not working:
docker run --rm -it -p 33060:3306 --name mydb -e MYSQL_ROOT_PASSWORD=secret mysql mysql -p
I know I can connect to mysql after running my container this way
docker -it docker exec -it mydb mysql -p
but i want to do it in one liner.
Thanks
(Updated)*****
Seems that you can do it in version 8 calling MySQLsh at the end of the command. But unable to do it for previous versions
docker run --name=mk-mysql -p3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -it mysql/mysql-server:8.0.20 mysqlsh
The database server and client are two separate programs. A container only runs one program, so you can't run both the server and the client in the same container, both as the main process. You could write a script that starts the container and then runs mysql to connect to it, but that's about the best you can do.
#!/bin/sh
docker run -d -p 33060:3306 --name mydb -e MYSQL_ROOT_PASSWORD=secret mysql
exec mysql --host=127.0.0.1 --port=33060 --connect-timeout=60 --wait --password
If you're trying to do this to create a database or do other first-time initialization, you can bind-mount an initialization script into /docker-entrypoint-initdb.d and it will run as part of the database setup (only the very first time the database is started).
# Create the storage for the database
# (delete and recreate to rerun the init script)
docker volume create mysql-data
docker run \
-v mysql-data:/var/lib/mysql \
-v $PWD/init.sql:/docker-entrypoint-initdb.d/init.sql \
... \
mysql
If you're just trying to experiment with SQL commands, a serverless database like SQLite might fit your needs better.
the -p parameter is for the ports to be published and should not be part of the -it interactive, that should be your error,
Have a read of the docker run command, in the docker documentation,
https://docs.docker.com/engine/reference/run/
I'm starting to use docker implement mysql in our environment. But I have a little bit confuse about it.
1. I have tried to use command, it's working
sudo docker run --name mysql5.7 --restart always --privileged=true -p 4306:3006 -v /Users/user/mysql/config/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf -v /Users/user/mysql/data:/var/lib/mysql -e MYSQL_USER=“usr” -e MYSQL_PASSWORD=“1234” -e MYSQL_ROOT_PASSWORD=“1234” -d mysql:5.7
But follow docker document, they suggest use volume to persist data. So I tried crate a volume first docker volume -d create local mysql_v
try to link mysql to volume mysql_v, but I don't know how to do it and what is different with step 1.
anyone can suggest it ~?
Like
docker run --name mysql5.7 --restart always -p 4306:3006 \
-v /Users/user/mysql/config/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf \
-v mysql_v:/var/lib/mysql \
-e MYSQL_USER=“usr” -e MYSQL_PASSWORD=“1234” \
-e MYSQL_ROOT_PASSWORD=“1234” -d mysql:5.7
Note, privileged removed, that's just asking for trouble
Ref: official documents
I'm writing a script to create backups of a MySQL database running in a docker container. The database is correctly up and running.
My current code is
#!/bin/bash
PATH=/usr/bin:/usr/local/bin:/root/.local/bin:$PATH
docker-compose exec -T db mkdir -p /opt/booking-backup
docker_backup_path="/opt/booking-backup/dump_prod_$(date +%F_%R).sql"
copy_backup_path="/root/backup_scripts/booking_prod/dump_prod_$(date +%F_%R).sql"
docker-compose exec db mysqldump --add-drop-database --add-drop-table --user=root --password="pw" booking > "$docker_backup_path"
docker-compose exec db mysqldump --add-drop-database --add-drop-table --user=root --password="pw" booking > "/opt/booking-backup/dump_prod.sql"
[ -d ./backup ] || mkdir ./backup
docker cp $(docker-compose ps -q db):$docker_backup_path $copy_backup_path
However, when I execute it it throws this error:
Error: No such container:path: f0baa241becd20d2690bb901fb257a4bbec8cac17e6f1ce6d50adb9532bbae03:/opt/booking-backup/dump_prod_2019-05-28_14:23.sql
What makes this weirder is that I have the exact same code (but with booking switched out for abc, and with PSQL instead of MySQL) that works correctly.
It appears that this line
docker-compose exec db mysqldump --add-drop-database --add-drop-table --user=root --password="pw" booking > $docker_backup_path
does not create the output file, but when I use tee I can see the contents of the dump and they are correct.
What's going wrong here?
The shell redirections
docker-compose exec db mysqldump ... > "$docker_backup_path"
docker-compose exec db mysqldump ... > "/opt/booking-backup/dump_prod.sql"
# -----------------------------------^ here
... will be expanded by your local shell, not inside the container. Meaning the files are written to your local filesystem not to the container's filesystem.
I have created a docker container based on the official image of MySQL from Docker Hub. It works fine, but I have some troubles with the database import.
My file with the SQL-Instructions is already stored in the folder /docker-entrypoint-initdb.d of my container, but it doesn't work! I have copied my sql-import.sql to /var/lib/docker/volumes/mysql-dump/_data, but I can only see the name of my database when I call "SHOW DATABASES;" within my container. There is no table available when I call "SHOW TABLES FROM myDB;". What can I do to import the content of my MySQL Database?
Here is my Dockerfile:
FROM mysql:5.7
ADD ./init-scripts/*.sql /docker-entrypoint-initdb.d/
ADD ./config/my.cnf /root/
RUN cd /root/ && \
chmod 0600 my.cnf && \
mv my.cnf .my.cnf
ENV MYSQL_DATABASE=regex
ENV MYSQL_ROOT_PASSWORD=mypassword
EXPOSE 3306
There are several methods to import. With docker exec if your contener is running:
Solution 1:
docker exec -i <id_conteneur> /usr/bin/mysql -u <fooUser> -e "CREATE DATABASE mydb"
cat schema.sql | docker exec -i <id_conteneur> /usr/bin/mysql -u <fooUser> --password=<password> <database>
Solution 2:
In a bash script:
#!/bin/bash
/usr/bin/mysqld_safe --skip-grant-tables &
mysql -u <fooUser> -e "CREATE DATABASE mydb"
mysql -u <fooUser> mydb < schema.sql
And add to your DockerFile with Run command:
ADD create_db.sh /tmp/create_db.sh
RUN /tmp/create_db.sh
I use the official MySQL docker image, and I am having difficulty exporting data from the instance without errors. I run my export like this:
docker run -it --link containername:mysql --rm mysql sh -c
'exec mysqldump
-h"$MYSQL_PORT_3306_TCP_ADDR"
-P"$MYSQL_PORT_3306_TCP_PORT" -uroot
-p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"
dbname'
| gz > output.sql.gz
However, this results in the warning:
"mysqldump: [Warning] Using a password on the command line interface can be insecure."
As the first line of the outputted file. Obviously this later causes problems for any other MySQL processes which are used to consume the data.
Is there any way to suppress this warning from the mysqldump client?
A little late to answer but this command saved my day.
docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql
I realise that this is an old question, but for those stumbling across it now I put together a post about exporting and importing from mysql docker containers: https://medium.com/#tomsowerby/mysql-backup-and-restore-in-docker-fcc07137c757
It covers the "Using a password on the command line interface..." warning and how to bypass it.
Run Following command on terminal
docker exec CONTAINER_id /usr/bin/mysqldump -uusername --password=yourpassword databasename> backup.sql
Replace the
CONTAINER_id. username, yourpassword
with specific to your configuration.
To get Container Id :
docker container ls
To eliminate this exact warning you can pass password in MYSQL_PWD environment variable or use other connection method - see http://dev.mysql.com/doc/refman/5.7/en/password-security-user.html
docker run -it --link containername:mysql --rm mysql sh -c
'export MYSQL_PWD="$MYSQL_ENV_MYSQL_ROOT_PASSWORD"; exec mysqldump
-h"$MYSQL_PORT_3306_TCP_ADDR"
-P"$MYSQL_PORT_3306_TCP_PORT" -uroot
dbname'
| gz > output.sql.gz
Here's how I solved this to dump a mysql db into a file.
I created a dump-db.sh file with the content:
# dump db from docker container
(docker exec -i CONTAINER_ID mysqldump -u DB_USER -pDB_PASS DB_NAME) > FILENAME.sql
To get the CONTAINER_ID list them: docker container list
Add run permissions to the script:
chmod o+x dump-db.sh
Run it:
./dump-db.sh
Remember to replace the CONSTANTS above with your own data.
I always create bash "tools" in my repo root with which I can repeat common tasks, such as database dumps. With bash, you can also load your .env file, so your credentials are not in a file in the repo, but just in your .env file.
#!/bin/bash
# load .env
set -o allexport; . ./.env; set +o allexport
# setup
TIMESTAMP=$(date +%Y-%m-%d__%H.%M)
BACKUP_DIR="dockerfiles/db"
CONTAINER_NAME="cp-db"
# dump
docker exec $CONTAINER_NAME /usr/bin/mysqldump -u$DB_USER --password=$DB_PASSWORD $DB_NAME> $BACKUP_DIR/dump__$TIMESTAMP.sql