Three ways to import SQL files into MySQL started by docker

Time:2021-6-14

Three ways to import SQL files into MySQL started by docker

Preparation of materials

First of all, we need to prepare the file to be importedexamples.sql;

Import file into docker container

docker cp examples.sql mysql:/examples.sql

Log in to the container to see if the file is imported successfully

docker exec -it mysql bash
ls -l mydata_mytable .sql

If you need to clear the original table data

truncate table table_name;
delete * from table_name;

The differences between truncate and delete are as follows:

  • Truncate is an overall deletion, and delete is an item by item deletion
  • Truncate does not log, delete logs
  • Truncate does not use triggers. It resets the identity column and autoincrement field

What should we do if the table is dependent

SET FOREIGN_KEY_CHECKS = 0; 
TRUNCATE table $table_name; 
SET FOREIGN_KEY_CHECKS = 1;

Import SQL

mysql -u username -p -D mydatabase examples.sql


Welcome to pay attention to personal wechat
Three ways to import SQL files into MySQL started by docker