問題描述
我有一個在 Amazon RDS 中運行的 MySQL 數據庫,我想知道如何將整個表格導出為 CSV 格式.
I have a MySQL database running in Amazon RDS, and I want to know how to export an entire table to CSV format.
我目前在 Windows 上使用 MySQL 服務器來查詢 Amazon 數據庫,但是當我嘗試運行導出時出現錯誤,可能是因為沒有用于 amazon RDS 的專用文件服務器.有沒有解決辦法?
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 a solution to this?
推薦答案
據推測,您正在嘗試從 Amazon RDS 數據庫通過 SELECT ... INTO OUTFILE
查詢,這會產生這個確實經常遇到的問題,參見例如將數據庫導出為 CSV.相應的 AWS 團隊回復證實了您對缺乏服務器訪問權限的假設防止像這樣導出,并建議一種替代方法,通過在 MySQL 命令行客戶端中選擇數據并通過管道將輸出以 CSV 格式導出您的數據以將數據重新格式化為 CSV,像這樣:
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
這篇關于將 Amazon RDS 中的表導出為 CSV 文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!