mysql dump csv,使用mysqldump将表数据导出到csv文件中

I want to export a table data into a csv file using mysqldump.

I want to make something like:

mysqldump --compact --no_create_info --tab=testing --fields-enclosed-by=\" --fields-terminated-by=, -uroot -proot mydatabase mytable

but i keep getting this error:

(Errcode: 13) when executing 'SELECT INTO OUTFILE'

I made my testing folder writable(I'm using Ubuntu as enviornment). Can somenone explain how to export a table in a CSV file, or how to modify my command shell in order to work? Thanks!

解决方案

The trouble with all these INTO OUTFILE or --tab=tmpfile answers is that it requires running mysqldump on the same server as the MySQL server.

My solution was simply to use mysql (NOT mysqldump) with the -B parameter, inline the SELECT statement with -e, then massage the ASCII output with sed, and wind up with CSV including a header field row:

Example:

mysql -B -u username -ppassword database -h dbhost -e "SELECT * FROM accounts;" |sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g"

"id","login","password","folder","email"

"8","mariana","57d40c8a954bc9e65f401592062019b1457be359","mariana",""

"3","squaredesign","b01134fac01dab765bcb55ab0ca33f9ec2885a7b","squaredesign","mkobylecki@squaredesign.com"

"4","miedziak","601f1889667efaebb33b8c12572835da3f027f78","miedziak","miedziak@mail.com"

"5","Sarko","480225f1ac707031bae300f3f5b06dbf79ed390e","Sarko",""

"6","Logitrans

Poland","9033b6b3c4acbb27418d8b0b26f4c880ea6dea22","LogitransPoland",""

"7","Amos","50f5604164db2d5ff0e984f973d2202d5358b6a6","Amos",""

"9","Annabelle","07e832cb64e66fa03c13498a26a5f8e3bdebddf1","Annabelle",""

"11","Brandfathers and

Sons","f08b194668249e4cb81fbb92df846e90569f5c01","BrandfathersAndSons",""

"12","Imagine

Group","e86f1645504c7d5a45ed41d16ecc39ed19181773","ImagineGroup",""

"13","EduSquare.pl","80c3c099f4ecc043a19f1527400d63588567a0ad","EduSquare.pl",""

"101","tmp","b01134fac01dab765bcb55ab0ca33f9ec2885a7b","_","WOBC-14.squaredesign.atlassian.net@yoMama.com"

Add a > outfile.csv at the end of that one-liner, to get your CSV file.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值