"ERROR 2013 (HY000): Lost connection to MySQL server during query" 正在加载数据

"ERROR 2013 (HY000): Lost connection to MySQL server during query" while loading data

当我尝试使用 load data 命令将数据加载到我的 MySQL(实际上是 MariaDB)table 时出现错误:

load data local infile '/tmp/my_data.tsv' 
into table my_schema.my_table
fields terminated by '\t' 
optionally enclosed by '"'
escaped by '\'
lines terminated by '\n';

尝试给出的解决方案 here and here 无效。

出于某种原因,当您加载的数据打破了外键约束时,MySQL 给出了这条模糊的错误消息(here 中有更详细的描述)。

我通过直接从命令行执行 load data 命令设法获得了正确的错误消息。 运行:

mysql -u username -p --local-infile --execute= \
"load data local infile '/tmp/my_data.tsv' into table my_schema.my_table \
fields terminated by '\t' optionally enclosed by '\"' escaped by '\\';"

给出了错误信息:

Cannot add or update a child row: a foreign key constraint fails 
(`my_schema`.`my_table`, CONSTRAINT `other_table` FOREIGN KEY
(`id`) REFERENCES `other_table` (`id`))

删除违反外键约束的行后,我能够导入我的数据。