Created
November 7, 2023 22:46
-
-
Save aamir814/b0ea567f14b6cd009a86765b8bb19bc4 to your computer and use it in GitHub Desktop.
Install Percona Toolkit and Export Mysql Table in CSV Format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
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:
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