Synchronize Tables on the Same MySQL ServerIt is a common use case to synchronize data in two tables inside MySQL servers. This blog post describes one specific case: how to synchronize data between two different tables on the same MySQL server. This could be useful, for example, if you test DML query performance and do not want to affect production data. After few experiments, tables get out of sync and you may need to update the test one to continue working on improving your queries. There are other use cases when you may need to synchronize the content of the two different tables on the same server, and this blog will show you how to do it.

Table Content Synchronization

The industry-standard tool for table content synchronization – pt-table-sync – is designed to synchronize data between different MySQL servers and does not support bulk synchronization between two different databases on the same server yet. If you try it, you will receive an error message:

However, it is possible to synchronize two individual tables on the same server by providing table names as DSN parameters:

You may even synchronize two tables in the same database:

We can use this feature to perform bulk synchronization.

First, we need to prepare a list of tables we want to synchronize:

Then we can invoke the tool as follows:

If you have multiple database pairs to sync, you can agree on the file name and parse it before looping through table names. For example, if you use pattern SOURCE_DATABASE-TARGET_DATABASE.sync  you can use the following loop:

Note that pt-table-sync synchronizes only tables that exist in both databases. It does not create tables that do not exist in the target database and does not remove those that do not exist in the source database. If your schema could be out of sync, you need to synchronize it first.

I used option --verbose in all my examples, so you can see what the tool is doing. If you omit this option the tool still is able to synchronize tables on the same server.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments