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 database to a different server and SELECT * INTO OUTFILE ....
no longer works, understandable - security reasons I believe.
But, interestingly LOAD DATA INFILE ....
can be changed to LOAD DATA LOCAL INFILE ....
and bam, it works.
I am not complaining nor am I expressing disgust towards MySQL. The alternative to that added 2 lines of extra code and a system call form a .sql script. All I wanted to know is why LOAD DATA LOCAL INFILE
works and why is there no such thing as SELECT INTO OUTFILE LOCAL
?
I did my homework, couldn't find a direct answer to my questions above. I couldn't find a feature request @ MySQL either. If someone can clear that up, that had be awesome!
Is MariaDB capable of handling this problem?
The path you give to
LOAD DATA INFILE
is for the filesystem on the machine where the server is running, not the machine you connect from.LOAD DATA LOCAL INFILE
is for the client's machine, but it requires that the the server was started with the right settings, otherwise it's not allowed. You can read all about it here: http://dev.mysql.com/doc/refman/5.0/en/load-data-local.htmlAs for
SELECT INTO OUTFILE
I'm not sure why there is not a local version, besides it probably being tricky to do over the connection. You can get the same functionality through themysqldump
tool, but not through sending SQL to the server.Re: SELECT * INTO OUTFILE
Check if MySQL has permissions to write a file to the OUTFILE directory on the server.
Using mysql CLI with -e option as Waverly360 suggests is a good one, but that might go out of memory and get killed on large results. (Havent find the reason behind it). If that is the case, and you need all records, my solution is: mysqldump + mysqldump2csv:
From the manual:
The SELECT ... INTO OUTFILE
statement is intended primarily to let you very quickly dump a table to a text file on the server machine. If you want to create the resulting file on some client host other than the server host, you cannot useSELECT ... INTO OUTFILE
. In that case, you should instead use a command such asmysql -e "SELECT ..." > file_name
to generate the file on the client host."http://dev.mysql.com/doc/refman/5.0/en/select.html
An example:
You can achieve what you want with the mysql console with the -s (--silent) option passed in.
It's probably a good idea to also pass in the -r (--raw) option so that special characters don't get escaped. You can use this to pipe queries like you're wanting.
mysql -u username -h hostname -p -s -r -e "select concat('this',' ','works')"
EDIT: Also, if you want to remove the column name from your output, just add another -s (mysql -ss -r etc.)
Since I find myself rather regularly looking for this exact problem (in the hopes I missed something before...), I finally decided to take the time and write up a small gist to export MySQL queries as CSV files, kinda like https://stackoverflow.com/a/28168869 but based on PHP and with a couple of more options. This was important for my use case, because I need to be able to fine-tune the CSV parameters (delimiter, NULL value handling) AND the files need to be actually valid CSV, so that a simple
CONCAT
is not sufficient since it doesn't generate valid CSV files if the values contain line breaks or the CSV delimiter.Caution: Requires PHP to be installed on the server! (Can be checked via
php -v
)"Install"
mysql2csv
via(download content of the gist, check checksum and make it executable)
Usage example
generates file
/tmp/result.csv
with contenthelp for reference