

# 将数据从 RDS for MySQL DB 数据库实例迁移到 Amazon Aurora MySQL 数据库集群
<a name="AuroraMySQL.Migrating.RDSMySQL"></a>

您可以将数据直接从 RDS for MySQL 数据库实例迁移（复制）到 Amazon Aurora MySQL 数据库集群。

**Topics**
+ [将 RDS for MySQL 快照迁移到 Aurora](AuroraMySQL.Migrating.RDSMySQL.Snapshot.md)
+ [使用 Aurora 只读副本将数据从 RDS for MySQL 数据库实例迁移到 Amazon Aurora MySQL 数据库集群](AuroraMySQL.Migrating.RDSMySQL.Replica.md)

**注意**  
由于 Amazon Aurora MySQL 与 MySQL 兼容，因此，您可以在 MySQL 数据库和 Amazon Aurora MySQL 数据库集群之间设置复制以迁移 MySQL 数据库中的数据。有关更多信息，请参阅 [使用 Amazon Aurora 进行复制](Aurora.Replication.md)。

# 将 RDS for MySQL 快照迁移到 Aurora
<a name="AuroraMySQL.Migrating.RDSMySQL.Snapshot"></a>

您可以迁移 RDS for MySQL 数据库实例的数据库快照来创建 Aurora MySQL 数据库集群。将使用原始 RDS for MySQL 数据库实例中的数据填充新的 Aurora MySQL 数据库集群。必须已从运行与 Aurora MySQL 兼容的 MySQL 版本的 Amazon RDS 数据库实例中创建数据库快照。

手动数据库快照和自动数据库快照都可以迁移。创建数据库集群后，您可创建可选 Aurora 副本。

**注意**  
您还可以通过创建源 RDS for MySQL 数据库实例的 Aurora 只读副本，将 RDS for MySQL 数据库实例迁移到 Aurora MySQL 数据库集群。有关更多信息，请参阅 [使用 Aurora 只读副本将数据从 RDS for MySQL 数据库实例迁移到 Amazon Aurora MySQL 数据库集群](AuroraMySQL.Migrating.RDSMySQL.Replica.md)。  
您无法从一些较早的 MySQL 8.0 版本（包括 8.0.11、8.0.13 和 8.0.15）迁移到 Aurora MySQL 版本 3.05 及更高版本。我们建议您在迁移之前先升级到 MySQL 版本 8.0.28。

您必须采取的常见步骤如下：

