MySQL: ignore errors when importing a dump or CSV
Codemia Knowledge Hub ·
To keep a MySQL import going past bad rows, run SQL dumps with mysql --force, use INSERT IGNORE or LOAD DATA ... IGNORE to skip rows that conflict, or use ON DUPLICATE KEY UPDATE when duplicates should overwrite. Whichever you pick, record what was skipped.
Which ignore option does what
mysql --force: continue executing SQL statements after errors.INSERT IGNORE: convert certain errors to warnings and skip problematic rows.LOAD DATA ... IGNORE: skip duplicate-key rows and convert bad values with a warning during bulk file load.ON DUPLICATE KEY UPDATE: keep import flowing by updating existing rows.
Use the least permissive option that fits your migration rules.
Continue a SQL script with mysql --force
For a dump with many statements, --force keeps going when one statement fails:
This suits non-critical historical loads where partial success is acceptable. Capture the output so you can review failures afterwards:
Skip row-level insert errors with INSERT IGNORE
INSERT IGNORE turns some errors into warnings, including duplicate key conflicts and certain invalid conversions. With this table:
Rows that violate unique constraints are skipped instead of aborting the statement. Invalid values are not skipped, they are adjusted: on MySQL 8.0 the second row was dropped with warning 1062 Duplicate entry '1' for key 'customers.PRIMARY', while the third row was inserted with age clipped to 0 and warning 1264 Out of range value for column 'age' at row 3. It does not cover every error type.
Bulk CSV import with LOAD DATA ... IGNORE
For high-volume file imports this is typically the fastest option:
IGNORE skips rows with duplicate keys, and it also downgrades bad values to warnings, even under strict SQL mode. A CSV value of abc for the integer age column was loaded as 0 with warning 1366 Incorrect integer value: 'abc' for column 'age' at row 3, so check the warnings rather than assuming only duplicates were dropped.
Strict SQL mode changes what gets ignored
Check the current mode first:
Under strict mode, invalid values throw errors in a plain INSERT or LOAD DATA. In less strict modes, or with IGNORE, MySQL coerces values and emits warnings instead. Choose the mode deliberately for migration jobs, document it, and restore it if you change it temporarily.
Load risky data into a staging table first
When data quality is uncertain, import into a staging table, validate, then merge:
Run explicit cleanup queries, reject bad rows, and merge into production tables with audited logic. This avoids importing unknown data straight into live tables.
Auditing skipped or updated rows
Ignoring errors without auditing can hide data corruption. Run SHOW WARNINGS as the very next statement after the import:
Do not put SELECT ROW_COUNT() in between. That SELECT resets the warning list, so a following SHOW WARNINGS returns an empty set and the import looks clean. For the counts, read the summary the mysql client prints after the statement, which for the CSV above was:
You can also store rejects in a dedicated table by preprocessing files or using ETL tooling before the data reaches MySQL. Do not run --force in production without reading the logs afterwards.
Use an upsert when skipping is not acceptable
If duplicates should update existing rows rather than be dropped silently, use an upsert instead of IGNORE:
The row alias (AS new) needs MySQL 8.0.19 or later. The older email = VALUES(email) form still works but has been deprecated since 8.0.20 and raises warning 1287.
This keeps imports idempotent, preserves the latest values, and reduces manual reconciliation when you rerun a migration script after a partial failure.
Related reading
- MySQL IN condition limit
- MySQL Incorrect datetime value '0000-00-00 000000
- MySQL incorrect string value error when save unicode string in Django
- MySQL indexes - what are the best practices?
- MySQL Insert query doesn't work with WHERE clause
- MySQL Invalid use of group function
- MySQL INNER JOIN select only one row from second table
- MySQL InnoDB not releasing disk space after deleting data rows from table
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.