Wednesday, December 21, 2022

Dumping and Restoring a DB from host into a Docker container or back

Assuming you have the sql (ie. backup.sql) file on hand:

To create a backup of a mysql DB (ie. dumping it):

$ docker exec CONTAINER_NAME /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql

To restore a mysql dump (for example):

$ cat backup.sql | docker exec -i CONTAINER_NAME /usr/bin/mysql -u root --password=root DATABASE 

Add the --force or -f flag to ignore errors

$ cat backup.sql | docker exec -i CONTAINER_NAME /usr/bin/mysql -u root --password=root  --force DATABASE 

I tested this using the mysql 8.0 image. 


ref: https://stackoverflow.com/questions/46579381/how-to-restore-mysql-dump-from-host-to-docker-container