All Products
Search
Document Center

PolarDB:Replicate data from PolarDB-X to MySQL

Last Updated:Mar 28, 2026

PolarDB-X is compatible with the MySQL replication protocol, so you can use a MySQL instance as a secondary database for PolarDB-X. Run SQL statements on the MySQL secondary to create, start, stop, and delete replication links. For the full SQL reference, see the MySQL documentation.

Prerequisites

Before you begin, ensure that you have:

  • A PolarDB-X instance (primary database)

  • A MySQL instance (secondary database) with a compatible version:

    • MySQL 5.7, if your PolarDB-X instance is compatible with MySQL 5.7

    • MySQL 8.0, if your PolarDB-X instance is compatible with MySQL 8.0. We recommend MySQL 8.0.27 or later.

  • A MASTER_USER account with sufficient privileges to connect to the PolarDB-X primary instance

Replication workflow

Managing a replication link follows this sequence:

Create link (CHANGE MASTER TO) → Start link (START SLAVE) → Stop link (STOP SLAVE) → Delete link (RESET SLAVE ALL)
Important

Stop the replication link before deleting it.

Usage notes

RETURNING optimization and data consistency

Starting from PolarDB-X version 5.4.12, the RETURNING optimization is enabled by default for INSERT IGNORE statements on tables with global secondary indexes (GSIs). In scenarios where PolarDB-X is the primary database and MySQL is the secondary database, this optimization can cause data inconsistency when MySQL consumes binlogs.

Mitigation

Disable the optimization before starting replication:

SET GLOBAL DML_USE_RETURNING = FALSE;
Note

Disabling this optimization may reduce the performance of INSERT IGNORE on tables with GSIs, which can affect data import throughput. Evaluate your workload before making this change.

Root cause

With RETURNING optimization enabled, INSERT IGNORE inserts data without checking for uniqueness, then deletes duplicate rows. This produces the following binlog entries:

INSERT INTO t VALUES(1,2,2);
DELETE FROM t WHERE pk = 1 AND sk = 2;

When MySQL consumes these entries, the INSERT statement triggers a unique key conflict on the uk column.

Reproduce the issue

  1. Create a table with a unique global secondary index (UGSI):

    CREATE TABLE t(
      pk INT,
      sk INT,
      uk INT,
      PRIMARY KEY(pk),
      UNIQUE GLOBAL INDEX g_i_k(uk) PARTITION BY uk
    ) PARTITION BY sk;
  2. Insert data:

    INSERT INTO t VALUES (1,1,1),(2,2,2);
    INSERT IGNORE INTO t VALUES (1,2,2);
  3. Observe the binlog entries generated (shown above). MySQL reports a unique key conflict on uk when consuming these entries.

Multi-stream binary logging

When multi-stream binary logging is enabled on PolarDB-X, create a separate replication link for each binary log stream. You can either:

  • Configure multiple replication links on a single MySQL instance, or

  • Dedicate a separate MySQL secondary instance to each binary log stream

Note

Broadcast replication via DDL statements is not straightforward when multiple replication links share a single MySQL instance.

Create a replication link

Run CHANGE MASTER TO on the MySQL secondary to establish a replication link pointing to PolarDB-X.

Syntax

CHANGE MASTER TO option [, option] ... [ channel_option ]

option: {
    MASTER_HOST = 'host_name'
  | MASTER_USER = 'user_name'
  | MASTER_PASSWORD = 'password'
  | MASTER_PORT = port_num
  | MASTER_LOG_FILE = 'source_log_name'
  | MASTER_LOG_POS = source_log_pos
  | MASTER_LOG_TIME_SECOND = source_log_time
  | SOURCE_HOST_TYPE = {RDS|POLARDBX|MYSQL}
  | STREAM_GROUP = 'stream_group_name'
  | WRITE_SERVER_ID = write_server_id
  | TRIGGER_AUTO_POSITION = {FALSE|TRUE}
  | WRITE_TYPE = {SPLIT|SERIAL|TRANSACTION}
  | MODE = {INCREMENTAL|IMAGE}
  | CONFLICT_STRATEGY = {OVERWRITE|INTERRUPT|IGNORE|DIRECT_OVERWRITE}
  | IGNORE_SERVER_IDS = (server_id_list)
}

