LOAD DATA INFILE quickly imports CSV data directly into a MariaDB table.
Use LOAD DATA [LOCAL] INFILE
. It streams a CSV file straight into a table, handling field separation, quoting, and line termination automatically.
Confirm the CSV columns align with the target table order—Customers.csv
should list id,name,email,created_at
. Save with UTF-8 encoding and Unix line breaks for predictable parsing.
Run GRANT FILE ON *.* TO 'app_user'@'%' IDENTIFIED BY 'strong_pw';
then FLUSH PRIVILEGES;
.Without the FILE
privilege, MariaDB blocks access to server-side CSV files.
Place Customers.csv
in MariaDB’s secure-file-priv directory (SHOW VARIABLES LIKE 'secure_file_priv';
). Use the command in the next section to import.
Yes. Append IGNORE 1 LINES
so the column names do not enter the table.
List the target columns in parentheses after the table name.This decouples CSV order from schema order and supports partial imports.
Increase bulk_insert_buffer_size
, disable indexes with ALTER TABLE ... DISABLE KEYS;
, then re-enable after the load. Wrap the import in a transaction to roll back on error.
Your client library may disable LOCAL for security. Enable it with the --local-infile
flag or the allowLocalInfile=true
connection parameter.
.
Yes. It bypasses the SQL parser for each row, offering 10–20× speedups on million-row files.
Yes. Provide the column list and use user variables for skipped CSV fields, e.g., (id, @skip, email)
.
Represent NULL as \N in the file or use SET column=NULLIF(@var,'')
in the command.