搭建持续复制 / CDC(零停机切换)
Oracle
# CDC-based replication with Oracle GoldenGate (outline)
# 1) Enable supplemental logging on the source:
ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;
# 2) Extract (capture changes from redo) and Replicat (apply to the target) processes:
# GGSCI> ADD EXTRACT ext1, INTEGRATED TRANLOG, BEGIN NOW
# GGSCI> ADD REPLICAT rep1, INTEGRATED
# Alternative: initial load with Data Pump + incremental capture with GoldenGate在 Oracle 中,零停机/异构 CDC 的标准工具是 Oracle GoldenGate:Extract 进程从重做日志捕获变更,Replicat 将其应用到目标;必须启用补充日志(supplemental logging)。典型迁移:先用 Data Pump 完成初始加载,此后由 GoldenGate 将变更流式传输到目标,实现近乎零停机的切换。
PostgreSQL
-- Source (publisher): create a publication (wal_level must be logical)
CREATE PUBLICATION migration_pub FOR ALL TABLES;
-- Target (subscriber): create a subscription (initial copy + continuous stream)
CREATE SUBSCRIPTION migration_sub
CONNECTION 'host=source dbname=shop user=repl password=***'
PUBLICATION migration_pub;PostgreSQL 通过内置的逻辑复制提供 CDC:在源上 CREATE PUBLICATION,在目标上 CREATE SUBSCRIPTION。订阅启动时会复制现有数据(initial copy),然后持续从 WAL 流式传输变更;要求 wal_level=logical。对于大型/复杂场景,推荐使用 pglogical 或 Debezium。
MySQL
-- Source: binlog must be enabled and in ROW format (log_bin, binlog_format=ROW)
-- Grant a replication user: GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
-- Define the source on the target (start from the binlog position of the initial load):
CHANGE REPLICATION SOURCE TO
SOURCE_HOST='source', SOURCE_USER='repl', SOURCE_PASSWORD='***',
SOURCE_LOG_FILE='binlog.000001', SOURCE_LOG_POS=1547;
START REPLICA;在 MySQL 中,零停机切换通过基于 binlog 的复制完成。初始加载用 mysqldump --source-data 获取并记录 binlog 位置/GTID;在目标上使用 CHANGE REPLICATION SOURCE TO(8.0.23+ 语法;原为 CHANGE MASTER TO)从该位置启动复制。START REPLICA 持续应用变更。基于 GTID 的配置更安全。
SQL Server
-- Option A: Transactional Replication (publisher->distributor->subscriber) — SSMS wizard
-- Option B: Change Data Capture (track changes as tables)
EXEC sys.sp_cdc_enable_db; -- enable CDC at database level
EXEC sys.sp_cdc_enable_table
@source_schema = N'dbo', @source_name = N'customer',
@role_name = NULL; -- enable CDC at table level
-- Changes are read from the cdc.dbo_customer_CT table and moved to the target.SQL Server 的持续复制有两条主要途径:如果需要直接流式传输到目标,配置事务复制(发布服务器/订阅服务器);如果要用自己的 ETL 迁移变更,启用变更数据捕获(sp_cdc_enable_db + sp_cdc_enable_table)并从 _CT 表读取。对于异构目标,CDC 可与 AWS DMS/Debezium 结合使用。
MongoDB
// Watch changes live with Change Streams (requires a replica set/sharded cluster)
const cs = db.customer.watch([], { fullDocument: "updateLookup" });
while (cs.hasNext()) { printjson(cs.next()); } // apply to the target
// Official tool for managed cluster-to-cluster replication/migration: mongosync
// mongosync --cluster0 "mongodb://source" --cluster1 "mongodb://target"在 MongoDB 中,CDC 使用 Change Streams:db.collection.watch() 从 oplog 实时流式传输 insert/update/delete 事件,供应用到目标(需要副本集)。对于完整的集群到集群持续迁移/复制,MongoDB 官方的 Cluster-to-Cluster Sync 工具 mongosync(不限于 Atlas;也适用于自管集群)可管理初始加载 + 持续同步。
Redis
# Make the target instance a replica of the source (live synchronization)
# On the target Redis:
REPLICAOF source_host 6379
# Monitor sync status:
INFO replication # master_link_status must be up
# Once the cutover is complete, make the target standalone:
REPLICAOF NO ONE在 Redis 中,零停机切换通过原生复制完成:在目标服务器上执行 REPLICAOF <source host port>(原为 SLAVEOF);目标先接收完整的 RDB,然后通过命令流保持持续同步。用 INFO replication 确认 master_link_status:up,切换完成后用 REPLICAOF NO ONE 将目标设为独立实例。
Elasticsearch
# Incremental reindex from the old cluster to the new one within the same version family
POST _reindex?wait_for_completion=false
{ "source": { "remote": { "host": "http://old:9200" }, "index": "customer",
"query": { "range": { "updated_at": { "gt": "<last-cursor>" } } } },
"dest": { "index": "customer" } }
# Licensed feature: Cross-Cluster Replication (CCR) — continuous following
# On the target (follower) cluster: PUT /customer/_ccr/follow { "remote_cluster": "old", "leader_index": "customer" }Elasticsearch 中真正的持续复制功能是 Cross-Cluster Replication(CCR):目标集群跟随源上的 leader 索引并持续拉取变更(需要商业/Platinum 许可)。没有许可时,可通过定期 remote reindex(基于时间/序列字段的增量方式)实现近乎持续的同步。