Requirement: MySQL database data is stored in the docker container in the remote host, and now it is necessary to import its database information into the docker container in the local machine
Import the database in the docker in the remote host to the local
The database name in the docker container of the remote host is:
the file exported by testdb is named: dump.sql The
remote host IP is: 192.168.240.123 The
database port is: 3306 The
database account is: root
database password is: 123123
Execute the following commands locally:
$ /usr/bin/mysqldump testdb --column-statistics=0 --result-file=./dump.sql --complete-insert --user=root --host=192.168.240.123 --port=3306 --password=123123
mysql8.0 version needs to add --column-statistics=0, otherwise an error will be reported:
mysqldump: Couldn't execute...Unknown table'COLUMN_STATISTICS' in information_schema (1109)
Copy the local database file to the docker container
The container name is: docker_mysql
$ docker cp dump.sql docker_mysql:/opt
Add the database file to the mysql database in the docker container
Execute the following command to enter the docker container:
$ docker exec -it docker_mysql /bin/bash
After entering the container, execute the following command to enter mysql:
$ mysql -u root -p
Create a database, consistent with the name of the previously exported database:
> create database testdb;
Import the database file into the database:
> use testdb;
> source /opt/dump.sql;
So you can use the local database!