Save MySQL Output to a Text File

I have just found out a nifty little trick regarding MySQL output the other day. I just had to put this up as a reminder to myself, and also to share with the viewers.

Let say, you want to have the output of your SELECT statement created to a text file. The MySQL database server could be sitting on a different box (or the same machine, but that doesn’t really matter). All you have to do is issue a command in your terminal:

echo "select * from MyTable" | mysql -h mydatabaseserver -u myusername -pmypassword mydatabase > output.txt

That’s it! Just replace the <MyTable>to your table of choice (or just replace the whole SELECT statement to suit your needs), <mydatabaseserver> to your database server name, <myusername> to your database user name, <mypassword> to your password, and <mydatabase> to your database.
The piping ‘>’ will redirect your output to a text file on your desired location.



Related posts:
  1. SQL Select Statement with Comma-Separated List Condition I have one problem the other day; Why does my “SELECT...
  2. VPN Connection in Ubuntu using VPNC I’ve been using VPNC in my Ubuntu to access my...

2 Comments

  1. Did know you could do this. I always use the -e option and re-direct it to a file

  2. I mean didn’t know you could do this

Leave a Comment