Home > OS >  Run mysql script from file and put the results into CSV file
Run mysql script from file and put the results into CSV file

Time:08-05

My question is quite simple, Im using MySql 5.7 (Cloud SQL so I dont have any access to any file in the DB server) and I want to run a sql select statement from file and put the result in CSV file. what mysql command that can help me on that plz? and I cant write the select statement in the terminal cause its about 60MB. it should be executed from the file

I tried MysqlWorkBench and it run out of memory every time I export the result to csv file. thats why Im trying to use the command line thinking it'll be much faster and a bit lighter

Any Help plz?

CodePudding user response:

What I do is use the mysql command-line client with the --batch option (or -B for short).

mysql --batch --execute "source myscript.sql"

It outputs tab-separated, not comma-separated, but that's easy enough to transform into a format you need.

I don't use MySQL Workbench for any task except designing ER diagrams.

CodePudding user response:

You can use mycli to produce a CSV file like so:

mycli -e '\T csv; source filename.sql'

  • Related