mysqldump has the "--tab" option to split up the dump separate files for each table. mysql creates a .sql file (with schema) and a .txt file (with data) for each table.
The .sql files work fine, but how do I import the data from the .txt files?
我自己找到了解决方案,请看这里: http : //dev.mysql.com/doc/refman/5.1/en/reloading-delimited-text-dumps.html
Official documentation doesn't covert the case of importing these *.txt
and foreign key constrains. There is still open long-lived bug#19996 to implement foreign key ignore in mysqlimport
. So it has to be done manually.
#!/bin/bash -e
DIR=/path/to/csv/backup
DATABASE=database
USER=user
PASSWORD=password
for filename in $DIR/*.txt
do
tablename=`basename $filename .txt`
mysql --user=$USER --password=$PASSWORD $DATABASE <<EOF
SET FOREIGN_KEY_CHECKS=0;
LOAD DATA INFILE '$filename' INTO TABLE \`$tablename\`;
EOF
done
Also I would like to note that there's no much sense in storing these per-table *.sql
files produced by mysqldump --tab
, because there's also a foreign key issue on fields. As a schema is always known I suggest just to delete them after backup is completed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.