web123456

Docker Practices Common Operations: Data in mysql Container Import | Export | Backup, etc.

First of all, we suggest to refer to these articlesDockerinfrastructural
Docker Basics: Docker Overview, Version Installation, Images, Containers, Networks, Data Volumes, Repositories
Dockerfile - The Basics of Docker
Docker Basics - Docker Compose

1.mysqlBackup | Export | Import of Images

Steps:
0. View the docker image for source mysql;

  • Under root privileges, thedocker ps Finding the mysql container

1. Backup the source mysql database;

  • docker exec -it xxx (note: docker container name or ID is fine) mysqldump -u xxx(data username) -p xxx(database password) db_name(database name) > (export table path)
  • For an explanation of the above commands, you can refer to the several basic articles above. Here it is also pasted below

Execute the new command docker exec in the container
Role:
Run a command in the container
Command Format:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Command Parameters (OPTIONS):
-d, --detach Runs commands in the background.
-i, --interactive Binds the current STDIN even if the container is not connected.
-t, --tty Assign a virtual terminal.
-w, --workdir string Specifies the working directory in the container.
-e, --env list Setting runtime environment variables in the container

2. Import into the target mysql container;

  • docker exec -i xxx(container name or id) db_name(database name) < (path where the file is stored)
    Of course, it's possible to do this in steps, by first copying the files inside the container and then importing them via source, etc.
  • Law II:docker cp container name:/xxx/(path); Then go inside the container and open the mysql command linedocker exec -ti xxx(container name or id) db_name(database name) <

Tips: 1. database backup import and other operations, try to use the command line way; 2. also try not to let the sql file in the different scripts between the move, each time the transfer of the end of the file size should also pay attention to whether the size of the file has changed;