Create a file defining the following variables, with the name .env-for-dump
, for example.
export MYSQL_CONTAINER=
export MYSQL_ORIGIN_PASSWORD=
export MYSQL_ORIGIN_USERNAME=
export ORIGIN_DB_NAME=
export MYSQL_TARGET_PASSWORD=
export MYSQL_TARGET_USERNAME=
export TARGET_DB_NAME=
Then load those variables on the terminal session by running source .env-for-dump
.
docker exec -i $MYSQL_CONTAINER mysqldump --skip-comments -h host_name -u $MYSQL_ORIGIN_USERNAME -p"$MYSQL_ORIGIN_PASSWORD" $ORIGIN_DB_NAME > dump.sql
Note: Do not use the --compact
option. This will make MySQL check your constraints which will cause troubles when using the dump file.
docker exec $MYSQL_CONTAINER sh -c "echo \"CREATE DATABASE $TARGET_DB_NAME;\" | mysql -u $MYSQL_TARGET_USERNAME -p\"$MYSQL_TARGET_PASSWORD\""
Copy the dump file to the mysql container and load it into the database.
docker cp dump.sql $MYSQL_CONTAINER:/dump.sql
docker exec $MYSQL_CONTAINER sh -c 'mysql -u $MYSQL_TARGET_USERNAME -p"$MYSQL_TARGET_PASSWORD" $TARGET_DB_NAME < /dump.sql'