Debian Admin - Your way to Debian World

February 6, 2007

Export Mysql Database into a CSV File

by @ 6:15 pm. Filed under Database

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

If you want to export your mysql databases into a csv file here is simple tip for you.

You need to run the following command from your terminal

mysql -u exampleuser -p letmein exampledb -B -e “select * from \`person\`;” | sed ’s/\t/”,”/g;s/^/”/;s/$/”/;s/\n//g’ > filename.csv

Here is some sample output of the above

“id”,”username”,”group”,”password”
“1″,”male”,”admin”,”5f4dcc3b5aa765d61d8327deb882cf99″
“2″,”newton”,”admin”,”5f4dcc3b5aa765d61d8327deb882cf99″
“3″,”ruchi”,”admin”,”5f4dcc3b5aa765d61d8327deb882cf99″
“4″,”ruchi1″,”staff”,”5f4dcc3b5aa765d61d8327deb882cf99″
“5″,”tej”,”staff”,”5f4dcc3b5aa765d61d8327deb882cf99″
“6″,”tej1″,”admin”,”5f4dcc3b5aa765d61d8327deb882cf99″

And now for the explanation:

Starting with the MySQL command.

The -u option is you need to enter the username

The -p option is you need to enter the password

“exampledb” - Database name

The -B option will delimit the data using tabs and each row will appear on a new line.

The -e option denotes the command to run once you have logged into the database.

In this case we are using a simple SELECT statement.

Onto sed. The command used here contains three seperate sed scripts:

s/\t/”,”/g;s/^/”/ —> this will search and replace all occurences of ‘tabs’ and replace them with a “,”.

;s/$/”/; —> This will place a ” at the start of the line.

s/\n//g —-> This will place a ” at the end of the line.

filename.csv —> Name of the file you want to export.

After running the result set through sed we redirect the output to a file with a .csv extension.

Tags: , , ,

You may also be interested in...

2 Responses to “Export Mysql Database into a CSV File”

  1. Thiago Says:

    I translated this article for the Portuguese of Brazil. The automatic translation was very bad:

    http://www.ajudalinux.org/wps/?p=91

  2. Sheeri Says:

    You can also use

    SELECT x,y,z INTO OUTFILE ‘/path/to/file’
    FIELDS TERMINATED BY ‘,’ ENCLOSED BY ‘”‘
    FROM tbl WHERE ….

    see http://dev.mysql.com/doc/refman/5.0/en/select.html

Leave a Reply

Subscribe RSS Feed

subscribe to the Debian Admin RSS feed

Internal links:

Sponsors:



Categories:

Support Debian Admin

Amount $:
Website(Optional):

Sponsors:

Archives:

Related Links:


Favourite Sites:

Wordpress Collection
Windows Reference
Ubuntu Geek
DebianHelp
All About Debian Tutorials
Power Electrical
Check Your IP Here
Debian,Ubuntu News
DebCentral
Tuxmachines
Capnkirby
Libervis
Nuxifield
Linux Horizon
Linux Appfinder
Debuntu
GNU/Linux For Everyone
Free Penguin
DebianAdmin is not related to the Debian Project.
This site is copyright © 2006,2007 Debian Admin
All Trademarks are the property of their respective owners.
The contents of this website may not be mirrored or archived without the express written permission of DebianAdmin Site Owner.

DISCLAIMER: All the information, troubleshooting methods, utilities offered in this website is provided AS-IS, without any warranties. Though I strive for perfection, and always test the validity and effectiveness of the troubleshooting content in various systems, I assume no responsibility for your use of these Fixes, Utilities and other troubleshooting advice. The author will not be liable for any special, incidental, consequential or indirect damages due to loss of data or any other reason. All use is completely at your own risk. Changes to the existing content and new additions are made to this website periodically, without notification.
Rodney's Kontera DynamiContext Plugin plugged in.