Advanced Data Dump with MySQL Shell

Over the last few years, I have become quite smitten with MySQL Shell. For those who may not be familiar with MySQL Shell, it is a new(ish) command line interface (CLI) for connecting to and managing MySQL instances. During a recent episode of Inside MySQL: Sakila Speaks, Fred and I talked to Miguel Araujo about many of the helpful (and lesser known) features of MySQL Shell. This post is the eighth in a series about these “hidden gem” features.
The Problem
There is no ‘problem’ for this post, but I wanted to keep the formatting I have used for other MySQL Shell Hidden Gems posts. I have also talked about dumping and loading data with MySQL Shell in previous posts, so today, I want to cover some of the options available when dumping data using MySQL Shell.
The Solution
Once again, not really a ‘solution’, but I want to stick to the formatting of related posts.
First, let’s take a look at some of the options we can use when dumping data. We can use these options regardless of the function used (util.dumpInstance()
, util.dumpSchemas()
, or util.dumptables()
). The last (and optional) argument for each function is the options configuration block in JSON format. For example, if we wanted to dump our entire instance with options, we would use a command that resembles:
I will discuss only some options, just those I find interesting and helpful.
Dry Ryn
If we have a database that haas a lot of data, we may want to do a dry run before running the dump to ensure everything will go as expected. To do a dump with this option, we add dryRun: true
. For example:
When I run this command against my local MySQL instance, I see the following results:
First, note that we see a message that no files will be created - which is good because we want to avoid creating files when doing a dry run.
Next, look for the line that tells us how many schemas will be dumped. Note that we are not dumping all the schemas, this is because util.dumpInstance()
does nto dump any system tables.
The rest of the output details the processes that will take place during the dump. If our data had any issues, we would see them in this output.
Threading
I have talked about this before, but it bears repeating. MySQL Shell can do multi-threaded dumps. This option makes the process of dumping data faster. By default, MySQL Shell uses four threads. Here is the syntax:
The output from this command would resemble the text below.
The number of threads specified might not be how many are used when dumping data. Fewer threads may be used if there is a limited amount of data or fewer tables. Also, don’t think that more threads will always mean better performance. That may not be the case.
Filtering Dumped Data
If we do not want to dump all our data, we can filter what data is dumped using the where
option. This option is handy when you want to eliminate older data from a dump or only need or want a subset of the data.
Please take a look at a table dump in my database.
This command tells MySQL Shell to dump the entire’ games’ table in the ‘mysql-shorts’ schema.
Here is the output from that command.
This output shows that 40 rows were written to the dump.
Here is an example of dumping only the rows in the games
table where the score was greater than or equal to 90.
The where
option is a JSON object where each key in the object is the name of a column, and the value of that key is the condition we want to use for our filter. Here is the output from that command:
This output shows that only 14 rows were written to the dump.
Dumping to Oracle Cloud
One of my favorite features of MySQL Shell is the ability to dump to an Oracle Cloud storage bucket. There are several options available to us to take advantage of this feature.
This example assumes you have installed the OCI CLI.
Depending on how the OCI CLI is configured on your system, you may also need to include your bucket’s namespace
.
Here is the output from this command.
This image shows the files we created in the database_dumps
bucket in my OCI account.
Ensuring HeatWave Compatability
If we are going to load our data into a HeatWave MySQL instance, we need to ensure that there are no issues with our database and table structure. We use the ocimds
options to enable these compatibility checks.
If you have no compatability issues, the dump will proceed as usual. If you do have compatability issues, your output may look like the following:
This output references several different issues, including issues with DEFINER
clauses, missing primary keys, and missing SQL SECURITY INVOKER
when defining some stored programs. Fortunately, the output gives us suggestions on how to ensure compatibility. Some suggestions include changing the DDL or adding the compatibility
option.
Using these suggestions, we can rewrite our command using the compatibility
option.
The output form this command shows the following:
This output shows what changes will be made to the schemas when the dump is created. These changes include adding invisible primary keys to several tables, removing the definer from some objects, and setting the SQl SECURITY characteristic
to INVOKER
on others.
By setting these configuration options, we ensure that compatibility will be maintained when we load this data into a HeatWave MySQL instance.
Wrap Up
The utilities in MySQL Shell for dumping data from a MySQL database are an excellent combination of ease and power. We can use options to limit the data we want to dump, specify an OCU bucket to dump the data into, ensure HeatWave compatibility, and perform a dry run of our dump process. Check out the MySQL Shell Dump Documentation to learn more about the different dump commands and other options available in MySQL Shell.
Photo by Michael Fousert on Unsplash
Related Entries
- Running External Scripts in MySQL Shell
- Executing System Commands in MySQL Shell
- Getting Help in MySQL Shell
- Sandboxes in MySQL Shell
- Server Upgrade Checks with MySQL Shell
- MySQL Connection Status with MySQL Shell
- Managing MySQL Shell Configuration Options
- Advanced Data Load with MySQL Shell
- MySQL Shell CLI Integration
- MySQL Shell Table Export Utility
- Copy Data Directly To A MySQL Instance With MySQL Shell
- Create an InnoDB Replica Set With MySQL Shell
- Using MySQL Shell to Get MySQL Diagnostic Information
- Customizing The MySQL Shell Prompt
- Extending MySQL Shell with Startup Scripts
- Extending MySQL Shell with Plugins
- Doing a 'Incremental' Dump With MySQL Shell