HomePostsMediumGitHubCurriculum

How to dump mysql using docker machine

27 June, 2021 - 1 min read

Tips,
Docker,
Dart

Need dump a mysql database inside a docker image to an .sql outside of image ? and don't know how to connect to docker interactively?

Dump mysql database from docker machine

To do this you need to connect iteractivelly to runing image with -it and send comands to mysqldump tool located at /usr/bin/mysqldump inside the image, providing your credentials, and after this write the output to an file ?

# Backup
docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql

# Restore
cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE

Or in powershell to preserve linux encoding with Set-content

#Backup

docker exec CONTAINER /usr/bin/mysqldump -u root --password=root -r DATABASE | Set-Content backup.sql

You could also create a function in .bash_profile like

dumpdb () {
   docker exec "${PWD##*/}_my-db_1" \
       mysqldump -uroot --password=password "$1" >backup.sql
}

dumpdb "my_database"

$ exit code 0

© 2021, Made With

gatsbyjs