Share!

You can import data from CSV (Comma-Separated Values) files directly to MySQL tables using LOAD DATA statement or by using MySQL's own mysqlimport tool.

To be able to import the files, you'll need to be able to figure out the following properties of the CSV files;

  • Line terminator
  • Field terminator
  • Field enclosure

The following example is for importing source.csv with the following content into target_db.target_table;

"aa","ab","ac"
"ba","bb","bc"
"ca","cb","cc"

Import CSV files to MySQL/MariaDB table via LOAD DATA

LOAD DATA LOCAL INFILE 'source.csv' INTO target_db.target_table FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n';

Import CSV files to MySQL/MariaDB table via mysqlimport

mysqlimport --local --fields-terminated-by="," --fields-enclosed-by="\"" target_db.target_table source.csv