Mysql export resultset as CSV from remote server

You may use outfile and afterwards ftp the file to the local host or pass the result of a normal query to some sed/awk to convert it to csv?

What I found is: mysql -umysqlusername -pmysqlpass databasename -B -e "select * from \`tablename\`;" | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > mysql_exported_table.csv

Besides that, we're going with the (s)ftp solution.


Newer versions of the MySQL clients (tested on 14.14) on Linux use tab delimiters by default when redirecting stdout to a file:

mysql -h serverhostname -uuserid -ppassword databasename -e 'select * from mytbale order by update_ts desc' >output.tsv

Most apps that accept csv will accept tsv, or you should be able to convert easily yourself, see How do I convert a .tsv to .csv?.

Tags:

Mysql