使用 'mysqldump' 以 CSV 格式转储所有表

Dump all tables in CSV format using #39;mysqldump#39;(使用 mysqldump 以 CSV 格式转储所有表)
本文介绍了使用 'mysqldump' 以 CSV 格式转储所有表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要以 CSV 格式转储 MySQL 中的所有表.

是否有使用 mysqldump只是以 CSV 格式输出每个表的每一行的命令?

解决方案

首先,我可以给你一个一个表的答案:

所有这些 INTO OUTFILE--tab=tmpfile(和 -T/path/to/directory)答案的问题是它需要在与 MySQL 服务器相同的服务器上运行 mysqldump ,并具有这些访问权限.

我的解决方案只是使用带有 -B 参数的 mysql(not mysqldump),内联使用 -e 的 SELECT 语句,然后使用 sed 处理 ASCII 输出,最后得到包含标题字段行的 CSV:

示例:

 mysql -B -u 用户名 -p 密码数据库 -h dbhost -e "SELECT * FROM accounts;"\|sed "s/\"/\"\"/g;s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g"

<块引用>

"id","login","password","folder","email"8"、玛丽安娜"、xxxxxxxxx"、玛丽安娜"、""3","squaredesign","xxxxxxxxxxxxxxxxx","squaredesign","mkobylecki@squaredesign.com""4","miedziak","xxxxxxxxx","miedziak","miedziak@mail.com""5","萨科","xxxxxxxxx","萨科","""6","Logitrans波兰","xxxxxxxxxxxxxx","LogitransPoland","""7","阿莫斯","xxxxxxxxxxxxxxxxxxxx","阿莫斯","""9","安娜贝尔","xxxxxxxxxxxxxxx","安娜贝尔",""《11》、《祖父和儿子们","xxxxxxxxxxxxxxxxx","BrandfathersAndSons","""12","想象组","xxxxxxxxxxxxxxx","ImagineGroup","""13","EduSquare.pl","xxxxxxxxxxxxxxxxx","EduSquare.pl","""101","tmp","xxxxxxxxxxxxxxxxxxxxx","_","WOBC-14.squaredesign.atlassian.net@yoMama.com"

添加<代码>>outfile.csv 在那一行的末尾,以获取该表的 CSV 文件.

接下来,获取所有表的列表

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

从那里开始,只需再执行一个循环即可,例如,在 Bash shell 中迭代这些表:

 for tb in $(mysql -u username -ppassword dbname -sN -e "SHOW TABLES;");做回声.....;完毕

do之间;done 插入我在上面第 1 部分中编写的长命令,但用 $tb 代替您的表名.

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.

这篇关于使用 'mysqldump' 以 CSV 格式转储所有表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)