[英]How to skip row when importing bad MySQL dump
Given bad mysqldump
that causes error on import: 给出导致错误导致错误的
mysqldump
:
namtar backups # mysql -p < 2010-12-01.sql
Enter password:
ERROR 1062 (23000) at line 8020: Duplicate entry 'l�he?' for key 'wrd_txt'
Is there an easy way to tell import to just skip given row and continue? 是否有一种简单的方法可以告诉导入只是跳过给定的行并继续?
(Yes, I know I can manually edit the file or parse output, but it's not very convinient) (是的,我知道我可以手动编辑文件或解析输出,但它不是很方便)
If you can make the dump again you could add --insert-ignore
to the command-line when dumping. 如果您可以再次进行转储,则可以在转储时将
--insert-ignore
添加到命令行。
Or you can try using the mysqlimport command with --force
,which will continue even if it encounters MySQL Errors. 或者你可以尝试使用带有
--force
的mysqlimport命令,即使它遇到MySQL错误也会继续。
mysql -f -p < 2010-12-01.sql
-f
(强制)是这里的操作选项,为我工作。
Following the advice from jmlsteele's answer and comment, here's how to turn the inserts into INSERT IGNORE
on the fly. 根据jmlsteele的回答和评论的建议,这里是如何动态地将插入转换为
INSERT IGNORE
。
If you're importing from an sql
file: 如果您从
sql
文件导入:
sed -e "s/^INSERT INTO/INSERT IGNORE INTO/" < 2010-12-01.sql | mysql -p
If you're importing from a gz
file, just pipe the output from gunzip into sed instead of using the file input: 如果您从
gz
文件导入,只需将gunzip的输出传输到sed而不是使用文件输入:
gunzip < 2010-12-01.sql.gz | sed -e "s/^INSERT INTO/INSERT IGNORE INTO/" | mysql -p
Just a thought did you delete the MySQL directives at the top of dump? 只是一个想法你删除了转储顶部的MySQL指令吗? (I unintentionally did when I restarted a restore after deleting all the records/tables I'd already inserted with a sed command).
(我在删除所有已经使用sed命令插入的记录/表后重新启动恢复时无意中做了)。 These directives tell MySQL ,among other things, not to do unique tests, foreign key tests etc)
这些指令告诉MySQL,除其他外,不要做独特的测试,外键测试等)
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
The other options certainly are viable options, but another solution would be to simply edit the .sql
file obtained from the mysqldump
. 其他选项当然是可行的选择,但另一种解决方案是简单地编辑从
mysqldump
获得的.sql
文件。
Change: 更改:
INSERT INTO table_name ...
TO 至
INSERT IGNORE INTO table_name ...
Great tip. 很棒的提示。 I did it a little different but same result.
我做了一点不同但结果相同。
perl -pi -e 's/INSERT INTO/INSERT IGNORE INTO/g' filename.sql
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.