{"id":4081,"date":"2012-08-15T11:23:23","date_gmt":"2012-08-15T09:23:23","guid":{"rendered":"http:\/\/code.openark.org\/blog\/?p=4081"},"modified":"2012-08-15T11:23:23","modified_gmt":"2012-08-15T09:23:23","slug":"ways-to-export-mysql-result-set-to-file-on-client-side","status":"publish","type":"post","link":"https:\/\/code.openark.org\/blog\/mysql\/ways-to-export-mysql-result-set-to-file-on-client-side","title":{"rendered":"Ways to export MySQL result set to file on client side"},"content":{"rendered":"<p><strong>Problem<\/strong>: you wish to write table data to file, but you wish to do so on client side.<\/p>\n<p><strong>SELECT &#8230; INTO OUTFILE<\/strong> writes the file on server. What are your options on client?<\/p>\n<h4>1. mysql client<\/h4>\n<p>If you have direct access from your client machine to your DB server machine, and can connect via <strong>mysql<\/strong> client, you get a very customizable file write:<\/p>\n<blockquote>\n<pre>bash$ <strong>mysql<\/strong> -h db_host --execute \"SELECT * FROM my_table\" &gt; \/tmp\/output.txt<\/pre>\n<\/blockquote>\n<p>The above writes fancy tables, so you probably want to:<\/p>\n<blockquote>\n<pre>bash$ <strong>mysql<\/strong> -h db_host --execute \"SELECT * FROM my_table\" --bat &gt; \/tmp\/output.txt<\/pre>\n<\/blockquote>\n<p>Also try:<\/p>\n<blockquote>\n<pre>bash$ <strong>mysql<\/strong> -h db_host --execute \"SELECT * FROM my_table\" --silent --raw &gt; \/tmp\/output.txt<\/pre>\n<\/blockquote>\n<p>To turn off headers.<\/p>\n<p>More options include the <strong>&#8211;xml<\/strong> or <strong>&#8211;html<\/strong> formats.<!--more--><\/p>\n<h4>2. tee<\/h4>\n<p><strong>tee<\/strong> works similarly to a unix <em>tee<\/em> command. From the mysql command line, issue:<\/p>\n<blockquote>\n<pre>mysql&gt; <strong>tee<\/strong> <em>file_name.txt<\/em><\/pre>\n<\/blockquote>\n<p>Anything you type from that moment on, and anything returned from the server, are written to given file, in the same format by which they are displayed on your client.<\/p>\n<p>This means result sets are written in table format (what with all the <strong>+&#8212;+&#8212;&#8211;+&#8212;+<\/strong> frames), which is not most convenient to parse later on.<\/p>\n<h4>3. pager<\/h4>\n<p>If you&#8217;re on unix\/linux, you have a third option: use the <strong>pager<\/strong> command to write result sets to file. For example, use:<\/p>\n<blockquote>\n<pre>mysql&gt; <strong>pager<\/strong> cat - &gt;&gt; \/tmp\/pager_output.txt<\/pre>\n<\/blockquote>\n<p>This will work similarly to the tee command, but will not verbose to screen. In the above we <em>append<\/em> results to file.<\/p>\n<h4>4. pager, Unix tee<\/h4>\n<p>So, while we&#8217;re at it, one can:<\/p>\n<blockquote>\n<pre>mysql&gt; <strong>pager<\/strong> <strong>tee<\/strong> \/tmp\/tee_file.txt<\/pre>\n<\/blockquote>\n<p>The above will rewrite the file for every result set. Play at your own leisure to generate different files. For example, use<\/p>\n<blockquote>\n<pre>mysql&gt; <strong>pager<\/strong> <strong>tee<\/strong> \/tmp\/tee_$(date +%M%H)_file.txt<\/pre>\n<\/blockquote>\n<p>for timestamp signature.<\/p>\n<p><strong>pager<\/strong> + unix command open an endless gateway of opportunities. Use <strong>awk<\/strong> to get rid of table frames. Use <strong>grep<\/strong> to filter frame rows out. What have you.<\/p>\n<h4>5. GUI Clients<\/h4>\n<p>I&#8217;m pretty sure your favorite GUI client does that, as well. Like the <strong>mysql<\/strong> client, you will need direct access to the DB server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: you wish to write table data to file, but you wish to do so on client side. SELECT &#8230; INTO OUTFILE writes the file on server. What are your options on client? 1. mysql client If you have direct access from your client machine to your DB server machine, and can connect via mysql [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[5],"tags":[86,87],"class_list":["post-4081","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-client","tag-export"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2bZZp-13P","_links":{"self":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/4081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/comments?post=4081"}],"version-history":[{"count":23,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/4081\/revisions"}],"predecessor-version":[{"id":5237,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/4081\/revisions\/5237"}],"wp:attachment":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/media?parent=4081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/categories?post=4081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/tags?post=4081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}