How to export a MySQL table to CSV: 4 ways, with or without server access

By Hammad Rasool · November 7, 2026

Quick answer: if you can reach the database server's disk and hold the FILE privilege, use SELECT ... INTO OUTFILE, but not with the options most tutorials show, because they can produce a broken file. If you can only connect as a client, which is common on managed and shared hosting, run mysqldump on your own machine and convert the dump. mysql --batch is the quickest option when tab-separated output is good enough.

How this was tested: the SQL and command-line examples were run on MySQL 8.4.9 on Windows on 7 November 2026, against a local server and a four-row table containing commas, quotes, NULLs, a line break, a tab and a backslash. The outputs below are copied from those runs. The tests connected to 127.0.0.1 and wrote to a Windows folder, so the host name and the Linux path in the examples are placeholders to replace. The Workbench wizard in method 4 was not run.

Which method should you use?

MethodFile ends up onNeedsBest for
INTO OUTFILEDatabase serverFILE privilege, allowed directoryLarge exports when you control the server
mysql --batchYour machineA normal loginQuick query results, tab-separated
mysqldump + converterYour machineSELECT, plus LOCK TABLES or --single-transactionReal CSV without server file access
Workbench wizardYour machineWorkbench and a loginOne-off exports without a terminal

Method 1: SELECT INTO OUTFILE

This statement writes the result straight to a file. MySQL's default output is tab-separated with no quoting, so you set the CSV options yourself. The catch is that ESCAPED BY decides whether the file is valid CSV, and no setting handles everything on its own. This is what each one wrote for the same table:

ESCAPED BYValue with quotesNULLLine break in a value
Not set (backslash)"Sana \"Sunny\" Mir": not standard CSV\NStray backslash added before the break
'"'"Sana ""Sunny"" Mir": correct"N: breaks the rowStray quote added, which breaks the row
'' (empty)"Sana "Sunny" Mir": breaks the rowThe word NULLCorrect

ESCAPED BY '"' is the trap. It looks right on a sample with commas and quotes, but the first NULL writes "N, and a CSV parser then swallows the following rows into one field. In the test, a four-row file came back as three rows with the wrong values and no error.

The version that held up turns escaping off and handles the two problems in the query: REPLACE doubles the quotes and IFNULL removes the NULLs.

SELECT id,
  REPLACE(IFNULL(name, ''), '"', '""'),
  REPLACE(IFNULL(city, ''), '"', '""'),
  REPLACE(IFNULL(note, ''), '"', '""')
FROM customers
INTO OUTFILE '/var/lib/mysql-files/customers.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY ''
LINES TERMINATED BY '\n';

Output, which parsed back into the correct four rows and four columns:

"1","Khan, Sara","Multan",""
"2","Ali 'AR' Raza","Lahore","line one
line two"
"3","Sana ""Sunny"" Mir","","plain"
"4","Tab	here","Karachi","back\slash"

Wrap every nullable column this way. A NULL you miss still comes out as the word NULL. Also note that NULL and an empty string look the same in this output, so if that difference matters, use a marker value in the IFNULL instead.

Three rules catch most people out:

  • The file is created on the server host, not on your computer, and it is owned by the account running MySQL.
  • You need the FILE privilege.
  • The target file must not already exist. MySQL will not overwrite it.

Fix: error 1290 and secure_file_priv

Error 1290 reads "The MySQL server is running with the --secure-file-priv option so it cannot execute this statement". Either the path is outside the directory the server allows, or file export is switched off. Check the setting:

SHOW VARIABLES LIKE 'secure_file_priv';
  • A directory path: write inside that directory. DEB and RPM installs commonly use /var/lib/mysql-files/. On Windows, the path in your statement can use forward slashes, as in 'D:/mysql-files/customers.csv'.
  • Empty: no directory restriction. The server accepted a path anywhere and logged an "Insecure configuration for --secure-file-priv" warning at startup.
  • NULL: file export is switched off, and every INTO OUTFILE fails with error 1290. Use one of the client-side methods below.

secure_file_priv cannot be changed on a running server: SET GLOBAL fails with error 1238 ("Variable 'secure_file_priv' is a read only variable"). It is a startup option, so changing it means editing the server configuration and restarting.

Fix: error 1227 and error 1086

Error 1227 ("Access denied; you need (at least one of) the FILE privilege(s) for this operation") means your account lacks FILE. Ask the administrator for it, or skip to method 3. Error 1086 ("File '...' already exists") means you need a new filename or must remove the old file first.

Add a header row

INTO OUTFILE writes only the selected rows, with no column names. The usual workaround is a UNION ALL with a row of labels:

