Doing a 'Incremental' Dump With MySQL Shell

In previous posts, I discussed how to use MySQL Shell to dump and load data using a multithreaded process. When using util.dumpInstance()
, we create a dump of all the data and schema changes, even those that have already been dumped previously. But what if we only want to dump the data and schema changes that have occurred since the last dump? In this post, we will demonstrate how to use MySQL Shell and util.dumpBinlogs()
to achieve this.
The Setup
For this demo, I created two MySQL Sandboxes running on ports 2319 and 2320. I ran the following commands on the instance running on port 2319 to create a new schema and table and added some data to the table.
As we can see, this is a basic table with just three columns.
After executing these scripts, I ran util.dumpInstance('~/dumps/binlog_setup)
which saved the dump to the dumps/binlog_setup
directory under my home folder.
I switched the sandbox instance on port 2320 and executed util.loadDump('~/dumps/binlog_setup')
to load the dump into this sandbox.
Both instances are now in sync.
In MySQL systems with large datasets or complex schemas, moving data from one instance to another can be time-consuming.
Adding Data & Schema Changes
Let’s make some data and schema changes to the instance running on port 2319.
Dumping the Binlogs
Now that we have changed our data and schema, we can dump the binlogs using util.dumpBinlogs()
. Here is the syntax we can use.
This command tells MySQL Shell to dump the binlogs to the dumps/binlog_dump
directory under my home folder. The since
option tells MySQL Shell to only dump the binlogs that have occurred since the last dump. In this case, the last dump was saved in the dumps/binlog_setup
directory. This location is the dump that was created when I ran util.dumpInstance('~/dumps/binlog_setup)
. The value of since
can be the result of a previous call to util.dumpInstance()
or util.dumpBinlogs()
.
Loading the Binlogs
With our changes dumped, we can now load the binlogs into the instance running on port 2320. Here is the syntax we can use.
This command tells MySQL Shell to load the binlogs from the dumps/binlog_dump
directory under my home folder. The ignoreGtidGap
option tells MySQL Shell to ignore any GTID gaps between the source and target instances. I am unsure why I was getting an error about gaps in the GTID sequence (that may be a post for another day), but using this option allowed me to get past that issue.
Checking For Changes
With the binlogs loaded, we can now check that the data and schema changes were applied correctly. First, let’s check the schema changes by running the following command.
The results should resemble the text below.
Notice how demo2
is now listed as a schema.
Next, running the following command will check that our data changes were applied correctly.
The query result indicates that the records for Lucky and Peterson have been successfully added to the table.
Wrap Up
MySQL Shell is a powerful tool that enables us to quickly dump and load data. When we use util.dumpInstance()
and util.loadInstance()
, we dump and load all data and schema changes, even if they have been previously dumped. In contrast, when we use util.dumpBinlogs()
and util.loadBinlogs()
, we can perform an incremental dump of our data. This approach is particularly useful for managing large datasets or complex database schemas, allowing us to apply changes more efficiently and with less overhead.
Photo by Timo C. Dinger 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 Dump with MySQL Shell
- 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