How to Export MySQL data to CSV

Posted on April 1, 2018 at 9:40 pm

MySQL command to export data in CSV format:

SELECT id,product,quantity
FROM orders
WHERE foo = 'bar'
INTO OUTFILE '/var/lib/mysql-csv/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

Use this command from bash shell to export MySQL to CSV:

nohup mysql -h localhost -u username -p'password' databasename -e "SELECT id,product,quantity FROM orders INTO OUTFILE '/var/lib/mysql-csv/orders.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '\'' LINES TERMINATED BY '\n';" > /dev/null 2>&1 &

The above command will run in background.

Receive updates via email

Other Posts

Updated Posts