迁移到其他数据库(异构)
Oracle
# Oracle -> PostgreSQL: Ora2Pg (schema + data + PL/SQL conversion)
ora2pg -c ora2pg.conf -t TABLE -o schema.sql
ora2pg -c ora2pg.conf -t COPY -o data.sql # data in COPY format
ora2pg -t SHOW_REPORT --estimate_cost # migration cost estimate
# Oracle -> (multiple targets) managed/online: AWS DMS or Oracle GoldenGate从 Oracle 进行异构迁移(尤其是迁移到 PostgreSQL)时,最常用的开源工具是 Ora2Pg:它将表、约束、PL/SQL 和数据转换为目标方言;--estimate_cost 可估算迁移工作量。在线/低停机迁移使用 AWS DMS 或 Oracle GoldenGate(配合 CDC)。
PostgreSQL
# MySQL -> PostgreSQL: pgloader (schema+data+type conversion in one command)
pgloader mysql://app_user:password@source/shop \
postgresql://app_user:password@target/shop
# Also from SQLite/MSSQL sources: with a pgloader ./migration.load script
# For a managed/online PostgreSQL target: AWS DMSpgloader 是从其他引擎(MySQL、SQLite、MS SQL)迁移到 PostgreSQL 的标准工具:只需连接字符串,一条命令即可创建 schema、映射类型并快速复制数据;自定义转换/类型映射在 .load 脚本中定义。如果源是 Oracle,推荐使用 Ora2Pg;在线迁移使用 AWS DMS。
MySQL
# PostgreSQL/Oracle/MSSQL -> MySQL: AWS DMS (managed) is the most common path
# or adapt the source-specific export for MySQL:
# pg_dump --inserts ... | (type/syntax fixes) | mysql shop
# MySQL <-> MariaDB (compatible): mysqldump output loads directly
mysqldump --single-transaction shop | mysql -h target_mariadb shop从其他引擎迁移到 MySQL 没有一个占主导地位的开源工具;最常见的方案是 AWS DMS(托管、支持 CDC),或调整源 dump 的语法/类型后用 mysql 客户端加载。MySQL 与 MariaDB 基本兼容;mysqldump 输出通常可直接迁移。
SQL Server
# SQL Server -> PostgreSQL: with pgloader
pgloader mssql://app_user:password@source/shop \
postgresql://app_user:password@target/shop
# SQL Server -> PostgreSQL compatibility layer: Babelfish for Aurora PostgreSQL
# (runs T-SQL and the TDS protocol on top of PostgreSQL)
# To other targets: SSMA (SQL Server Migration Assistant) or AWS DMS从 SQL Server 迁移到 PostgreSQL 使用 pgloader(mssql:// 源);如果希望保留 T-SQL 而不修改应用,Babelfish for Aurora PostgreSQL 提供兼容层。Microsoft 的 SSMA 工具可自动将其他引擎(Oracle、MySQL、DB2)迁移到 SQL Server。在线迁移使用 AWS DMS。
MongoDB
# Relational <-> MongoDB conversion requires schema mapping (ETL):
# - Relational Migrator (official MongoDB tool): maps tables to the document model
# - or generate JSON from the source -> load with mongoimport
mongoexport --db=shop --collection=customer --type=csv --fields=id,name > c.csv
# (To an SQL target: mongoexport CSV -> relational COPY/LOAD DATA)MongoDB 与 SQL 引擎之间的迁移需要行<->文档模型转换(ETL);没有自动的单一命令。从 SQL 到 MongoDB,MongoDB Relational Migrator 将表映射为文档 schema;简单情况下,从源生成 CSV/JSON 后用 mongoimport 加载。反方向则将 mongoexport 的输出用 COPY/LOAD DATA 加载到目标关系表。
Redis
# With RIOT (Redis Input/Output Tools), to/from Redis from other sources:
# Relational -> Redis:
riot db-import --jdbc-url "jdbc:postgresql://host/shop" \
--sql "SELECT * FROM customer" hset --keyspace customer --keys id
# Redis -> another Redis / file: riot replicate / riot file-export在异构迁移中,Redis 通常作为缓存层充当目标/源;要从关系型或文件源向 Redis 加载数据,使用 Redis 官方的 RIOT 工具(db-import 将 SQL 结果映射为 HSET/SET 结构)。Redis 到 Redis 或到文件,可使用 riot replicate / file-export 命令。
Elasticsearch
# Relational/other source -> Elasticsearch: Logstash (JDBC input)
# logstash.conf: input { jdbc { statement => "SELECT * FROM customer" } }
# output { elasticsearch { index => "customer" } }
# ES -> ES (version/cluster upgrade): reindex from a remote source
POST _reindex
{ "source": { "remote": { "host": "http://old:9200" }, "index": "customer" },
"dest": { "index": "customer" } }从其他数据库迁移到 Elasticsearch 最常用的工具是 Logstash(JDBC/file input -> elasticsearch output);Elastic 自己的 connectors/Beats 也很常用。从旧的 Elasticsearch 集群迁移到新集群时,常用 remote reindex(带 'remote' 源的 _reindex);目标上必须定义 reindex.remote.whitelist。