Export MySQL Query Results to CSV

If you’re SSH’d into a MySQL server and want to export MySQL query results to CSV (readable by Excel), simply add the below snippet to the end of your query.

Note that '/tmp/results.csv' in the code above should correspond to a writeable destination, and the file can’t already exist.