How to run SQL query inside docker container and export results outside the container (.CSV)

Viewed 2869

In SQL server I tried to export a CSV file using below command

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

But no luck, So How I can export the above query result to CSV file?

1 Answers

The easiest approach is to run the query inside the container and then export the result to file on the host.

docker exec -it some-mysql bash -c 'mysql -h myhost -u root -pPassword --database sample --batch -e "select * from company"' 

This will print the query result, now redirect the resulting output to file on the host.

docker exec -it some-mysql bash -c 'mysql -h myhost -u root -pPassword --database sample --batch -e "select * from company"'  > sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//' > company.csv

then upload the file to s3 from the host

aws s3 cp company.csv s3://mybucket/rds/mydb/table.csv

company sample databse

Related