Skip to content

Instantly share code, notes, and snippets.

@aamir814
Created November 7, 2023 22:46
Show Gist options
  • Select an option

  • Save aamir814/b0ea567f14b6cd009a86765b8bb19bc4 to your computer and use it in GitHub Desktop.

Select an option

Save aamir814/b0ea567f14b6cd009a86765b8bb19bc4 to your computer and use it in GitHub Desktop.
Install Percona Toolkit and Export Mysql Table in CSV Format
#!/usr/bin/env bash
#####################################################################
# Purpose: Following are commands needed to install Percona tool kit on
# Amazon Linux 2023 AMI. It also shows how to export data from MySQL table
# into CSV format.
#
# Assumption: You have EC2 instance (Amazon Linux 2023 AMI) up and
# running and you can ssh into it.
# You have RDS MySQL (or Aurora MySQL) up and running
# i.e. ssh -i "some-key.pem" [email protected]
#
# REFERENCES
# - https://www.percona.com/software/database-tools/percona-toolkit
#
#####################################################################
# CHANGE ME
export DB_INSTANCE_NAME=sample-replica-cluster.cluster-cuqvlc7xce5s.us-east-1.rds.amazonaws.com
export DB_NAME=classicmodels
export TABLE_NAME=products
export USER_NAME=aamir
# update perl
sudo yum install perl
sudo yum install "perl(DBD::mysql)"
# verify DBI is installed
perldoc -l DBI
#install mysql client
sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm && sudo dnf -y install mysql-community-server
#unable to find yum package for percona-toolkit. Going to install manually
#yum repolist all
# this is older version found on percona yum repo. Latest is 3.5.5
#sudo yum install https://repo.percona.com/yum/release/latest/RPMS/x86_64/percona-toolkit-3.2.1-1.el6.x86_64.rpm
# get percona-toolkit
wget https://downloads.percona.com/downloads/percona-toolkit/3.5.5/binary/tarball/percona-toolkit-3.5.5_x86_64.tar.gz
gunzip percona-toolkit-3.5.5_x86_64.tar.gz
tar -xvf percona-toolkit-3.5.5_x86_64.tar
# install percona-toolkit
cd percona-toolkit-3.5.5/
perl Makefile.PL
make
make test
sudo make install
# connect to mysql to verify connection
mysql -h $DB_INSTANCE_NAME -u $USER_NAME -p
# run percona tool to export data in csv format
# VERY IMPORTANT: must use --no-delete option otherwise script will
# DELETE your data
pt-archiver --source h=$DB_INSTANCE_NAME,D=$DB_NAME,t=$TABLE_NAME \
-u $USER_NAME \
--ask-pass \
--file './products-archiver.csv' \
--where "1=1" \
--output-format csv \
--no-check-charset \
--no-delete
@aamir814
Copy link
Author

aamir814 commented Nov 7, 2023

Export Data from RDS MySQL to CSV File


A customer wanted to know how to export data from RDS MySQL table into CSV format. There are few different ways to export data. In the above script, I am showing how to install Percona Toolkit and use pt-archiver to export data in CSV format.

Here are other ways I could think:

  • mysql command can be used to output data to file. But it is limited to tab delimited. If you want a comma-separated output, you can use the CONCAT function in your SQL query to manually format your output as CSV. Here's an example:
mysql --host=$DB_INSTANCE_NAME --user=$USER_NAME -p $DB_NAME -e \
   "SELECT CONCAT(productCode, ',', productName, ',', productLine, ',', productScale, ',', productVendor, ',', productDescription, ',', quantityInStock, ',', buyPrice) FROM $TABLE_NAME WHERE productCode BETWEEN 'S10_1949' AND 'S24_2000'" > ./output.csv
  • mysqldump - This option will not export data in CSV format for Cloud Managed databases

  • Output to s3 - You can use SELECT INTO OUTFILE S3 clause to write data in CSV format to s3 bucket. This requires IAM roles with specific permissions. For example:

SELECT isoId, mktDate, nodeId, peakTypeId, hour, lmp, mcc, mlc 
  FROM historic_da_prices_hourly 
 WHERE mktDate BETWEEN '2023-01-02' AND '2023-01-02' AND isoId = 7
 INTO OUTFILE S3 's3://aurora-select-into-s3-pdx/historic_da_prices_hourly'
    FIELDS TERMINATED BY ','
    LINES TERMINATED BY '\n'
  • AWS Data Pipeline - You can use Data Pipeline to automate the movement of data from Amazon Aurora MySQL to Amazon S3.

  • AWS Glue - A bit complicated and requires few lines of code but you can get data in parquet format in s3.

As time permits, I'll put instructions on how to export in different ways.

#rds #mysql #export #csv #percona-toolkit #pt-archiver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment