Questions tagged [into-outfile]

MySQL's `SELECT ... INTO OUTFILE` command lets you quickly dump the results of a MySQL query into a file on the MySQL server. It generally should not be used for production use.

MySQL's SELECT ... INTO OUTFILE command lets you quickly dump the results of a MySQL query into a file on the MySQL server. It generally should not be used for production use.

From the MySQL documentation

The SELECT ... INTO OUTFILE 'file_name' form of SELECT writes the selected rows to a file. The file is created on the server host, so you must have the FILE privilege to use this syntax. file_name cannot be an existing file, which among other things prevents files such as /etc/passwd and database tables from being destroyed.

One must be able have access to the MySQL server machine, and permissions to operate on files as the mysql user in order to access and manage the dump files. These restrictions mean the SELECT ... INTO OUTFILE command is generally suitable only for testing and debugging.

If you want a dump of a table from a MySQL database on your client machine, you can call the mysqldump binary on the client. If you want a CSV of the results of a MySQL query, you generally need to roll your own code to create that file in your client application, though there are libraries that can help with the CSV format.

139 questions
145
votes
23 answers

Include headers when using SELECT INTO OUTFILE?

Is it possible to include the headers somehow when using the MySQL INTO OUTFILE?
Brett
  • 19,449
  • 54
  • 157
  • 290
116
votes
13 answers

How can I get around MySQL Errcode 13 with SELECT INTO OUTFILE?

I am trying to dump the contents of a table to a csv file using a MySQL SELECT INTO OUTFILE statement. If I do: SELECT column1, column2 INTO OUTFILE 'outfile.csv' FIELDS TERMINATED BY ',' FROM table_name; outfile.csv will be created on the server…
Ryan Olson
  • 2,766
  • 4
  • 29
  • 36
94
votes
6 answers

MYSQL into outfile "access denied" - but my user has "ALL" access.. and the folder is CHMOD 777

Any ideas? SELECT * INTO OUTFILE '/home/myacnt/docs/mysqlCSVtest.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '*' LINES TERMINATED BY '\n' FROM tbl_property WHERE managerGroupID = {$managerGroupID} Error: Access denied for user…
Shackrock
  • 4,601
  • 10
  • 48
  • 74
57
votes
7 answers

MySQL INTO OUTFILE override existing file?

I've written a big sql script that creates a CSV file. I want to call a cronjob every night to create a fresh CSV file and have it available on the website. Say for example I'm store my file in '/home/sites/example.com/www/files/backup.csv' and my…
Derek Organ
  • 8,323
  • 17
  • 56
  • 75
50
votes
6 answers

MySQL export into outfile : CSV escaping chars

I've a database table of timesheets with some common feilds. id, client_id, project_id, task_id, description, time, date There are more but thats the gist of it. I have an export running on that table to a CSV file overnight to give the user a…
Derek Organ
  • 8,323
  • 17
  • 56
  • 75
48
votes
7 answers

MySQL - SELECT * INTO OUTFILE LOCAL ?

MySQL is awesome! I am currently involved in a major server migration and previously, our small database used to be hosted on the same server as the client. So we used to do this : SELECT * INTO OUTFILE .... LOAD DATA INFILE .... Now, we moved the…
ThinkCode
  • 7,841
  • 21
  • 73
  • 92
30
votes
3 answers

Output MySQL source results to log file

I am trying to execute foo.sql using the source command in MySQL. When I type the command, the file is sourced accordingly: mysql> source ~/foo.sql Now, there are a lot of statements being executed in this file and I would like to review the…
Isaac
  • 303
  • 1
  • 3
  • 4
20
votes
3 answers

MySQL query to print output as CSV to standard output

I want to do the following mysql -uuser -ppass -h remote.host.tld database < script.sql where script.sql contains the following SELECT * FROM webrecord_wr25mfz_20101011_175524 FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' I…
Blaine Lafreniere
  • 3,451
  • 6
  • 33
  • 55
15
votes
8 answers

Extra backslash \ when SELECT ... INTO OUTFILE ... in MySQL

So I'm trying to export a MySQL table into CSV. I'm using this query: SELECT * FROM business WHERE id > 0 AND id <= 20000 INTO OUTFILE "business.csv" FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY "\n"; That output…
datasn.io
  • 12,564
  • 28
  • 113
  • 154
15
votes
2 answers

How do I handle NULL values in a mysql SELECT ... OUTFILE statement in conjunction with FIELDS ESCAPED BY? NULL values are currently being truncated

I'm encountering some difficulties using MySQL's SELECT ... OUTFILE on result sets that include both null values and columns that require double quote escaping (ie, columns that contain '"' characters). This is the outfile syntax I am using: INTO…
Kevin Jhangiani
  • 1,597
  • 2
  • 13
  • 23
15
votes
3 answers

MySQL select into outfile /tmp no output

I cannot get the following code to generate any output. The MySQL user has "all" grant level, /tmp is writable, the query returns a results set. mysql> SELECT field FROM test_table WHERE condition='test' -> INTO OUTFILE '/tmp/test.csv' ->…
Andy
  • 17,423
  • 9
  • 52
  • 69
14
votes
2 answers

Query output to a file gives access denied error

I am trying to capture the output of a SQL query in MySQL, to a text file using the following query. select count(predicate),subject from TableA group by subject into outfile '~/XYZ/output.txt'; I get the following error. ERROR 1045 (28000):…
Arnkrishn
  • 29,828
  • 40
  • 114
  • 128
13
votes
1 answer

mysql dump to localhost outfile from a remote database

I'm stuck. I basically want to create a LOCAL data file (csv file) from a remote database using the OUTFILE command. I am basically, pulling data.. and want to create it on my local file server vs. creating the outfile on the remote server. I'm…
jdamae
  • 3,839
  • 16
  • 58
  • 78
12
votes
4 answers

How can I have MySQL write outfiles as a different user?

I'm working with a MySQL query that writes into an outfile. I run this query once every day or two and so I want to be able to remove the outfile without having to resort to su or sudo. The only way I can think of making that happen is to have the…
David Locke
  • 17,926
  • 9
  • 33
  • 53
11
votes
4 answers

SELECT INTO OUTFILE can't write to file

I'm trying to do a SELECT INTO OUTFILE and I'm getting the following error: General error: 1 Can't create/write to file '/home/jason/projects/mcif/web/downloads/dump.csv' (Errcode: 13). Failing Query: " SELECT name INTO OUTFILE…
Jason Swett
  • 43,526
  • 67
  • 220
  • 351
1
2 3
9 10