89

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

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

4

7 回答 7

132

首先,我可以给你一张桌子的答案:

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

我的解决方案是简单地将mysql( not mysqldump ) 与-B参数一起使用,用 内联 SELECT 语句-e,然后用 按摩 ASCII 输出sed,最后使用包含标题字段行的 CSV:

例子:

 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","登录名","密码","文件夹","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", "安娜贝尔",""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"_","WOBC-14.squaredesign.atlassian.net@yoMama.com"_","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
     echo .....;
 done

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

于 2014-08-21T13:31:35.977 回答
34

此命令将在/path/to/directory table_name.sqltable_name.txt中创建两个文件。

SQL 文件将包含表创建模式,而 txt 文件将包含 mytable 表的记录,其中字段由逗号分隔。

mysqldump -u username -p -t  -T/path/to/directory dbname table_name --fields-terminated-by=','
于 2013-01-16T11:07:46.960 回答
21

mysqldump具有 CSV 格式的选项:

--fields-terminated-by=name
                  Fields in the output file are terminated by the given
--lines-terminated-by=name
                  Lines in the output file are terminated by the given

name应包含以下内容之一:

`--fields-terminated-by`

\t或者"\""

`--fields-enclosed-by=name`
   Fields in the output file are enclosed by the given

--lines-terminated-by

  • \r
  • \n
  • \r\n

当然,您应该单独对每个表进行 mysqldump。

我建议您将所有表名收集在一个文本文件中。然后,遍历所有运行 mysqldump 的表。这是一个脚本,它将一次转储和 gzip 10 个表:

MYSQL_USER=root
MYSQL_PASS=rootpassword
MYSQL_CONN="-u${MYSQL_USER} -p${MYSQL_PASS}"
SQLSTMT="SELECT CONCAT(table_schema,'.',table_name)"
SQLSTMT="${SQLSTMT} FROM information_schema.tables WHERE table_schema NOT IN "
SQLSTMT="${SQLSTMT} ('information_schema','performance_schema','mysql')"
mysql ${MYSQL_CONN} -ANe"${SQLSTMT}" > /tmp/DBTB.txt
COMMIT_COUNT=0
COMMIT_LIMIT=10
TARGET_FOLDER=/path/to/csv/files
for DBTB in `cat /tmp/DBTB.txt`
do
    DB=`echo "${DBTB}" | sed 's/\./ /g' | awk '{print $1}'`
    TB=`echo "${DBTB}" | sed 's/\./ /g' | awk '{print $2}'`
    DUMPFILE=${DB}-${TB}.csv.gz
    mysqldump ${MYSQL_CONN} -T ${TARGET_FOLDER} --fields-terminated-by="," --fields-enclosed-by="\"" --lines-terminated-by="\r\n" ${DB} ${TB} | gzip > ${DUMPFILE}
    (( COMMIT_COUNT++ ))
    if [ ${COMMIT_COUNT} -eq ${COMMIT_LIMIT} ]
    then
        COMMIT_COUNT=0
        wait
    fi
done
if [ ${COMMIT_COUNT} -gt 0 ]
then
    wait
fi
于 2012-08-20T16:10:56.057 回答
21

如果您使用 MySQL 或 MariaDB,最简单且高效的方式为单个表转储 CSV 是 -

SELECT customer_id, firstname, surname INTO OUTFILE '/exportdata/customers.txt'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  FROM customers;

现在您可以使用其他技术对多个表重复此命令。在此处查看更多详细信息:

于 2017-08-24T11:49:03.970 回答
11

这对我来说效果很好:

mysqldump <DBNAME> --fields-terminated-by ',' \
--fields-enclosed-by '"' --fields-escaped-by '\' \
--no-create-info --tab /var/lib/mysql-files/

或者,如果您只想转储特定表:

mysqldump <DBNAME> <TABLENAME> --fields-terminated-by ',' \
--fields-enclosed-by '"' --fields-escaped-by '\' \
--no-create-info --tab /var/lib/mysql-files/

我正在倾倒以/var/lib/mysql-files/避免此错误:

mysqldump:出现错误:1290:MySQL 服务器正在使用 --secure-file-priv 选项运行,因此在执行 'SELECT INTO OUTFILE' 时无法执行此语句

于 2019-01-08T14:08:19.597 回答
9

看起来其他人也有这个问题,现在有一个简单的 Python 脚本,用于将 mysqldump 的输出转换为 CSV 文件。

wget https://raw.githubusercontent.com/jamesmishra/mysqldump-to-csv/master/mysqldump_to_csv.py
mysqldump -u username -p --host=rdshostname database table | python mysqldump_to_csv.py > table.csv
于 2015-01-27T10:47:30.140 回答
1

您也可以使用dbForge Studio for MySQL中的数据导出工具来执行此操作。

它将允许您选择部分或所有表并将它们导出为 CSV 格式。

于 2012-08-21T06:01:20.087 回答