MySQL
CSV Import
LOAD DATA INFILE
Database Management
Data Migration

Import data in MySQL from a CSV file using LOAD DATA INFILE

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

LOAD DATA INFILE is one of the fastest ways to import CSV data into MySQL. It is much faster than row-by-row INSERT statements because MySQL parses and loads the file in bulk. The practical challenge is not the command itself but matching the CSV format, table schema, and server security settings so the import behaves exactly as expected.

Know the Difference Between INFILE and LOCAL INFILE

MySQL supports two related forms:

  • 'LOAD DATA INFILE reads a file from the server host'
  • 'LOAD DATA LOCAL INFILE reads a file from the client side'

A typical server-side example:

sql
1LOAD DATA INFILE '/var/lib/mysql-files/users.csv'
2INTO TABLE users
3FIELDS TERMINATED BY ','
4ENCLOSED BY '"'
5LINES TERMINATED BY '\n'
6IGNORE 1 LINES
7(name, email, age);

This tells MySQL:

  • where the file lives
  • which table receives the rows
  • how fields and lines are separated
  • whether the first line is a header
  • which columns should receive the CSV values

That last point is especially important. Explicit column lists make imports much safer than relying on physical column order.

Match the CSV Format Precisely

Most import failures come from format mismatch rather than SQL syntax. Pay attention to:

  • field delimiter
  • quote character
  • line ending
  • header row
  • character set

For example, a semicolon-delimited file:

sql
1LOAD DATA INFILE '/var/lib/mysql-files/products.csv'
2INTO TABLE products
3CHARACTER SET utf8mb4
4FIELDS TERMINATED BY ';'
5ENCLOSED BY '"'
6LINES TERMINATED BY '\r\n'
7IGNORE 1 LINES
8(sku, title, price);

If any of those details are wrong, MySQL may shift columns, misread quotes, or load bad values without giving the kind of friendly error message people expect.

Use User Variables for Transformation

One of the most useful features of LOAD DATA INFILE is that incoming CSV fields can be loaded into variables first and then transformed.

sql
1LOAD DATA INFILE '/var/lib/mysql-files/orders.csv'
2INTO TABLE orders
3FIELDS TERMINATED BY ','
4ENCLOSED BY '"'
5LINES TERMINATED BY '\n'
6IGNORE 1 LINES
7(@order_id, @customer_name, @created_at)
8SET
9  order_id = CAST(@order_id AS UNSIGNED),
10  customer_name = NULLIF(@customer_name, ''),
11  created_at = STR_TO_DATE(@created_at, '%Y-%m-%d %H:%i:%s');

This is the right place to handle:

  • empty strings that should become NULL
  • string dates that need parsing
  • basic type cleanup

It is far cleaner than loading dirty values first and repairing them afterward with a second batch of queries.

Watch Server Security Settings

Modern MySQL installations often restrict bulk file loading. A common guardrail is secure_file_priv, which controls where the server is allowed to read files from.

Check it with:

sql
SHOW VARIABLES LIKE 'secure_file_priv';

If the value points to a specific directory, put the CSV there. If LOCAL INFILE is disabled, client-side loading may fail until both the server and client permit it.

This is a frequent source of confusion: the SQL statement is fine, but the server policy blocks file access.

Validate the Import with a Small Sample First

Before loading millions of rows, test with a small file:

sql
SELECT COUNT(*) FROM users;

Run the import, then verify:

sql
SELECT COUNT(*) FROM users;
SELECT * FROM users LIMIT 5;

This quick check catches problems such as:

  • columns shifted by bad delimiters
  • truncated values
  • unexpected header import
  • date parsing mistakes

Bulk import is fast enough that it is worth doing a tiny safe rehearsal instead of debugging a huge bad load later.

Common Pitfalls

  • Using the wrong line ending or delimiter for the actual CSV file.
  • Forgetting IGNORE 1 LINES and importing the header row as data.
  • Relying on table column order instead of specifying the column list explicitly.
  • Ignoring secure_file_priv or LOCAL INFILE restrictions and treating the problem as SQL syntax.
  • Loading raw strings directly when the file really needs transformation through user variables.

Summary

  • 'LOAD DATA INFILE is the fastest standard way to bulk-import CSV into MySQL.'
  • Success depends on matching the CSV format details precisely.
  • Use explicit column lists and user variables to keep the import predictable.
  • Check server security settings such as secure_file_priv before debugging the statement itself.
  • Test imports on a small sample first so formatting mistakes are caught early.

Course illustration
Course illustration

All Rights Reserved.