In my previous post I had discussed on Exporting Query Results from MySQL to CSV file. Today let us see how to redirect the output to file.
Before actually jumping into the solution, Let me answer to this:
Why do we need to redirect the output from MySQL queries to a file?
>> If you want to keep a record of what you did in a mysql session, then redirecting will be useful. Please note that redirecting allows you to write the data into a file in your local host i.e. on your local/client machine and not in the MySQL server location.
How to do this?
Well, Let us first establish MySQL DB connection from Command Prompt first by issuing the below command. (To know more on establishing MySQL DB connection from command prompt check out my previous post here.)
mysql --user=root --password=root --tee=c:\mysqloutput.txt
Here in the above command, provide your actual user & password details along with the output file path. This will start MySQL session with a --tee option which will record your session in the specified file.
Note that the above command will create a Tee File which will maintain a log of your interactive MySQL session. At some point of time if you want to revisit on any complex queries which you had worked earlier this approach will be useful.
Now the common question that arises at this point is “How to control the session logging from MySQL?” or “What if I want to log only a part of my interaction with MySQL?”
The answer is very simple, By using \T & \t commands we can control on what to record and what not to record into file.
Issue \T command to log the session of your mysql interaction to a file. On issuing this command you will get a message something like this “Logging to file 'c:\mysqloutput.txt'”
Now, If you want to stop logging to a file issue \t command. On issuing this command you will get a message like this ‘Outfile disabled’
This way you can either log the entire session into a file or only a part of the session by using \T & \t commands.
I shall discuss about the difference b/w Exporting Query Results from MySQL to a file using Select …. INTO OUTFILE approach w.r.t Redirecting output from MySQL queries to a file in my next post.
Feel free to let me know if you face any issues in reproducing the above steps. Keep visiting this blog for more information… till then happy reading..!! :)
Before actually jumping into the solution, Let me answer to this:
Why do we need to redirect the output from MySQL queries to a file?
>> If you want to keep a record of what you did in a mysql session, then redirecting will be useful. Please note that redirecting allows you to write the data into a file in your local host i.e. on your local/client machine and not in the MySQL server location.
How to do this?
Well, Let us first establish MySQL DB connection from Command Prompt first by issuing the below command. (To know more on establishing MySQL DB connection from command prompt check out my previous post here.)
mysql --user=root --password=root --tee=c:\mysqloutput.txt
Here in the above command, provide your actual user & password details along with the output file path. This will start MySQL session with a --tee option which will record your session in the specified file.
Note that the above command will create a Tee File which will maintain a log of your interactive MySQL session. At some point of time if you want to revisit on any complex queries which you had worked earlier this approach will be useful.
Now the common question that arises at this point is “How to control the session logging from MySQL?” or “What if I want to log only a part of my interaction with MySQL?”
The answer is very simple, By using \T & \t commands we can control on what to record and what not to record into file.
Issue \T command to log the session of your mysql interaction to a file. On issuing this command you will get a message something like this “Logging to file 'c:\mysqloutput.txt'”
Now, If you want to stop logging to a file issue \t command. On issuing this command you will get a message like this ‘Outfile disabled’
This way you can either log the entire session into a file or only a part of the session by using \T & \t commands.
I shall discuss about the difference b/w Exporting Query Results from MySQL to a file using Select …. INTO OUTFILE approach w.r.t Redirecting output from MySQL queries to a file in my next post.
Feel free to let me know if you face any issues in reproducing the above steps. Keep visiting this blog for more information… till then happy reading..!! :)
Redirecting output from MySQL Queries to a file
Reviewed by Suntaragali The Smart Techie
on
February 20, 2013
Rating:
thanks a lot. It help me so much!!!
ReplyDelete