Manage Table Schemas of Tables to Be Migrated Using TiDB Data Migration
This document describes how to manage the schema of the table in DM during migration using dmctl.
When DM performs incremental replication, it first reads the upstream binlog, then creates SQL statements and executes them in the downstream. However, the upstream binlog does not contain the complete table schema. To generate the SQL statements, DM maintains internally the schema information of the table to be migrated. This is called the internal table schema.
To deal with some special occasions, or to handle migration interruptions caused by mismatch of the table schemas, DM provides the binlog-schema
command to obtain, modify, and delete the internal table schema.
Implementation principles
The internal table schema comes from the following sources:
- For full data migration (
task-mode=all
), the migration task goes through three stages: dump/load/sync, which means full export, full import, and incremental replication. In the dump stage, DM exports the table schema information along with the data and automatically creates the corresponding table in the downstream. In the sync stage, this table schema is used as the starting table scheme for incremental replication. - In the sync stage, when DM handles DDL statements such as
ALTER TABLE
, it updates the internal table schema at the same time. - If the task is an incremental migration (
task-mode=incremental
), in which the downstream has completed creating the table to be migrated, DM obtains the table schema information from the downstream database. This behavior varies with DM versions.
For incremental replication, schema maintenance is complicated. During the whole data replication, the following four table schemas are involved. These schemas might be the consistent or inconsistent with one another:
- The upstream table schema at the current time, identified as
schema-U
. - The table schema of the binlog event currently being consumed by DM, identified as
schema-B
. This schema corresponds to the upstream table schema at a historical time. - The table schema currently maintained in DM (the schema tracker component), identified as
schema-I
. - The table schema in the downstream TiDB cluster, identified as
schema-D
.
In most cases, the preceding four table schemas are consistent.
When the upstream database performs a DDL operation to change the table schema, schema-U
is changed. By applying the DDL operation to the internal schema tracker component and the downstream TiDB cluster, DM updates schema-I
and schema-D
in an orderly manner to keep them consistent with schema-U
. Therefore, DM can then normally consume the binlog event corresponding to the schema-B
table schema. That is, after the DDL operation is successfully migrated, schema-U
, schema-B
, schema-I
, and schema-D
are still consistent.
Note the following situations that might cause inconsistency:
During the migration with optimistic mode sharding DDL support enabled, the
schema-D
of the downstream table might be inconsistent with theschema-B
andschema-I
of some upstream sharded tables. In such cases, DM still keepsschema-I
andschema-B
consistent to ensure that the binlog event corresponding to DML can be parsed normally.When the downstream table has more columns than the upstream table,
schema-D
might be inconsistent withschema-B
andschema-I
. In the full data migration (task-mode=all
), DM automatically handles inconsistency. In the incremental migration (task-mode=incremental
), because the task is on a first start and there is no internal schema information yet, DM automatically reads the downstream schema (schema-D
) and updatesschema-I
(this behavior varies with DM versions). After that, if DM usesschema-I
to parseschema-B
's binlog, it will reportColumn count doesn't match value count
error. For details, refer to Migrate Data to a Downstream TiDB Table with More Columns.
You can run the binlog-schema
command to obtain, modify, or delete the schema-I
table schema maintained in DM.
Command
help binlog-schema
manage or show table schema in schema tracker
Usage:
dmctl binlog-schema [command]
Available Commands:
delete delete table schema structure
list show table schema structure
update update tables schema structure
Flags:
-h, --help help for binlog-schema
Global Flags:
-s, --source strings MySQL Source ID.
Use "dmctl binlog-schema [command] --help" for more information about a command.
Parameters
delete
: Deletes the table schema.list
: Lists the table schema.update
: Updates the table schema.-s
or--source
:- Required.
- Specifies the MySQL source that the operation is applied to.
Usage example
Get the table schema
To get the table schema, run the binlog-schema list
command:
help binlog-schema list
show table schema structure
Usage:
dmctl binlog-schema list