Bash Script to Dump a MySQL Table into a CSV File

Posted on February 20, 2019 at 8:06 pm

Simple bash script to dump a MySQL table into a CSV file:

mysql dbname -u'user' -p'password' <<QUERY_INPUT
SELECT field1, field2, field3, field4 FROM table_name
INTO OUTFILE '/path/file.csv' 
FIELDS ENCLOSED BY '"' 
TERMINATED BY ',' 
ESCAPED BY '"' 
LINES TERMINATED BY '\r\n';
QUERY_INPUT

Make sure user mysql can write to INTO OUTFILE ‘/path/file.csv’

Else you will get a “Permission denied” error.

Receive updates via email

Other Posts

Updated Posts