MySQL
database import
error handling
SQL
data migration

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:

bash
mysql --host=127.0.0.1 --user=app --password --database=mydb --force < dump.sql

This suits non-critical historical loads where partial success is acceptable. Capture the output so you can review failures afterwards:

bash
mysql --user=app --password --database=mydb --force < dump.sql > import.log 2>&1

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:

sql
1CREATE TABLE customers (
2  id    INT PRIMARY KEY,
3  email VARCHAR(100),
4  age   TINYINT UNSIGNED
5);
6
7INSERT IGNORE INTO customers (id, email, age)
8VALUES
9  (1, '[email protected]', 31),
10  (1, '[email protected]', 22),
11  (2, '[email protected]', -1);
12
13SHOW WARNINGS;

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:

sql
1LOAD DATA LOCAL INFILE '/tmp/customers.csv'
2IGNORE
3INTO TABLE customers
4FIELDS TERMINATED BY ','
5OPTIONALLY ENCLOSED BY '"'
6LINES TERMINATED BY '\n'
7IGNORE 1 LINES
8(id, email, age);

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:

sql
SELECT @@sql_mode;

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:

sql
1CREATE TABLE staging_customers LIKE customers;
2
3LOAD DATA LOCAL INFILE '/tmp/customers.csv'
4INTO TABLE staging_customers
5FIELDS TERMINATED BY ','
6OPTIONALLY ENCLOSED BY '"'
7LINES TERMINATED BY '\n'
8IGNORE 1 LINES;

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:

sql
SHOW WARNINGS LIMIT 50;

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:

text
Query OK, 1 row affected, 3 warnings (0.01 sec)
Records: 3  Deleted: 0  Skipped: 2  Warnings: 3

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:

sql
1INSERT INTO customers (id, email, age)
2VALUES (1, '[email protected]', 33), (2, '[email protected]', 28) AS new
3ON DUPLICATE KEY UPDATE
4  email = new.email,
5  age = new.age;

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.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design