导出表从Amazon RDS成csv文件文件、Amazon、RDS、csv

2023-09-11 10:21:54 作者:持刀人。

我在亚马逊RDS运行MySQL数据库,我想知道如何将整个表导出为CSV格式。我目前使用MySQL服务器在Windows上查询亚马逊的数据库,但是当我试图运行一个出口,我得到一个错误,可能是因为没有专门的文件服务器,亚马逊RDS。有没有什么解决办法?

I have a mysql database running in Amazon RDS, and I want to know how to export an entire table to csv format. I currently use mysql server on Windows to query the Amazon database, but when I try to run an export I get an error, probably because there's no dedicated file server for amazon RDS. Is there any solution to this?

推荐答案

presumably你正试图从一个亚马逊RDS 通过 SELECT ... INTO OUTFILE 查询,这将产生这个确实是经常遇到的问题数据库,如见导出数据库到CSV 。各 AWS团队响应确认您缺少服务器访问preventing出口的假设

Presumably you are trying to export from an Amazon RDS database via a SELECT ... INTO OUTFILE query, which yields this indeed commonly encountered issue, see e.g. export database to CSV. The respective AWS team response confirms your assumption of lacking server access preventing an export like so, and suggests an alternative approach as well via exporting your data in CSV format by selecting the data in the mysql command line client and piping the output to reformat the data as CSV, like so:

mysql -u username -p --database=dbname --host=rdshostname --port=rdsport --batch 
  -e "select * from yourtable" 
  | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > yourlocalfilename

用户fpalero 提供了一种替代并理应更简单的方法,如果你知道,并指定该领域的前期:

User fpalero provides an alternative and supposedly simpler approach, if you know and specify the fields upfront:

mysql -uroot -ppassword --database=dbtest 
  -e "select concat(field1,',',field2,',',field3) FROM tabletest" > tabletest.csv

祝你好运!