SELECT 'id', 'name', 'city', 'note'
UNION ALL
SELECT id,
  REPLACE(IFNULL(name, ''), '"', '""'),
  REPLACE(IFNULL(city, ''), '"', '""'),
  REPLACE(IFNULL(note, ''), '"', '""')
FROM customers
INTO OUTFILE '/var/lib/mysql-files/customers.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY ''
LINES TERMINATED BY '\n';

The label row came first in the test, but without an outer sort MySQL does not promise that order, so check the top of the file after a real export.

Method 2: mysql --batch

The mysql client runs on your machine, so the output lands there and no FILE privilege is involved:

mysql --batch -h db.example.com -u report_user -p shop -e "SELECT id, name, city, note FROM customers" > customers.tsv

Output from the test table:

id	name	city	note
1	Khan, Sara	Multan	NULL
2	Ali 'AR' Raza	Lahore	line one\nline two
3	Sana "Sunny" Mir	NULL	plain
4	Tab\there	Karachi	back\\slash

Column names are included unless you add --skip-column-names. Special characters are escaped, so a line break appears as the two characters \n, a tab as \t and a backslash as \\. NULL appears as the word NULL, the same as a real value spelled that way.

This is tab-separated, not CSV. Replacing every tab with a comma looks like it works until a value contains a comma, and then columns shift without any error. If you need true CSV and your data has commas or quotes, use method 3 or 4. In Windows PowerShell 5.1, the > in this command also saves the file as UTF-16, as covered next.

Method 3: mysqldump, then convert

This is the method for managed databases where you have no access to the server's disk. Dump one table from your own machine:

mysqldump -h db.example.com -u report_user -p --no-tablespaces shop customers --result-file=customers.sql

Two of those options prevent problems seen in testing:

  • --result-file keeps the encoding intact. In Windows PowerShell 5.1, a dump redirected with > was saved as UTF-16 and failed to load back with "ASCII '\0' appeared in the statement". The same dump written with --result-file restored all four rows.
  • --no-tablespaces stops "Access denied; you need (at least one of) the PROCESS privilege(s) for this operation" on accounts without PROCESS. In the test, the dump still finished without the option, but the error is alarming in a log or script.

If your account has only SELECT, the dump stops with "Got error: 1044: Access denied ... when doing LOCK TABLES". Adding --single-transaction, which suits InnoDB tables, worked with the same account:

mysqldump -h db.example.com -u report_user -p --no-tablespaces --single-transaction shop customers --result-file=customers.sql

Then open customers.sql in the SQL to CSV converter, which runs in your browser, so the data is not uploaded. This is the INSERT line from the test dump:

INSERT INTO `customers` VALUES (1,'Khan, Sara','Multan',NULL),(2,'Ali \'AR\' Raza','Lahore','line one\nline two'),(3,'Sana \"Sunny\" Mir',NULL,'plain'),(4,'Tab	here','Karachi','back\\slash');

And the CSV the converter produced from that dump, with column names taken from its CREATE TABLE statement:

id,name,city,note
1,"Khan, Sara",Multan,
2,Ali 'AR' Raza,Lahore,"line one
line two"
3,"Sana ""Sunny"" Mir",,plain
4,Tab	here,Karachi,back\slash

The comma, the quotes, the line break, the tab, the backslash and the NULLs all came through correctly, with none of the escaping choices method 1 needs. As in method 1, NULL becomes an empty field. For more on what can go wrong with dumps, see how to convert a SQL dump file to CSV.

Avoid mysqldump --tab for this. It looks convenient, but the server writes the data file, so it has the same limits as INTO OUTFILE. An account without FILE gets error 1227.

Method 4: MySQL Workbench

According to the Workbench manual, you right-click the table in the object browser and choose Table Data Export Wizard, which exports to a CSV or JSON file on your computer. It suits occasional exports, but a command-line method is easier to repeat or schedule. This is the one method here that was not run for this guide, so check its output on rows with quotes and NULLs before relying on it.

FAQ

Why does my CSV contain \N or "N?

Both are how INTO OUTFILE writes NULL: \N with the default escape character and "N with ESCAPED BY '"'. Wrap nullable columns in IFNULL(column, '') and set ESCAPED BY '', as in method 1.

Why can't I find the file after INTO OUTFILE?

It is on the database server, not your computer. If MySQL runs in a container or on another host, look there.

Can I export a table to CSV without the FILE privilege?

Yes. Methods 2, 3 and 4 run from the client side and do not use INTO OUTFILE.

How do I export only some rows?

Add a WHERE clause to the SELECT in methods 1 and 2. With mysqldump, use --where="id=1".

How do I load the CSV into another database?

See importing a CSV into MySQL, PostgreSQL, SQL Server and SQLite, or generate INSERT statements with the CSV to SQL converter.

Sources