1. 确定要为 Aurora MySQL 数据库集群配置的空间量。有关更多信息，请参阅“[需要多少空间？](#AuroraMySQL.Migrating.RDSMySQL.Space)”

1. 使用控制台在 Amazon RDS MySQL 实例所在的AWS区域中创建快照。有关创建数据库快照的信息，请参阅[创建数据库快照](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_CreateSnapshot.html)。

1. 如果数据库快照不在数据库集群所在的AWS区域中，请使用 Amazon RDS 控制台将数据库快照复制到该AWS区域。有关复制数据库快照的信息，请参阅[复制数据库快照](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_CopySnapshot.html)。

1. 使用控制台迁移数据库快照，并创建包含的数据库与原始 MySQL 数据库实例相同的 Aurora MySQL 数据库集群。

**警告**  
Amazon RDS 只允许每个AWS账户一次对应于每个AWS区域中的一个快照副本。

## 需要多少空间？
<a name="AuroraMySQL.Migrating.RDSMySQL.Space"></a>

将 MySQL 数据库实例的快照迁移到 Aurora MySQL 数据库集群时，Aurora 将先使用 Amazon Elastic Block Store (Amazon EBS) 卷设置快照中数据的格式，然后再迁移数据。在某些情况下，为迁移设置数据格式操作需要额外的空间。

MyISAM 之外的未压缩的表的大小可高达 16 TB。如果具有 MyISAM 表，则 Aurora 必须使用卷中的额外空间转换这些表以使其与 Aurora MySQL 兼容。如果有压缩表，则 Aurora 必须使用卷中的额外空间将这些表解压缩，然后再将它们存储在 Aurora 集群卷中。由于此额外的空间要求，您应确保从 MySQL 数据库实例迁移的 MyISAM 表和压缩表的大小不会超出 8 TB。

## 减少将数据迁移到 Amazon Aurora MySQL 中所需的空间量
<a name="AuroraMySQL.Migrating.RDSMySQL.PreImport"></a>

您可能需要先修改数据库架构，然后再将其迁移到 Amazon Aurora 中。这种修改在以下情况下会很有用：
+ 您需要加快迁移过程。
+ 您不确定需要配置的空间量。
+ 您已尝试迁移数据，但迁移因配置的空间不足而失败。

您可进行以下更改来改善将数据库迁移到 Amazon Aurora 的过程。

**重要**  
请务必对从生产数据库快照还原的新数据库实例而非生产实例执行这些更新。然后，您可以将新数据库实例快照中的数据迁移到 Aurora 数据库集群，以避免生产数据库发生任何服务中断。


| 表类型 | 限制或指南 | 
| --- | --- | 
|  MyISAM 表  |  Aurora MySQL 仅支持 InnoDB 表。如果您的数据库中有 MyISAM 表，则必须先转换这些表，然后才能将其迁移到 Aurora MySQL 中。迁移过程中，转换过程需要额外的空间以便将 MyISAM 转换为 InnoDB。 若要降低空间用尽的可能性或加快迁移过程，请先将所有 MyISAM 表转换为 InnoDB 表，然后再迁移这些表。生成的 InnoDB 表的大小与 Aurora MySQL 要求该表具有的大小相同。要将 MyISAM 表转换为 InnoDB 表，请运行以下命令： `alter table <schema>.<table_name> engine=innodb, algorithm=copy;`   | 
|  压缩表  |  Aurora MySQL 不支持压缩的表（即，使用 `ROW_FORMAT=COMPRESSED` 创建的表）。 为了避免用完空间或为了加速迁移过程，请通过将 `ROW_FORMAT` 设置为 `DEFAULT`、`COMPACT`、`DYNAMIC` 或 `REDUNDANT` 来扩展您的压缩表。有关更多信息，请参阅 MySQL 文档中的 [InnoDB 行格式](https://dev.mysql.com/doc/refman/8.0/en/innodb-row-format.html)。  | 

您可以在现有 MySQL 数据库实例上使用以下 SQL 脚本来列出数据库中属于 MyISAM 表或压缩表的表。

```
-- This script examines a MySQL database for conditions that block
-- migrating the database into Amazon Aurora.
-- It needs to be run from an account that has read permission for the
-- INFORMATION_SCHEMA database.

-- Verify that this is a supported version of MySQL.

select msg as `==> Checking current version of MySQL.`
from
  (
  select
    'This script should be run on MySQL version 5.6 or higher. ' +
    'Earlier versions are not supported.' as msg,
    cast(substring_index(version(), '.', 1) as unsigned) * 100 +
      cast(substring_index(substring_index(version(), '.', 2), '.', -1)
      as unsigned)
    as major_minor
  ) as T
where major_minor <> 506;


-- List MyISAM and compressed tables. Include the table size.

select concat(TABLE_SCHEMA, '.', TABLE_NAME) as `==> MyISAM or Compressed Tables`,
round(((data_length + index_length) / 1024 / 1024), 2) "Approx size (MB)"
from INFORMATION_SCHEMA.TABLES
where
  ENGINE <> 'InnoDB'
  and
  (
    -- User tables
    TABLE_SCHEMA not in ('mysql', 'performance_schema',
                         'information_schema')
    or
    -- Non-standard system tables
    (
      TABLE_SCHEMA = 'mysql' and TABLE_NAME not in
        (
          'columns_priv', 'db', 'event', 'func', 'general_log',
          'help_category', 'help_keyword', 'help_relation',
          'help_topic', 'host', 'ndb_binlog_index', 'plugin',
          'proc', 'procs_priv', 'proxies_priv', 'servers', 'slow_log',
          'tables_priv', 'time_zone', 'time_zone_leap_second',
          'time_zone_name', 'time_zone_transition',
          'time_zone_transition_type', 'user'
        )
    )
  )
  or
  (
    -- Compressed tables
       ROW_FORMAT = 'Compressed'
  );
```

脚本会生成类似于以下示例的输出。该示例显示了两个必须从 MyISAM 转换为 InnoDB 的表。输出还包括每个表的相应大小（以 MB 为单位）。

```
+---------------------------------+------------------+
| ==> MyISAM or Compressed Tables | Approx size (MB) |
+---------------------------------+------------------+
| test.name_table                 |          2102.25 |
| test.my_table                   |            65.25 |
+---------------------------------+------------------+
2 rows in set (0.01 sec)
```

## 将 RDS for MySQL 数据库快照迁移到 Aurora MySQL 数据库集群
<a name="migrate-snapshot-ams-cluster"></a>

您可以使用 AWS 管理控制台或 AWS CLI 迁移 RDS for MySQL 数据库实例的数据库快照来创建 Aurora MySQL 数据库集群。将使用原始 RDS for MySQL 数据库实例中的数据填充新的 Aurora MySQL 数据库集群。有关创建数据库快照的信息，请参阅[创建数据库快照](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_CreateSnapshot.html)。

如果数据库快照不在要从中找到数据的 AWS 区域中，请将数据库快照复制到该 AWS 区域。有关复制数据库快照的信息，请参阅[复制数据库快照](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_CopySnapshot.html)。

### 控制台
<a name="AuroraMySQL.Migrating.RDSMySQL.Import.Console"></a>

使用 AWS 管理控制台迁移数据库快照时，控制台将执行必要的操作来仅创建数据库集群。

您还可以选择使用 AWS KMS key 静态加密新的 Aurora MySQL DB 数据库集群。

**使用 AWS 管理控制台 迁移 MySQL 数据库快照**

1. 登录AWS 管理控制台并通过以下网址打开 Amazon RDS 控制台：[https://console.aws.amazon.com/rds/](https://console.aws.amazon.com/rds/)。

1. 从 MySQL 数据库实例或快照中开始迁移：

   要从数据库实例中开始迁移，请执行以下操作：

   1. 在导航窗格中，选择 **Databases (数据库)**，然后选择 MySQL 数据库实例。

   1. 对于 **Actions (操作)**，选择 **Migrate latest snapshot (迁移最新快照)**。

   要从快照中开始迁移，请执行以下操作：

   1. 选择 **Snapshots**。

   1. 在 **Snapshots** 页面上，选择要迁移到 Aurora MySQL 数据库集群的快照。

   1. 选择**快照操作**，然后选择**迁移快照**。

   将显示**迁移数据库**页。

1. 在 **Migrate Database** 页面上设置以下值：
   + **迁移到数据库引擎**：选择 `aurora`。
   + **数据库引擎版本**：选择 Aurora MySQL 数据库集群的数据库引擎版本。
   + **数据库实例类**：选择具有数据库所需的存储和容量的数据库实例类，例如 `db.r3.large`。Aurora 集群卷随着数据库中数据量的增加自动增大。Aurora 集群卷可增大到最大大小 128 tebibytes (TiB)。因此，您只需选择满足当前存储要求的数据库实例类。有关更多信息，请参阅“[Amazon Aurora 存储概述](Aurora.Overview.StorageReliability.md#Aurora.Overview.Storage)”。
   + **DB Instance Identifier**（数据库实例标识符）：为数据库集群键入一个名称，该名称在您选择的 AWS 区域中对于您的账户是唯一的。此标识符将在数据库集群中实例的终端节点地址中使用。您可选择对该名称进行一些巧妙处理，例如将所选的 AWS 区域和数据库引擎包括在名称中（如 **aurora-cluster1**）。

     数据库实例标识符具有以下限制：
     + 它必须包含 1 到 63 个字母数字字符或连字符。
     + 它的第一个字符必须是字母。
     + 它不能以连字符结束或包含两个连续连字符。
     + 对于每个 AWS 区域的每个 AWS 账户的所有数据库实例必须是唯一的。
   + **Virtual Private Cloud (VPC)**：如果已具有 VPC，您可以选择 VPC 标识符（如 `vpc-a464d1c1`）以将该 VPC 用于 Aurora MySQL 数据库集群。有关创建 VPC 的信息，请参阅[教程：创建 VPC 以用于数据库集群（仅限 IPv4）](CHAP_Tutorials.WebServerDB.CreateVPC.md)。

     否则，可以通过选择 **Create a new VPC (新建 VPC)**，让 Aurora 为您创建 VPC。
   + **DB subnet group**（数据库子网组）：如果已具有子网组，您可以选择子网组标识符（如 `gs-subnet-group1`）以将该子网组用于 Aurora MySQL 数据库集群。

     否则，可以通过选择 **Create a new subnet group (新建子网组)**，让 Aurora 为您创建子网组。
   + **公有可访问性**：选择**否**以指定仅 VPC 中的资源可以访问数据库集群中的实例。选择 **Yes** 可指定数据库集群中的实例可以由公用网络上的资源访问。默认值为 **Yes**。
**注意**  
您的生产数据库集群可能不需要位于公有子网中，因为仅应用程序服务器将需要访问数据库集群。如果数据库集群不需要位于公有子网中，请将 **Publicly Accessible** 设置为 **No**。
   + **可用区**：选择可用区以托管 Aurora MySQL 数据库集群的主实例。要让 Aurora 选择可用区，请选择 **No Preference (无首选项)**。
   + **数据库端口**：键入在连接到 Aurora MySQL 数据库集群中的实例时使用的默认端口。默认为 `3306`。
**注意**  
您可能位于企业防火墙后面，该防火墙不允许访问默认端口 (例如，MySQL 默认端口 3306)。在此情况下，请提供企业防火墙允许的端口值。请记住此端口值，以便在稍后连接到 Aurora MySQL 数据库集群时使用。
   + **加密**：为要静态加密的新 Aurora MySQL 数据库集群选择**启用加密**。如果选择**启用加密**，您必须选择一个 KMS 密钥以作为 **AWS KMS key** 值。

     如果数据库快照未加密，请指定一个加密密钥来对数据库集群进行静态加密。

     如果数据库快照已加密，请指定一个加密密钥，以便使用指定的加密密钥对您的数据库集群进行静态加密。您可以指定数据库快照使用的加密密钥或其他密钥。您无法从加密的数据库快照创建未加密的数据库集群。
   + **自动次要版本升级**：该设置不适用于 Aurora MySQL 数据库集群。

     有关 Aurora MySQL 引擎更新的更多信息，请参阅[Amazon Aurora MySQL 的数据库引擎更新Amazon Aurora MySQL 的长期支持（LTS）版本和测试版](AuroraMySQL.Updates.md)。

1. 选择 **Migrate** 以迁移您的数据库快照。

1. 选择 **Instances**，然后选择箭头图标以显示数据库集群详细信息并监控迁移的进度。在详细信息页面上，您可以找到用于连接到数据库集群的主实例的集群终端节点。有关连接到 Aurora MySQL 数据库集群的更多信息，请参阅[连接到 Amazon Aurora 数据库集群](Aurora.Connecting.md)。

### AWS CLI
<a name="USER_ImportAuroraCluster.CLI"></a>

您可以使用包含以下参数的 [https://docs.aws.amazon.com/cli/latest/reference/rds/restore-db-cluster-from-snapshot.html](https://docs.aws.amazon.com/cli/latest/reference/rds/restore-db-cluster-from-snapshot.html) 命令从 RDS for MySQL 数据库实例的数据库快照创建 Aurora 数据库集群：
+ `--db-cluster-identifier` – 要创建的数据库集群的名称。
+ `--engine aurora-mysql` – 适用于 MySQL 5.7 兼容或 8.0 兼容的数据库集群。
+ `--kms-key-id` – 用于选择性加密数据库集群的 AWS KMS key，具体取决于是否加密了数据库快照。
  + 如果数据库快照未加密，请指定一个加密密钥来对数据库集群进行静态加密。否则，不会对您的数据库集群进行加密。
  + 如果数据库快照已加密，请指定一个加密密钥，以便使用指定的加密密钥对您的数据库集群进行静态加密。否则，您的数据库集群将使用数据库快照的加密密钥进行静态加密。
**注意**  
您无法从加密的数据库快照创建未加密的数据库集群。
+ `--snapshot-identifier`：待迁移数据库快照的 Amazon Resource Name (ARN)。有关 Amazon RDS ARN 的更多信息，请参阅 [Amazon Relational Database Service (Amazon RDS)](https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html#arn-syntax-rds)。

在使用 `RestoreDBClusterFromSnapshot` 命令迁移数据库快照时，该命令将创建数据库集群和主实例。

在该示例中，您从将 ARN 设置为 *mydbsnapshotARN* 的数据库快照创建一个名为 *mydbcluster* 的 MySQL 5.7 兼容数据库集群。

对于 Linux、macOS 或 Unix：

```
aws rds restore-db-cluster-from-snapshot \
    --db-cluster-identifier mydbcluster \
    --snapshot-identifier mydbsnapshotARN \
    --engine aurora-mysql
```

对于：Windows

```
aws rds restore-db-cluster-from-snapshot ^
    --db-cluster-identifier mydbcluster ^
    --snapshot-identifier mydbsnapshotARN ^
    --engine aurora-mysql
```

在该示例中，您从将 ARN 设置为 *mydbsnapshotARN* 的数据库快照创建一个名为 *mydbcluster* 的 MySQL 5.7 兼容数据库集群。

对于 Linux、macOS 或 Unix：

```
aws rds restore-db-cluster-from-snapshot \
    --db-cluster-identifier mydbcluster \
    --snapshot-identifier mydbsnapshotARN \
    --engine aurora-mysql
```

对于：Windows

```
aws rds restore-db-cluster-from-snapshot ^
    --db-cluster-identifier mydbcluster ^
    --snapshot-identifier mydbsnapshotARN ^
    --engine aurora-mysql
```

# 使用 Aurora 只读副本将数据从 RDS for MySQL 数据库实例迁移到 Amazon Aurora MySQL 数据库集群
<a name="AuroraMySQL.Migrating.RDSMySQL.Replica"></a>

Aurora 使用 MySQL 数据库引擎的二进制日志复制功能，为源 RDS for MySQL 数据库实例创建一个特殊类型的数据库集群，称为 Aurora 只读副本。对源 RDS for MySQL 数据库实例的更新将异步复制到 Aurora 只读副本。

我们建议您创建源 RDS for MySQL 数据库实例的 Aurora 只读副本，以使用该功能从 RDS for MySQL 数据库实例迁移到 Aurora MySQL 数据库集群。如果 RDS for MySQL 数据库实例和 Aurora 只读副本之间的副本滞后为 0，您可以将客户端应用程序引导到 Aurora 只读副本，然后停止复制以使 Aurora 只读副本成为独立的 Aurora MySQL 数据库集群。准备迁移需要花费一段时间，每 TiB 数据大约需要几小时。

有关 Aurora 可用的区域的列表，请参阅《AWS 一般参考》**中的 [Amazon Aurora](https://docs.aws.amazon.com/general/latest/gr/rande.html#aurora)。

在创建 RDS for MySQL 数据库实例的 Aurora 只读副本时，Amazon RDS 为源 RDS for MySQL 数据库实例创建数据库快照（Amazon RDS 私有，不产生费用）。Amazon RDS 之后会将数据从数据库快照迁移到 Aurora 只读副本。在将数据从数据库快照迁移到新的 Aurora MySQL 数据库集群后，Amazon RDS 开始在 RDS for MySQL 数据库实例和 Aurora MySQL 数据库集群之间进行复制。如果 RDS for MySQL 数据库实例中包含的表使用 InnoDB 之外的存储引擎，或者使用压缩行格式，您可以在创建 Aurora 只读副本之前更改这些表以使用 InnoDB 存储引擎和动态行格式，从而加快创建 Aurora 只读副本的过程。有关将 MySQL 数据库快照复制到 Aurora MySQL 数据库集群的过程的更多信息，请参阅[将数据从 RDS for MySQL DB 数据库实例迁移到 Amazon Aurora MySQL 数据库集群](AuroraMySQL.Migrating.RDSMySQL.md)。

一个 RDS for MySQL 数据库实例只能有一个 Aurora 只读副本。

**注意**  
由于 Aurora MySQL 和作为复制主实例的 RDS for MySQL 数据库实例的 MySQL 数据库引擎版本之间的特征不同，可能导致复制问题。如果遇到错误，您可以在 [Amazon RDS 社群论坛](https://forums.aws.amazon.com/forum.jspa?forumID=60)或联系 AWS 支持 以获得帮助。  
如果您的 RDS for MySQL 数据库实例已经是跨区域只读副本的来源，则无法创建 Aurora 只读副本。  
您无法从一些较早的 RDS for MySQL 8.0 版本（包括 8.0.11、8.0.13 和 8.0.15）迁移到 Aurora MySQL 版本 3.05 及更高版本。我们建议您在迁移之前先升级到 RDS for MySQL 版本 8.0.28。

有关 MySQL 只读副本的更多信息，请参阅[使用 MariaDB、MySQL 和 PostgreSQL 数据库实例的只读副本](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_ReadRepl.html)。

## 创建 Aurora 只读副本
<a name="AuroraMySQL.Migrating.RDSMySQL.Replica.Create"></a>

您可以使用控制台、AWS CLI 或 RDS API 为 RDS for MySQL 数据库实例创建 Aurora 只读副本。

### 控制台
<a name="AuroraMySQL.Migrating.RDSMySQL.Replica.Create.Console"></a>

**从源 RDS for MySQL 数据库实例创建 Aurora 只读副本**

1. 登录AWS 管理控制台并通过以下网址打开 Amazon RDS 控制台：[https://console.aws.amazon.com/rds/](https://console.aws.amazon.com/rds/)。

1. 在导航窗格中，选择 **Databases (数据库)**。

1. 选择要作为 Aurora 只读副本源的 MySQL 数据库实例。

1. 对于**操作**，请选择**创建 Aurora 只读副本**。

1. 按照下表所述，选择 Aurora 只读副本要使用的数据库集群规格。    
<a name="aurora_read_replica_param_advice"></a>[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/AmazonRDS/latest/AuroraUserGuide/AuroraMySQL.Migrating.RDSMySQL.Replica.html)

1. 选择 **Create read replica (创建只读副本)**。

### AWS CLI
<a name="AuroraMySQL.Migrating.RDSMySQL.Replica.Create.CLI"></a>

要从源 RDS for MySQL 数据库实例中创建 Aurora 只读副本，请使用 [https://docs.aws.amazon.com/cli/latest/reference/rds/create-db-cluster.html](https://docs.aws.amazon.com/cli/latest/reference/rds/create-db-cluster.html) 和 [https://docs.aws.amazon.com/cli/latest/reference/rds/create-db-instance.html](https://docs.aws.amazon.com/cli/latest/reference/rds/create-db-instance.html) AWS CLI 命令创建新的 Aurora MySQL 数据库集群。当您调用 `create-db-cluster` 命令时，请加入 `--replication-source-identifier` 参数，识别源 MySQL 数据库实例的 Amazon Resource Name (ARN)。有关 Amazon RDS ARN 的更多信息，请参阅 [Amazon Relational Database Service (Amazon RDS)](https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html#arn-syntax-rds)。

不要指定主用户名、主密码或数据库名称，因为 Aurora 只读副本使用和源 MySQL 数据库实例相同的主用户名、主密码和数据库名称。

对于 Linux、macOS 或 Unix：

```
aws rds create-db-cluster --db-cluster-identifier sample-replica-cluster --engine aurora \
    --db-subnet-group-name mysubnetgroup --vpc-security-group-ids sg-c7e5b0d2 \
    --replication-source-identifier arn:aws:rds:us-west-2:123456789012:db:primary-mysql-instance
```

对于：Windows

```
aws rds create-db-cluster --db-cluster-identifier sample-replica-cluster --engine aurora ^
    --db-subnet-group-name mysubnetgroup --vpc-security-group-ids sg-c7e5b0d2 ^
    --replication-source-identifier arn:aws:rds:us-west-2:123456789012:db:primary-mysql-instance
```

如果您使用控制台创建 Aurora 只读副本，Aurora 将自动为您的数据库集群 Aurora 只读副本创建主实例。如果您使用 AWS CLI 创建 Aurora 只读副本，则必须明确为数据库集群创建主实例。主实例是在数据库集群中创建的第一个实例。

您可以使用带以下参数的 [https://docs.aws.amazon.com/cli/latest/reference/rds/create-db-instance.html](https://docs.aws.amazon.com/cli/latest/reference/rds/create-db-instance.html) AWS CLI 命令为数据库集群创建主实例。
+ `--db-cluster-identifier`

  数据库集群的名称。
+ `--db-instance-class`

  要用于主实例的数据库实例类的名称。
+ `--db-instance-identifier`

  主实例的名称。
+ `--engine aurora`

在该示例中，您将使用 *myinstanceclass* 中指定的数据库实例类为名为 *myreadreplicacluster* 的数据库集群创建一个名为 *myreadreplicainstance* 的主实例。

**Example**  
对于 Linux、macOS 或 Unix：  

```
aws rds create-db-instance \
    --db-cluster-identifier myreadreplicacluster \
    --db-instance-class myinstanceclass \
    --db-instance-identifier myreadreplicainstance \
    --engine aurora
```
对于：Windows  

```
aws rds create-db-instance ^
    --db-cluster-identifier myreadreplicacluster ^
    --db-instance-class myinstanceclass ^
    --db-instance-identifier myreadreplicainstance ^
    --engine aurora
```

### RDS API
<a name="Aurora.Migration.RDSMySQL.Create.API"></a>

要从源 RDS for MySQL 数据库实例中创建 Aurora 只读副本，请使用 [https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBCluster.html](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBCluster.html) 和 [https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBInstance.html](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBInstance.html) Amazon RDS API 命令创建新的 Aurora 数据库集群和主实例。不要指定主用户名、主密码或数据库名称，因为 Aurora 只读副本使用和源 RDS for MySQL 数据库实例相同的主用户名、主密码和数据库名称。

您可以使用带以下参数的 [https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBCluster.html](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBCluster.html) Amazon RDS API 命令从源 RDS for MySQL 数据库实例为 Aurora 只读副本创建新的 Aurora 数据库集群：
+ `DBClusterIdentifier`

  要创建的数据库集群的名称。
+ `DBSubnetGroupName`

  要与该数据库集群关联的数据库子网组的名称。
+ `Engine=aurora`
+ `KmsKeyId`

  用于选择性加密数据库集群的 AWS KMS key，具体取决于是否加密了 MySQL 数据库实例。
  + 如果 MySQL 数据库实例未加密，请指定一个加密密钥来对数据库集群进行静态加密。否则，将使用您的账户的默认加密密钥对数据库集群进行静态加密。
  + 如果 MySQL 数据库实例已加密，请指定一个加密密钥，以便使用指定的加密密钥对您的数据库集群进行静态加密。否则，将使用 MySQL 数据库实例的加密密钥对您的数据库集群进行静态加密。
**注意**  
您无法从已加密的 MySQL 数据库实例创建未加密的数据库集群。
+ `ReplicationSourceIdentifier`

  源 MySQL 数据库实例的 Amazon Resource Name (ARN)。有关 Amazon RDS ARN 的更多信息，请参阅 [Amazon Relational Database Service (Amazon RDS)](https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html#arn-syntax-rds)。
+ `VpcSecurityGroupIds`

  要与该数据库集群关联的 EC2 VPC 安全组列表。

在该示例中，您将从 ARN 设置为 *mysqlprimaryARN*、与名为 *mysubnetgroup* 的数据库子网组和名为 *mysecuritygroup* 的 VPC 安全组关联的源 MySQL 数据库实例创建一个名为 *myreadreplicacluster* 的数据库集群。

**Example**  

```
https://rds.us-east-1.amazonaws.com/
    ?Action=CreateDBCluster
    &DBClusterIdentifier=myreadreplicacluster
    &DBSubnetGroupName=mysubnetgroup
    &Engine=aurora
    &ReplicationSourceIdentifier=mysqlprimaryARN
    &SignatureMethod=HmacSHA256
    &SignatureVersion=4
    &Version=2014-10-31
    &VpcSecurityGroupIds=mysecuritygroup
    &X-Amz-Algorithm=AWS4-HMAC-SHA256
    &X-Amz-Credential=AKIADQKE4SARGYLE/20150927/us-east-1/rds/aws4_request
    &X-Amz-Date=20150927T164851Z
    &X-Amz-SignedHeaders=content-type;host;user-agent;x-amz-content-sha256;x-amz-date
    &X-Amz-Signature=6a8f4bd6a98f649c75ea04a6b3929ecc75ac09739588391cd7250f5280e716db
```

如果您使用控制台创建 Aurora 只读副本，Aurora 将自动为您的数据库集群 Aurora 只读副本创建主实例。如果您使用 AWS CLI 创建 Aurora 只读副本，则必须明确为数据库集群创建主实例。主实例是在数据库集群中创建的第一个实例。

您可以使用带以下参数的 [https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBInstance.html](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBInstance.html) Amazon RDS API 命令为数据库集群创建主实例：
+ `DBClusterIdentifier`

  数据库集群的名称。
+ `DBInstanceClass`

  要用于主实例的数据库实例类的名称。
+ `DBInstanceIdentifier`

  主实例的名称。
+ `Engine=aurora`

在该示例中，您将使用 *myinstanceclass* 中指定的数据库实例类为名为 *myreadreplicacluster* 的数据库集群创建一个名为 *myreadreplicainstance* 的主实例。

**Example**  

```
https://rds.us-east-1.amazonaws.com/
    ?Action=CreateDBInstance
    &DBClusterIdentifier=myreadreplicacluster
    &DBInstanceClass=myinstanceclass
    &DBInstanceIdentifier=myreadreplicainstance
    &Engine=aurora
    &SignatureMethod=HmacSHA256
    &SignatureVersion=4
    &Version=2014-09-01
    &X-Amz-Algorithm=AWS4-HMAC-SHA256
    &X-Amz-Credential=AKIADQKE4SARGYLE/20140424/us-east-1/rds/aws4_request
    &X-Amz-Date=20140424T194844Z
    &X-Amz-SignedHeaders=content-type;host;user-agent;x-amz-content-sha256;x-amz-date
    &X-Amz-Signature=bee4aabc750bf7dad0cd9e22b952bd6089d91e2a16592c2293e532eeaab8bc77
```

## 查看 Aurora 只读副本
<a name="AuroraMySQL.Migrating.RDSMySQL.Replica.View"></a>

您可以使用 AWS 管理控制台 或 AWS CLI 查看 Aurora 数据库集群的 MySQL 与 Aurora MySQL 的复制关系。

### 控制台
<a name="AuroraMySQL.Migrating.RDSMySQL.Replica.View.Console"></a>

**要查看 Aurora 只读副本的主 MySQL 数据库实例**

1. 登录AWS 管理控制台并通过以下网址打开 Amazon RDS 控制台：[https://console.aws.amazon.com/rds/](https://console.aws.amazon.com/rds/)。

1. 在导航窗格中，选择 **Databases (数据库)**。

1. 选择 Aurora 只读副本的数据库集群以显示其详细信息。主 MySQL 数据库实例信息位于 **Replication source**（复制源）字段中。  
![\[查看 MySQL 主数据库\]](http://docs.aws.amazon.com/zh_cn/AmazonRDS/latest/AuroraUserGuide/images/aurora-repl6.png)

### AWS CLI
<a name="AuroraMySQL.Migrating.RDSMySQL.Replica.View.CLI"></a>

要针对您的 Aurora MySQL 数据库集群，使用 AWS CLI 查看 MySQL 与 Aurora MySQL 的复制关系，请使用 [https://docs.aws.amazon.com/cli/latest/reference/rds/describe-db-clusters.html](https://docs.aws.amazon.com/cli/latest/reference/rds/describe-db-clusters.html) 和 [https://docs.aws.amazon.com/cli/latest/reference/rds/describe-db-instances.html](https://docs.aws.amazon.com/cli/latest/reference/rds/describe-db-instances.html) 命令。

要确定哪个 MySQL 数据库实例是主实例，请使用 [https://docs.aws.amazon.com/cli/latest/reference/rds/describe-db-clusters.html](https://docs.aws.amazon.com/cli/latest/reference/rds/describe-db-clusters.html) 并在 `--db-cluster-identifier` 选项中指定 Aurora 只读副本的集群标识符。请参阅作为复制主实例的数据库实例的 ARN 输出中的 `ReplicationSourceIdentifier` 元素。

要确定哪个数据库集群是 Aurora 只读副本，请使用 [https://docs.aws.amazon.com/cli/latest/reference/rds/describe-db-instances.html](https://docs.aws.amazon.com/cli/latest/reference/rds/describe-db-instances.html) 并在 `--db-instance-identifier` 选项中指定 MySQL 数据库实例的实例标识符。请参阅 Aurora 只读副本的数据库集群标识符输出中的 `ReadReplicaDBClusterIdentifiers` 元素。

**Example**  
对于 Linux、macOS 或 Unix：  

```
aws rds describe-db-clusters \
    --db-cluster-identifier myreadreplicacluster
```

```
aws rds describe-db-instances \
    --db-instance-identifier mysqlprimary
```
对于：Windows  

```
aws rds describe-db-clusters ^
    --db-cluster-identifier myreadreplicacluster
```

```
aws rds describe-db-instances ^
    --db-instance-identifier mysqlprimary
```

## 提升 Aurora 只读副本
<a name="AuroraMySQL.Migrating.RDSMySQL.Replica.Promote"></a>

迁移完成后，您可以使用 AWS 管理控制台或 AWS CLI 将 Aurora 只读副本提升为独立的数据库集群。

然后，您可以将客户端应用程序引导到 Aurora 只读副本的端点。有关 Aurora 终端节点的更多信息，请参阅[Amazon Aurora 端点连接](Aurora.Overview.Endpoints.md)。提升会很快完成，您可以在提升过程中读取和写入 Aurora 只读副本。但是在此期间您无法删除 MySQL 主数据库实例，或取消数据库实例和 Aurora 只读副本之间的关联。

在提升 Aurora 只读副本之前，终止写入到源 MySQL 数据库实例的所有事务，然后等待 Aurora 只读副本的副本滞后达到 0。您可以通过对 Aurora 只读副本调用 `SHOW SLAVE STATUS`（Aurora MySQL 版本 2）或 `SHOW REPLICA STATUS`（Aurora MySQL 版本 3）命令，以查看 Aurora 只读副本的副本滞后。检查 **Seconds behind master** 值。

写入主实例的事务终止，且副本滞后为 0 时，可以开始写入 Aurora 只读副本。如果在此之前写入到 Aurora 只读副本，并修改了在 MySQL 主实例上也进行修改的表，则可能会中断复制到 Aurora 的过程。如果发生此情况，您必须删除并重新创建 Aurora 只读副本。

### 控制台
<a name="AuroraMySQL.Migrating.RDSMySQL.Replica.Promote.Console"></a>

**将 Aurora 只读副本提升为 Aurora 数据库集群**

1. 登录 AWS 管理控制台 并通过以下网址打开 Amazon RDS 控制台：[https://console.aws.amazon.com/rds/](https://console.aws.amazon.com/rds/)。

1. 在导航窗格中，选择 **Databases (数据库)**。

1. 选择 Aurora 只读副本的数据库集群。

1. 对于**操作**，请选择**提升**。

1. 选择 **Promote Read Replica**（提升只读副本）。

升级后，请使用以下步骤确认提升已完成。

**要确认 Aurora 只读副本已提升**

1. 登录 AWS 管理控制台 并通过以下网址打开 Amazon RDS 控制台：[https://console.aws.amazon.com/rds/](https://console.aws.amazon.com/rds/)。

1. 在导航窗格中，选择 **Events**。

1. 在 **Events**（事件）页面上，验证您提升的集群是否存在 `Promoted Read Replica cluster to a stand-alone database cluster` 事件。

提升完成后，MySQL 主数据库实例和 Aurora 只读副本之间的关联会取消，如果您愿意，可以安全删除数据库实例。

### AWS CLI
<a name="AuroraMySQL.Migrating.RDSMySQL.Replica.Promote.CLI"></a>

要将 Aurora 只读副本提升为独立数据库集群，请使用 [https://docs.aws.amazon.com/cli/latest/reference/rds/promote-read-replica-db-cluster.html](https://docs.aws.amazon.com/cli/latest/reference/rds/promote-read-replica-db-cluster.html)AWS CLI 命令。

**Example**  
对于 Linux、macOS 或 Unix：  

```
aws rds promote-read-replica-db-cluster \
    --db-cluster-identifier myreadreplicacluster
```
对于：Windows  

```
aws rds promote-read-replica-db-cluster ^
    --db-cluster-identifier myreadreplicacluster
```