mysql dump格式,使用'mysqldump'转储CSV格式的所有表

本文介绍了如何使用`mysql`命令而非`mysqldump`来将MySQL数据库中的所有表格数据导出为CSV格式。通过在命令行中结合`SELECT * FROM`语句、`sed`命令进行文本转换,可以生成包含表头的CSV文件。首先,展示了一个针对单个表格的示例,然后给出了获取数据库中所有表格名称的命令,并演示了如何在Bash shell中使用循环遍历这些表格,逐一执行导出操作。
摘要由CSDN通过智能技术生成

I need to dump all tables in MySQL in CSV format.

Is there a command using mysqldump to just output every row for every table in CSV format?

解决方案

First, I can give you the answer for one table:

The trouble with all these INTO OUTFILE or --tab=tmpfile (and -T/path/to/directory) answers is that it requires running mysqldump on the same server as the MySQL server, and having those access rights.

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 -p password database -h dbhost -e "SELECT * FROM accounts;" \

| sed "s/\"/\"\"/g;s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g"

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

"8","mariana","xxxxxxxxxx","mariana",""

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

"4","miedziak","xxxxxxxxxx","miedziak","miedziak@mail.com"

"5","Sarko","xxxxxxxxx","Sarko",""

"6","Logitrans

Poland","xxxxxxxxxxxxxx","LogitransPoland",""

"7","Amos","xxxxxxxxxxxxxxxxxxxx","Amos",""

"9","Annabelle","xxxxxxxxxxxxxxxx","Annabelle",""

"11","Brandfathers and

Sons","xxxxxxxxxxxxxxxxx","BrandfathersAndSons",""

"12","Imagine

Group","xxxxxxxxxxxxxxxx","ImagineGroup",""

"13","EduSquare.pl","xxxxxxxxxxxxxxxxx","EduSquare.pl",""

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

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

Next, get a list of all your tables with

mysql -u username -ppassword dbname -sN -e "SHOW TABLES;"

From there, it's only one more step to make a loop, for example, in the Bash shell to iterate over those tables:

for tb in $(mysql -u username -ppassword dbname -sN -e "SHOW TABLES;"); do

echo .....;

done

Between the do and ; done insert the long command I wrote in Part 1 above, but substitute your tablename with $tb instead.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值