MySQL Backup, Restore, Import, and Export Cheatsheet/Export query results to CSV-like output

Write rows to a server-side file.

Section: imports, exports, and mysql shell utilities

Export query results to CSV-like output

sql
sql
SELECT id, email
INTO OUTFILE '/var/lib/mysql-files/users.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM users;
Explanation

Requires file privileges and uses a path writable by the server.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing imports, exports, and mysql shell utilities
Load CSV data into a table
Import delimited files efficiently.
OpenIn sheetsqlsame section
Import delimited text with mysqlimport
Use the mysqlimport command-line utility.
OpenIn sheetbashsame section
Dump an instance with MySQL Shell
Use MySQL Shell utilities for scalable dumps.
OpenIn sheetbashsame section
Load a MySQL Shell dump
Restore a dump created by MySQL Shell utilities.
OpenIn sheetbashsame section
Dump one database
Export schema and data from one database.
Dump one table
Export a single table only.