MYSQL: Export einer Abfrage in eine Datei

Eine SQl-Abfrage in Datei ausgeben

Man kann das Ergebnis einer SQL Abfarge mittels INTO FILE ausgeben lassen. Das Problem: der Datenbankuser hat vermutlich keine Rechte auf dem Filesystem.

Daher die leichteste Option zum Export der SQL-Ergebnisse als erstes.

Richtig und leicht exportieren in csv

Als erstes eine Datei mit der SQL-Abfrage erstellen und dann folgendes ausführen:

mysql -u MYSQLUSER -p MYSQLDATENBANK < DATEIMITSQLABFRAGE.sql | sed 's/\t/,/g' > ABLAGEORTDERAUSGABE/out.csv

 

Save MySQL query results into a text or CSV file

Posted June 28, 2006 by Quinn McHenry in MySQL

Last Updated on August 30, 2011

MySQL provides an easy mechanism for writing the results of a select statement into a text file on the server. Using extended options of the INTO OUTFILE nomenclature, it is possible to create a comma separated value (CSV) which can be imported into a spreadsheet application such as OpenOffice or Excel or any other applciation which accepts data in CSV format.



Given a query such as

SELECT order_id,product_name,qty FROM orders 

which returns three columns of data, the results can be placed into the file /tmo/orders.txt using the query:

SELECT order_id,product_name,qty FROM orders
INTO OUTFILE '/tmp/orders.txt'

This will create a tab-separated file, each row on its own line. To alter this behavior, it is possible to add modifiers to the query:

SELECT order_id,product_name,qty FROM orders
INTO OUTFILE '/tmp/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

 

In this example, each field will be enclosed in “double quotes,” the fields will be separated by commas, and each row will be output on a new line separated by a newline (\n). Sample output of this command would look like:

"1","Tech-Recipes sock puppet","14.95" "2","Tech-Recipes chef's hat","18.95"

...

Keep in mind that the output file must not already exist and that the user MySQL is running as has write permissions to the directory MySQL is attempting to write the file to.

Syntax

SELECT Your_Column_Name
FROM Your_Table_Name  
INTO OUTFILE 'Filename.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

Or you could try to grab the output via the client:

You could try executing the query from the your local client and redirect the output to a local file destination:

mysql -user -pass -e "select cols from table where cols not null" > /tmp/output

Probleme

Es kann zu einer Fehlermeldung kommen aufgrund unzureichender Rechte.

ERROR 1045 (28000): Access denied for user 'ariberlinsql1'@'localhost' (using password: YES)

Der MYSQL Nutzer benötigt die Berechtigung "File" , daher als root die Berechtigungen setzen. Die Berechtigung File kann nur auf alle Datenbanken gesetzt werden:

SHOW GRANTS;
GRANT FILE ON *.* 
TO 'asdfsdf'@'localhost' 
IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES; 

Das Problem bleibt allerdings, dass in der Regel der Nutzer der Datenbank nicht übereinstimmt mit dem Nutzer auf dem Betriebssystem und daher doch der Weg über die Output Redirection genommen werden muss.

 

Quelle:

https://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-te…

https://stackoverflow.com/questions/21253704/how-to-save-mysql-query-ou…

 

Tags