This is an English translation of a Japanese blog. Some content may not be fully translated.
MySQL

Extracting Data from a MySQL Table to a File

There are two main methods for file output.

Method 1: Output via Redirect

echo 'SELECT * FROM t1 ' | mysql -u myuser -p<password> mydb > /tmp/t1.dmp

Method 2: Using SELECT INTO OUTFILE

SELECT * FROM t1 INTO OUTFILE '/var/lib/mysql-files/t1.dmp' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';

Specify a file write destination that is permitted by secure_file_priv. If you specify a directory other than the above, the following error will occur:

ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
mysql> SELECT @@secure_file_priv;
+-----------------------+
| @@secure_file_priv    |
+-----------------------+
| /var/lib/mysql-files/ |
+-----------------------+
1 row in set (0.00 sec)

Reference

MySQL :: MySQL 5.6 Reference Manual :: 13.2.9.1 SELECT … INTO Syntax https://dev.mysql.com/doc/refman/5.6/ja/select-into.html

SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;
Suggest an edit on GitHub