Mysql Import csv file to database

Nguyễn Tuấn Phúc
1 min readMar 17, 2020

To import CSV file to mysql database, we have many ways

  1. DATA LOAD INFILE → this need to be logged in to mysql console, more info here https://dev.mysql.com/doc/refman/8.0/en/load-data.html
  2. use mysqlimport

in terminal run command

mysqlimport -c col_a,col_b --ignore-lines=1 --fields-terminated-by="," -u[user_name] -p database_name /path/[table_name.csv]

Explanation:

-c: columns in table which you want to insert. Each column is in order of csv data.

For example, you have 4 columns id,name,age,created_at in table, but you just need to import name and age — -> -c name,age

Remember that the data of name, age in csv file must be in order separately

--fields-terminated-by="," --> each column separated by ,

database_name is name of database you want to import

table_name.csv --> the file name must be the same as table_name

--ignore-lines=n --> this will ignore number of lines from beginning of csv file

--

--