channel_option:
    FOR CHANNEL channel

server_id_list:
    [server_id [, server_id] ... ]

Parameters

Connection

ParameterDescriptionDefault
MASTER_HOSTAddress of the PolarDB-X primary instance
MASTER_USERUsername to connect to the primary instance
MASTER_PASSWORDPassword to connect to the primary instance
MASTER_PORTPort to connect to the primary instance
SOURCE_HOST_TYPEType of the primary instance. Set to POLARDBX for a PolarDB-X 2.0 instance.MYSQL

Replication position

ParameterDescriptionDefault
MASTER_LOG_FILEName of the binlog file on the primary instance
MASTER_LOG_POSByte offset in the binlog file where replication starts
MASTER_LOG_TIME_SECONDUNIX timestamp (seconds since 1970-01-01 00:00:00 UTC) from which replication starts. Ignored if MASTER_LOG_FILE and MASTER_LOG_POS are set.
TRIGGER_AUTO_POSITIONSpecifies whether to automatically start from the latest offset on the primary instance.FALSE

Behavior

ParameterDescriptionDefault
WRITE_TYPEWrite mode. Valid values: SPLIT (non-transactional parallel), SERIAL (non-transactional serial), TRANSACTION (transactional serial).SPLIT
MODESynchronization scope. Valid values: INCREMENTAL (incremental data only), IMAGE (schema + full data + incremental data).
CONFLICT_STRATEGYConflict policy. Valid values: OVERWRITE (run REPLACE INTO), INTERRUPT (stop replication), IGNORE (skip the conflicting row), DIRECT_OVERWRITE.OVERWRITE
STREAM_GROUPStream group name for multi-stream binlog. Required when using the multi-stream binlog service on a PolarDB-X 2.0 primary instance.
WRITE_SERVER_IDID of the write server.
IGNORE_SERVER_IDSList of server IDs to exclude from replication.

Channel

ParameterDescription
FOR CHANNEL channelName of the replication link. Must be unique across existing link names.

Examples

Example 1: Start from a specific timestamp

CHANGE MASTER TO
MASTER_HOST='pxc-YOURENDPOINT.com',
MASTER_PORT=3306,
MASTER_USER='polardbx',
MASTER_PASSWORD='123456',
SOURCE_HOST_TYPE=polardbx,
MASTER_LOG_TIME_SECOND=1713159938,
write_server_id=1944903859
FOR CHANNEL 'pxc-bjrcsnbyagcdxh';

This starts replication from 2024-04-15 13:45:38 UTC (UNIX timestamp 1713159938).

Example 2: Start from a specific binlog file and offset

CHANGE MASTER TO
MASTER_HOST='pxc-YOURENDPOINT.com',
MASTER_PORT=3306,
MASTER_USER='polardbx',
MASTER_PASSWORD='123456',
SOURCE_HOST_TYPE=polardbx,
MASTER_LOG_FILE='binlog.000001',
MASTER_LOG_POS=4,
write_server_id=1944903859
FOR CHANNEL 'pxc-bjrcsnbyagcdxh';

This starts replication from offset 4 in binlog.000001.

Note

For the full parameter reference, see CHANGE MASTER TO.

Start a replication link

Syntax

START SLAVE [ channel_option ]

channel_option:
    FOR CHANNEL channel

Examples

Start all replication links:

START SLAVE;

Start a specific replication link:

START SLAVE FOR CHANNEL 'TEST';
Note

For the full reference, see START SLAVE.

Stop a replication link

Syntax

STOP SLAVE [ channel_option ]

channel_option:
    FOR CHANNEL channel

Examples

Stop all replication links:

STOP SLAVE;

Stop a specific replication link:

STOP SLAVE FOR CHANNEL 'TEST';
Note

For the full reference, see STOP SLAVE.

Delete a replication link

Important

Stop the replication link before deleting it.

Syntax

RESET SLAVE ALL [ channel_option ]

channel_option:
    FOR CHANNEL channel

Examples

Delete all replication links:

RESET SLAVE ALL;

Delete a specific replication link:

RESET SLAVE ALL FOR CHANNEL 'TEST';
Note

For the full reference, see RESET SLAVE ALL.