1437

Is there an easy way to run a MySQL query from the Linux command line and output the results in CSV format?

Here's what I'm doing now:

mysql -u uid -ppwd -D dbname << EOQ | sed -e 's/        /,/g' | tee list.csv
select id, concat("\"",name,"\"") as name
from students
EOQ

It gets messy when there are a lot of columns that need to be surrounded by quotes, or if there are quotes in the results that need to be escaped.

10

40 Answers 40

2024

From Save MySQL query results into a text or CSV file:

SELECT order_id,product_name,qty
FROM orders
WHERE foo = 'bar'
INTO OUTFILE '/var/lib/mysql-files/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

Note: That syntax may need to be reordered to

SELECT order_id,product_name,qty
INTO OUTFILE '/var/lib/mysql-files/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM orders
WHERE foo = 'bar';

in more recent versions of MySQL.

Using this command, columns names will not be exported.

Also note that /var/lib/mysql-files/orders.csv will be on the server that is running MySQL. The user that the MySQL process is running under must have permissions to write to the directory chosen, or the command will fail.

If you want to write output to your local machine from a remote server (especially a hosted or virtualize machine such as Heroku or Amazon RDS), this solution is not suitable.

7
  • 10
    You may need to be root. "The file is created on the server host, so you must have the FILE privilege to use this syntax." - dev.mysql.com/doc/refman/8.0/en/select-into.html Commented Jun 23, 2021 at 21:10
  • 2
    Regarding the path where to save the file, this is related to secure-priv-file directive. You can also disable it in my.ini and restarting the db or change that directory's permissions if you don't want (or can't) to use root. See stackoverflow.com/questions/32737478/…
    – Chris
    Commented Jan 19, 2022 at 8:44
  • 1
    I got the error: "The MySQL server is running with the --secure-file-priv option so it cannot execute this statement"
    – Flimm
    Commented Feb 15, 2023 at 14:53
  • 2
    before running the query execute SHOW VARIABLES LIKE "secure_file_priv"; which will show (if any) which directory you can output to Commented Feb 22, 2023 at 16:27
  • 1
    For RDS if you're using Aurora mysql you can SELECT INTO OUTFILE S3 docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/…
    – Carl
    Commented Mar 23, 2023 at 22:02
575
mysql your_database --password=foo < my_requests.sql > out.tsv

This produces a tab-separated format. If you are certain that commas do not appear in any of the column data (and neither do tabs), you can use this pipe command to get a true CSV (thanks to user John Carter):

... .sql | sed 's/\t/,/g' > out.csv
1
  • 138
    the sed 'fix' does not compensate for commas that may appear in any of the selected data and will skew your columns outputted accordingly
    – Joey T
    Commented Dec 11, 2012 at 1:17
243

mysql --batch, -B

Print results using tab as the column separator, with each row on a new line. With this option, mysql does not use the history file. Batch mode results in non-tabular output format and escaping of special characters. Escaping may be disabled by using raw mode; see the description for the --raw option.

This will give you a tab-separated file. Since commas (or strings containing comma) are not escaped, it is not straightforward to change the delimiter to comma.

1
  • Just using Stan's answer here helped me get it in a .tsv file. I didn't need to use --batch, -B (MySQL 5.7). Also, I'd think a good number of use cases are fine with a .tsv if getting a .csv has extra steps.
    – joeljpa
    Commented Feb 19 at 11:32
207

Here's a fairly gnarly way of doing it[1]:

mysql --user=wibble --password mydatabasename -B -e "select * from vehicle_categories;" | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > vehicle_categories.csv

It works pretty well. Once again, though, a regular expression proves write-only.


Regex Explanation:

  • s/// means substitute what's between the first // with what's between the second //
  • the "g" at the end is a modifier that means "all instance, not just first"
  • ^ (in this context) means beginning of line
  • $ (in this context) means end of line

So, putting it all together:

s/'/\'/          Replace ' with \'
s/\t/\",\"/g     Replace all \t (tab) with ","
s/^/\"/          at the beginning of the line place a "
s/$/\"/          At the end of the line, place a "
s/\n//g          Replace all \n (newline) with nothing

[1] I found it somewhere and can't take any credit.

2
  • There is a typo in the command: "--password=wobble"
    – avernus
    Commented Jul 1, 2022 at 7:50
  • 2
    @AydinÖzcan --password just prompts the user for a password after the command is executed. wobble is the database name but I agree this isn't clear and have edited it for clarity. Thanks. Commented Jul 7, 2022 at 1:23
141

Pipe it through 'tr' (Unix/Cygwin only):

mysql <database> -e "<query here>" | tr '\t' ',' > data.csv

N.B.: This handles neither embedded commas, nor embedded tabs.

2
  • 7
    I'm surprised at how less-upvoted this solution is. Currently the top solution requires a privilege that many db users don't have (and for good reason; it's a security risk for administrators to give it out.) Your solution here works without any special privileges, and also could probably be improved to address the shortcomings of commas or tabs, possibly with a substitution in the query itself.
    – cazort
    Commented Jul 3, 2021 at 17:09
  • 2
    Agree with the comments here, this is precisely what's needed to avoid mysql privilege hell. Commented Aug 7, 2022 at 22:45
84

This saved me a couple of times. It is fast and it works!

--batch Print results using tab as the column separator, with each row on a new line.

--raw disables character escaping (\n, \t, \0, and \)

Example:

mysql -udemo_user -p -h127.0.0.1 --port=3306 \
   --default-character-set=utf8mb4 --database=demo_database \
   --batch --raw < /tmp/demo_sql_query.sql > /tmp/demo_csv_export.tsv

For completeness you could convert to CSV (but be careful because tabs could be inside field values - e.g., text fields)

tr '\t' ',' < file.tsv > file.csv

1
  • This is an answer to a completely different question. Commented Nov 21, 2023 at 17:36
46

The OUTFILE solution given by Paul Tomblin causes a file to be written on the MySQL server itself, so this will work only if you have FILE access, as well as login access or other means for retrieving the file from that box.

If you don't have such access, and tab-delimited output is a reasonable substitute for CSV (e.g., if your end goal is to import to Excel), then serbaut's solution (using mysql --batch and optionally --raw) is the way to go.

0
42

MySQL Workbench can export recordsets to CSV, and it seems to handle commas in fields very well. The CSV opens up in OpenOffice Calc fine.

6
  • 3
    Thanks a million David. After spending 3 hours getting the newlines to output properly for HTML content in the data, I used the MySQL Workbench and in 2 minutes I had my CSV file ready.
    – mr-euro
    Commented Jul 11, 2012 at 17:02
  • I've just found it can save as XML, too, which is great. I'm hoping to migrate from one application to another by using XSLT to transform this XML into a CSV file suitable for importing into the target application. Commented Aug 5, 2012 at 21:27
  • mysql workbench is best option for import export feature.
    – cijagani
    Commented Aug 5, 2015 at 3:20
  • Well,but each time the workbench limit the select records up to 1000 and when it comes to much more records it does not work that well,the same condition for the import it often blocked if I try to import a relatively large csv file into the mysql database by workbench. Commented Oct 29, 2016 at 6:42
  • 1
    I just successfully exported over half a million rows using mysql workbench so large files don't seem to be a problem. You just have to make sure you remove the select limit before running your query and you may also have to increase the following values in your my.ini file: max_allowed_packet = 500M, net_read_timeout = 600, net_write_timeout = 600
    – Vincent
    Commented Apr 22, 2018 at 15:47
38

All of the solutions here to date, except the MySQL Workbench one, are incorrect and quite possibly unsafe (i.e., security issues) for at least some possible content in the MySQL database.

MySQL Workbench (and similarly phpMyAdmin) provide a formally correct solution, but they are designed for downloading the output to a user's location. They're not so useful for things like automating data export.

It is not possible to generate reliably correct CSV content from the output of mysql -B -e 'SELECT ...' because that cannot encode carriage returns and white space in fields. The '-s' flag to mysql does do backslash escaping, and might lead to a correct solution. However, using a scripting language (one with decent internal data structures that is, not Bash), and libraries where the encoding issues have already been carefully worked out is far safer.

I thought about writing a script for this, but as soon as I thought about what I'd call it, it occurred to me to search for preexisting work by the same name. While I haven't gone over it thoroughly, mysql2csv looks promising. Depending on your application, the YAML approach to specifying the SQL commands might or might not appeal though. I'm also not thrilled with the requirement for a more recent version of Ruby than comes as standard with my Ubuntu 12.04 (Precise Pangolin) laptop or Debian 6.0 (Squeeze) servers. Yes, I know I could use RVM, but I'd rather not maintain that for such a simple purpose.

6
  • 1
    You are right, for complex strings in the table you have to use some decent library, not just bash. I think nodejs has a better solutions for this kind of actions. like this example
    – yeya
    Commented Jul 28, 2016 at 15:28
  • 1
    Hi, good answer, I would add a link to the python solution proposed below stackoverflow.com/a/35123787/1504300, which works well and is very simple. I tried to edit your post but the edit's been rejected
    – reallynice
    Commented Jun 2, 2017 at 12:44
  • Rob Miller's mysql2csv script insists on connecting to the database itself, either via network or socket, and cannot be used as a unix-style pipe. Maybe that's required but it really limits the use. Commented Sep 9, 2020 at 19:21
  • @chrisinmtown what would you want to pipe into it? Output of myslqldump maybe? As I've pointed out, the output of mysql -Bcan't be fixed.
    – mc0e
    Commented Oct 4, 2020 at 14:03
  • 1
    @mc0e I want to pipe in the output of mysqldump, read that as stdin. Commented Oct 5, 2020 at 13:20
35

Many of the answers on this page are weak, because they don't handle the general case of what can occur in CSV format. E.g., commas and quotes embedded in fields and other conditions that always come up eventually. We need a general solution that works for all valid CSV input data.

Here's a simple and strong solution in Python:

#!/usr/bin/env python

import csv
import sys

tab_in = csv.reader(sys.stdin, dialect=csv.excel_tab)
comma_out = csv.writer(sys.stdout, dialect=csv.excel)

for row in tab_in:
    comma_out.writerow(row)

Name that file tab2csv, put it on your path, give it execute permissions, then use it like this:

mysql OTHER_OPTIONS --batch --execute='select * from whatever;' | tab2csv > outfile.csv

The Python CSV-handling functions cover corner cases for CSV input format(s).

This could be improved to handle very large files via a streaming approach.

5
  • 11
    An even more dependable solution would be to actually connect to the database with Python, then you should have an easier time doing what you need to do to deal with larger datasets (chunking results, streaming, etc). Commented Feb 4, 2016 at 15:52
  • @JoshRumbut, really late, but I made stackoverflow.com/a/41840534/2958070 to complement your comment
    – Ben
    Commented Aug 30, 2019 at 16:28
  • Please see stackoverflow.com/questions/356578/… for an extended version of this script with an input dialect that handles embedded comma and double-quote characters! Commented Sep 10, 2020 at 13:51
  • 1
    What is the gist of it? Using a library? A built-in library? Can you link to documentation, etc.? (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.). Commented Aug 6, 2021 at 11:34
  • 1
    What would be required for a streaming approach? Can you elaborate a little bit in your answer? (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.) Commented Aug 12, 2021 at 9:30
33

Use:

mysql your_database -p < my_requests.sql | awk '{print $1","$2}' > out.csv
2
  • 3
    I really like this one. It is much cleaner, and I like the use of awk. However, I would have probably gone with this: mysql -uUser -pPassword your_database < my_requests.sql | awk 'BEGIN{OFS="=";} {print $1,$2}' > out.csv
    – Josh
    Commented Apr 11, 2012 at 0:00
  • 11
    This fails for field values with spaces. Commented Apr 10, 2015 at 6:17
31

From your command line, you can do this:

mysql -h *hostname* -P *port number* --database=*database_name* -u *username* -p -e *your SQL query* | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > *output_file_name.csv*

Credits: Exporting table from Amazon RDS into a CSV file

0
22

I encountered the same problem and Paul's Answer wasn't an option since it was Amazon RDS. Replacing the tab with the commas did not work as the data had embedded commas and tabs. I found that mycli, which is a drop-in alternative for the mysql-client, supports CSV output out of the box with the --csv flag:

mycli db_name --csv -e "select * from flowers" > flowers.csv
5
  • Works like a charm, you can install mycli in macOS via: brew update && brew install mycli. I'm never using the stock mysql client again, mycli is so legit!
    – bithavoc
    Commented Jul 24, 2020 at 2:40
  • 1
    Mycli and it's cousin pgcli are fantastic. I came here to add this tip mysql because I wasn't getting anywhere with the other solutions. Cleanly outputting to a local .csv is surprisingly difficult with mysql.
    – iturgeon
    Commented Sep 4, 2020 at 19:17
  • Awesome, it's so sad that this answer got very few "likes". It's the only solution that fits my needs. All other didn't work perfectly for me but this. I had line breaks in columns - and that led to new lines in Excel program when opening the file.
    – Skiff
    Commented Nov 20, 2020 at 23:57
  • This answer won't work if you're stuck using version 1.8.1 (maybe because you're using an older OS that still has Python 2.x), as '--csv' wasn't available in that version. Commented Dec 14, 2020 at 9:35
  • 1
    I'm puzzled as to why other solutions keep getting upvoted. Thank you for sharing info about mycli, it's now my default client.
    – RobMac
    Commented Apr 29, 2023 at 20:17
21

This answer uses Python and a popular third party library, PyMySQL. I'm adding it because Python's csv library is powerful enough to correctly handle many different flavors of .csv and no other answers are using Python code to interact with the database.

import contextlib
import csv
import datetime
import os

# https://github.com/PyMySQL/PyMySQL
import pymysql

SQL_QUERY = """
SELECT * FROM my_table WHERE my_attribute = 'my_attribute';
"""

# embedding passwords in code gets nasty when you use version control
# the environment is not much better, but this is an example
# https://stackoverflow.com/questions/12461484
SQL_USER = os.environ['SQL_USER']
SQL_PASS = os.environ['SQL_PASS']

connection = pymysql.connect(host='localhost',
                             user=SQL_USER,
                             password=SQL_PASS,
                             db='dbname')

with contextlib.closing(connection):
    with connection.cursor() as cursor:
        cursor.execute(SQL_QUERY)
        # Hope you have enough memory :)
        results = cursor.fetchall()

output_file = 'my_query-{}.csv'.format(datetime.datetime.today().strftime('%Y-%m-%d'))
with open(output_file, 'w', newline='') as csvfile:
    # http://stackoverflow.com/a/17725590/2958070 about lineterminator
    csv_writer = csv.writer(csvfile, lineterminator='\n')
    csv_writer.writerows(results)
4
  • An answer using python was already provided. stackoverflow.com/a/35123787/5470883 Commented Jan 25, 2017 at 9:39
  • 5
    @AlexanderBaltasar, right, and it looks useful, but it's not using Python code to interact with the database. See the comment on that on that question.
    – Ben
    Commented Jan 26, 2017 at 2:29
  • Re "no other answers are using Python code to interact with the database.": No, but Chris Johnson's answer also uses the 'csv' library. Commented Aug 6, 2021 at 11:40
  • @PeterMortensen, the comments on that answer motivated me to write this answer :)
    – Ben
    Commented Aug 6, 2021 at 17:36
15

Also, if you're performing the query on the Bash command line, I believe the tr command can be used to substitute the default tabs to arbitrary delimiters.

$ echo "SELECT * FROM Table123" | mysql Database456 | tr "\t" ,
2
  • This is a nice way to go if you're having trouble with file permissions, but it won't quote your fields, so other tools may misinterpret the CSV if your data includes commas or quotation marks. Commented Apr 8, 2021 at 15:29
  • Requires -s (silent) option to produce tabbed output
    – JamesP
    Commented Jul 19, 2021 at 15:03
14

This is simple, and it works on anything without needing batch mode or output files:

select concat_ws(',',
    concat('"', replace(field1, '"', '""'), '"'),
    concat('"', replace(field2, '"', '""'), '"'),
    concat('"', replace(field3, '"', '""'), '"'))

from your_table where etc;

Explanation:

  1. Replace " with "" in each field --> replace(field1, '"', '""')
  2. Surround each result in quotation marks --> concat('"', result1, '"')
  3. Place a comma between each quoted result --> concat_ws(',', quoted1, quoted2, ...)

That's it!

1
  • 4
    Note that NULL values will be skipped by concat_ws(), resulting in a column mismatch. To avoid this, simply use an empty string instead: IFNULL(field, '') (or whatever else you use to represent NULL)
    – Marco Roy
    Commented May 21, 2019 at 23:11
11

You can have a MySQL table that uses the CSV engine.

Then you will have a file on your hard disk that will always be in a CSV format which you could just copy without processing it.

1
  • 3
    What are the limits of this in terms of file size / writes / reads / etc?
    – Zach Smith
    Commented Oct 10, 2018 at 8:21
10

To expand on previous answers, the following one-liner exports a single table as a tab-separated file. It's suitable for automation, exporting the database every day or so.

mysql -B -D mydatabase -e 'select * from mytable'

Conveniently, we can use the same technique to list out MySQL's tables, and to describe the fields on a single table:

mysql -B -D mydatabase -e 'show tables'

mysql -B -D mydatabase -e 'desc users'

Field   Type    Null    Key Default Extra
id  int(11) NO  PRI NULL    auto_increment
email   varchar(128)    NO  UNI NULL    
lastName    varchar(100)    YES     NULL    
title   varchar(128)    YES UNI NULL    
userName    varchar(128)    YES UNI NULL    
firstName   varchar(100)    YES     NULL    
3
  • 4
    To convert to CSV: mysql -B -D mydatabase -e 'select * from mytable' | sed -e 's/\t/,/g'
    – DSimon
    Commented Jan 12, 2015 at 21:15
  • 8
    Using sed -e 's/\t/,/g' is only safe if you are sure that your data doesn't contain any commas or tabs.
    – awatts
    Commented Oct 20, 2015 at 16:23
  • @awatts I suggest: sed -e 's/"/\\"/g' -e 's/\t/","/g' -e 's/^/"/' -e 's/$/"/'
    – Flimm
    Commented Feb 15, 2023 at 15:07
9

Building on user7610, here is the best way to do it. With mysql outfile there were 60 mins of file ownership and overwriting problems.

It's not cool, but it worked in 5 mins.

php csvdump.php localhost root password database tablename > whatever-you-like.csv

<?php

$server = $argv[1];
$user = $argv[2];
$password = $argv[3];
$db = $argv[4];
$table = $argv[5];

mysql_connect($server, $user, $password) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

// fetch the data
$rows = mysql_query('SELECT * FROM ' . $table);
$rows || die(mysql_error());


// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// output the column headings

$fields = [];
for($i = 0; $i < mysql_num_fields($rows); $i++) {
    $field_info = mysql_fetch_field($rows, $i);
    $fields[] = $field_info->name;
}
fputcsv($output, $fields);

// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);

?>
1
  • 1
    Nice, clean, and works quickly - this is a great solution if you can run PHP in that environment!
    – John Fiala
    Commented Apr 5, 2018 at 22:01
8

Here's what I do:

echo $QUERY | \
  mysql -B  $MYSQL_OPTS | \
  perl -F"\t" -lane 'print join ",", map {s/"/""/g; /^[\d.]+$/ ? $_ : qq("$_")} @F ' | \
  mail -s 'report' person@address

The Perl script (snipped from elsewhere) does a nice job of converting the tab spaced fields to CSV.

2
  • 2
    This is great. Slight improvement might be to quote everything except numbers perl -F"\t" -lane 'print join ",", map {s/"/""/g; /^\d+(?:\.\d+)?$/ ? $_ : qq("$_")} @F ' -- yours would not quote 1.2.3 Commented Mar 15, 2017 at 16:00
  • 1
    This should be the accepted solution IMHO, with @artfulrobot's improvement of course. Commented Sep 25, 2020 at 14:55
7

Not exactly as a CSV format, but the tee command from the MySQL client can be used to save the output into a local file:

tee foobar.txt
SELECT foo FROM bar;

You can disable it using notee.

The problem with SELECT … INTO OUTFILE …; is that it requires permission to write files at the server.

2
  • If .csv extension is used instead of .txt, are there any formatting issues to be aware of? Commented May 8, 2018 at 21:53
  • @myidealab Formatting issues arise from commas etc. not being escaped. CSV is a plain text format so there's no formatting issue just from swapping out the extension. Commented Jun 20, 2019 at 17:24
7

In my case from table_name ..... before INTO OUTFILE ..... gives an error:

Unexpected ordering of clauses. (near "FROM" at position 10)

What works for me:

SELECT *
INTO OUTFILE '/Volumes/Development/sql/sql/enabled_contacts.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM table_name
WHERE column_name = 'value'
1
  • This is what I was looking for, but be aware that many modern MySQL servers are set up running --secure-file-priv which could prevent this from working
    – SteJ
    Commented Jul 15, 2022 at 11:56
5

What worked for me:

SELECT *
FROM students
WHERE foo = 'bar'
LIMIT 0,1200000
INTO OUTFILE './students-1200000.csv'
FIELDS TERMINATED BY ',' ESCAPED BY '"'
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n';

None of the solutions on this thread worked for my particular case. I had pretty JSON data inside one of the columns, which would get messed up in my CSV output. For those with a similar problem, try lines terminated by \r\n instead.

Also another problem for those trying to open the CSV with Microsoft Excel, keep in mind there is a limit of 32,767 characters that a single cell can hold, above that it overflows to the rows below. To identify which records in a column have the issue, use the query below. You can then truncate those records or handle them as you'd like.

SELECT id,name,CHAR_LENGTH(json_student_description) AS 'character length'
FROM students
WHERE CHAR_LENGTH(json_student_description)>32767;
1
  • Re "...pretty JSON data...": Pretty in what way? Commented Aug 6, 2021 at 11:57
4

Using the solution posted by Tim Harding, I created this Bash script to facilitate the process (root password is requested, but you can modify the script easily to ask for any other user):

#!/bin/bash

if [ "$1" == "" ];then
    echo "Usage: $0 DATABASE TABLE [MYSQL EXTRA COMMANDS]"
    exit
fi

DBNAME=$1
TABLE=$2
FNAME=$1.$2.csv
MCOMM=$3

echo "MySQL password: "
stty -echo
read PASS
stty echo

mysql -uroot -p$PASS $MCOMM $DBNAME -B -e "SELECT * FROM $TABLE;" | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > $FNAME

It will create a file named: database.table.csv

1
  • what about embedded double-quote characters? They need to be escaped also but I don't see a pattern in the sed incantation for them. Commented Sep 25, 2020 at 14:50
3

If you have PHP set up on the server, you can use mysql2csv to export an (actually valid) CSV file for an arbitrary MySQL query. See my answer at MySQL - SELECT * INTO OUTFILE LOCAL ? for a little more context/info.

I tried to maintain the option names from mysql so it should be sufficient to provide the --file and --query options:

./mysql2csv --file="/tmp/result.csv" --query='SELECT 1 as foo, 2 as bar;' --user="username" --password="password"

"Install" mysql2csv via

wget https://gist.githubusercontent.com/paslandau/37bf787eab1b84fc7ae679d1823cf401/raw/29a48bb0a43f6750858e1ddec054d3552f3cbc45/mysql2csv -O mysql2csv -q && (sha256sum mysql2csv | cmp <(echo "b109535b29733bd596ecc8608e008732e617e97906f119c66dd7cf6ab2865a65  mysql2csv") || (echo "ERROR comparing hash, Found:" ;sha256sum mysql2csv) ) && chmod +x mysql2csv

(Download content of the gist, check checksum and make it executable.)

3

The following produces tab-delimited and valid CSV output. Unlike most of the other answers, this technique correctly handles escaping of tabs, commas, quotes, and new lines without any stream filter like sed, AWK, or tr.

The example shows how to pipe a remote MySQL table directly into a local SQLite database using streams. This works without FILE permission or SELECT INTO OUTFILE permission. I have added new lines for readability.

mysql -B -C --raw -u 'username' --password='password' --host='hostname' 'databasename'
-e 'SELECT
    CONCAT('\''"'\'',REPLACE(`id`,'\''"'\'', '\''""'\''),'\''"'\'') AS '\''id'\'',
    CONCAT('\''"'\'',REPLACE(`value`,'\''"'\'', '\''""'\''),'\''"'\'') AS '\''value'\''
    FROM sampledata'
2>/dev/null | sqlite3 -csv -separator $'\t' mydb.db '.import /dev/stdin mycsvtable'

The 2>/dev/null is needed to suppress the warning about the password on the command line.

If your data has NULLs, you can use the IFNULL() function in the query.

3

A simple solution in Python that writes a standard-format CSV file with headers and writes data as a stream (low memory use):

import csv

def export_table(connection, table_name, output_filename):
    cursor = connection.cursor()
    cursor.execute("SELECT * FROM " + table_name)

    # thanks to https://gist.github.com/madan712/f27ac3b703a541abbcd63871a4a56636 for this hint
    header = [descriptor[0] for descriptor in cursor.description]

    with open(output_filename, 'w') as csvfile:
        csv_writer = csv.writer(csvfile, dialect='excel')
        csv_writer.writerow(header)
        for row in cursor:
            csv_writer.writerow(row)

You could use it like:

import mysql.connector as mysql
# (or https://github.com/PyMySQL/PyMySQL should work but I haven't tested it)

db = mysql.connect(
    host="localhost",
    user="USERNAME",
    db="DATABASE_NAME",
    port=9999)

for table_name in ['table1', 'table2']:
    export_table(db, table_name, table_name + '.csv')

db.close()

For simplicity, this intentionally doesn't include some fancier stuff from another answer like using an environment variable for credentials, contextlib, etc. There is a subtlety mentioned there about line endings that I haven't evaluated.

2

Tiny Bash script for doing simple query to CSV dumps, inspired by Tim Harding's answer.

#!/bin/bash

# $1 = query to execute
# $2 = outfile
# $3 = mysql database name
# $4 = mysql username

if [ -z "$1" ]; then
    echo "Query not given"
    exit 1
fi

if [ -z "$2" ]; then
    echo "Outfile not given"
    exit 1
fi

MYSQL_DB=""
MYSQL_USER="root"

if [ ! -z "$3" ]; then
    MYSQL_DB=$3
fi

if [ ! -z "$4" ]; then
    MYSQL_USER=$4
fi

if [ -z "$MYSQL_DB" ]; then
    echo "Database name not given"
    exit 1
fi

if [ -z "$MYSQL_USER" ]; then
    echo "Database user not given"
    exit 1
fi

mysql -u $MYSQL_USER -p -D $MYSQL_DB -B -s -e "$1" | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > $2
echo "Written to $2"
1
  • 1
    This uses the sed incantation that ignores embedded double-quote characters. Suggest using the perl solution instead. Commented Sep 25, 2020 at 14:52
2

If you are getting an error of secure-file-priv then, also after shifting your destination file location inside the C:\ProgramData\MySQL\MySQL Server 8.0\Uploads and also after then the query -

SELECT * FROM attendance INTO OUTFILE 'C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\FileName.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

is not working, you have to just change \(backsplash) from the query to / (forwardsplash)

And that works!!

Example:

SELECT * FROM attendance INTO OUTFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/FileName.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

Each time when you run the successful query, it will generate the new CSV file each time! Cool, right?

1
  • On Windows, presumably? What was it tested on, incl. versions (of Windows, MySQL, etc.)? Commented Aug 6, 2021 at 12:06
2

This is dirty and ugly. It is only suitable for the particular situation where all you have is a PHP-*-admin and the server is running with the --secure-file-priv option, so then you cannot use the INTO OUTFILE '/path/to/export.csv' clause in your query.

What you can do is parse lines of CSV with ... wait for it!, CONCAT, then copy the results and paste them into a file.

Here's an example, where I needed SQL format (it'd be trivial to adapt it for CSV):

SELECT CONCAT(
"('",
`username`, "', '",
`password`, "', '",
`first_name`, "', '",
`last_name`, "', '",
`gender`, "'),"
) AS `row` 
FROM `users`
WHERE `role` != 'Not set'
AND `user_status` = 'Active'
ORDER BY `role`, `gender`, `user_id`
LIMIT 200

That gives nice, ready for import output similar to this:

('jane', '3d7ff...', 'Jane', 'Doe', 'Female'),  
('john', 'd2a33...', 'John', 'Doe', 'Male'),
...

Not the answer you're looking for? Browse other questions tagged or ask your own question.