

# Moving a collocation

You can move a collocation from one member cluster to another in a database cluster group for Aurora by running a data movement job. To understand how a move works, see [Moving data between member clusters](databaseclustergroups-data-movement.md). To see what a job moves and what can block it, see [Data movement considerations](databaseclustergroups-move-considerations.md).

## Before you begin
<a name="databaseclustergroups-running-before-you-begin"></a>

Complete the following before you start a move.

### Requirements
<a name="databaseclustergroups-running-requirements"></a>

Make sure that these conditions hold:
+ Routing is enabled for the database cluster group for Aurora, and you can connect to the routing endpoint. See [Finding the routing endpoint name](databaseclustergroups-connecting.md#databaseclustergroups-routing-endpoint-names).
+ Data mappings describe the objects that you move, and the data mappings for all of those objects share the same collocation. See [Data mappings and collocations](databaseclustergroups-data-mappings.md).
+ Your router user holds `dbclustergroup_move_admin`. The privilege is administrative in scope. It permits a move of any collocation between any member clusters in the group.
+ No other data movement job is active in the group.

**Note**  
A job moves at most 10,000 tables and 1 TiB of table data, indexes included. Plan collocations under these limits.

### Confirm the objects that will be moved
<a name="databaseclustergroups-running-confirm-objects"></a>

1. At the routing endpoint, read the data mappings of the collocation that you intend to move. See [Data mappings and collocations](databaseclustergroups-data-mappings.md).

   ```sql
   SELECT * FROM dbclustergroup_sys.data_mappings WHERE router_collocation_group = '{{COLLOCATION_NAME}}';
   ```

1. Check that the result lists only the objects that you intend to move. If it lists other objects, regroup them first. For each of them, set `router_collocation_group` and `member_cluster_alias` together in one `UPDATE`. Use the current alias to keep the data where it is.

1. Check that the objects are eligible to be moved. See [Data movement considerations](databaseclustergroups-move-considerations.md).

### Check for queries that go directly to the source member cluster
<a name="databaseclustergroups-running-direct-queries"></a>

Check whether any application sends queries directly to the moving objects on the source member cluster, instead of through the routing endpoint. If you want these queries to continue working, modify your application to send them through the routing endpoint before you start the move. For more information, see [Access to the moving objects](databaseclustergroups-data-movement.md#databaseclustergroups-data-movement-access).

### Prepare the destination member cluster
<a name="databaseclustergroups-running-prepare-destination"></a>

For a table mapping, make sure that the database that contains the table exists on the destination member cluster.

A job fails during the precheck if an object that it moves already exists on the destination member cluster.

### Confirm the users and roles that have access
<a name="databaseclustergroups-running-confirm-users"></a>

Identify the users and roles that have access to the moving objects on the source member cluster. Check whether those users and roles exist on the destination member cluster. To keep their access after the cutover, see [Preserving grants](#databaseclustergroups-preserving-grants).

### Back up the source member cluster and export the mappings (optional)
<a name="databaseclustergroups-running-backup"></a>

1. In the AWS CLI or the console, back up the source member cluster. The move drops the source copy after the cutover. See [Backing up and restoring a database cluster group for Aurora](databaseclustergroups-backups.md).

1. At the routing endpoint, export your data mappings so that you have a record of them from before the move. See [Exporting the mappings before and after a change](databaseclustergroups-changing-mappings.md#databaseclustergroups-export-mappings).

## Starting a data movement job
<a name="databaseclustergroups-moving-a-collocation-group-with-the-data-mover"></a>

**To move collocations**

1. Connect to the routing endpoint in a session that isn't pinned. Run the move statement with `EXPLAIN` in front of it, and read the result. `EXPLAIN` checks the move without starting a job, so a wrong collocation name or member cluster alias surfaces while the source is still authoritative. For the syntax and every option, see [EXPLAIN ALTER DBCLUSTERGROUP MOVE COLLOCATION](databaseclustergroups-sql-explain-move-collocation.md) and [ALTER DBCLUSTERGROUP MOVE COLLOCATION](databaseclustergroups-sql-move-collocation.md).

   ```sql
   EXPLAIN ALTER DBCLUSTERGROUP MOVE COLLOCATION {{COLLOCATION_NAME}} FROM {{SOURCE_CLUSTER}} TO {{TARGET_CLUSTER}};
   ```

1. At the routing endpoint, run the same statement without `EXPLAIN`, and note the job identifier that it returns in the `JOB_ID` column. You use the job identifier to monitor, cut over, and cancel the job.

   ```sql
   ALTER DBCLUSTERGROUP MOVE COLLOCATION {{COLLOCATION_NAME}} FROM {{SOURCE_CLUSTER}} TO {{TARGET_CLUSTER}};
   ```

   To choose when the cutover happens, or to keep users' grants, add the options. For example:

   ```sql
   ALTER DBCLUSTERGROUP MOVE COLLOCATION {{COLLOCATION_NAME}} FROM {{SOURCE_CLUSTER}} TO {{TARGET_CLUSTER}}
     MANUAL_CUTOVER = 1, PRESERVE_GRANTS_FOR = ('app_user');
   ```

1. At the routing endpoint, monitor the job by reading the job's row in the `move_info` view. See [Reading move status](#databaseclustergroups-move-monitoring).

1. At the routing endpoint, if you started the move with `MANUAL_CUTOVER = 1`, run the cutover when you are ready for it. See [To cut over manually](#databaseclustergroups-move-switch-manual).

1. At the routing endpoint, continue to monitor the job's row in the `move_info` view until `status` shows `Completed`, `Error`, or `Cancelled`. The job shows `Completed` after the cutover, `Error` if the job fails, and `Cancelled` if you cancel it.

## Preserving grants
<a name="databaseclustergroups-preserving-grants"></a>

After the cutover, the routing layer sends queries on the moved objects to the destination member cluster. A move doesn't automatically preserve grants on the moving objects. The destination member cluster refuses a user who lacks privileges on the moved objects, so a query that succeeded on the source member cluster might now fail. Either grant the users their permissions on the destination, or use the `PRESERVE_GRANTS_FOR` option of the move statement.
+ `PRESERVE_GRANTS_FOR` takes a comma-separated list of users and roles in parentheses, for example `PRESERVE_GRANTS_FOR = ('app_user', 'report_user')`.
+ Each account that you list must exist on both the source and the destination member cluster. A move copies only direct grants on the moving objects, not privileges that a user holds through role membership, so grant role memberships on the destination yourself.
+ The move fails if you list an account that has no direct grants on the moving objects.
+ Name each account as `'{{USER}}'@'{{HOST}}'`. An account named without a host is treated as `'{{USER}}'@'%'`, as in a MySQL `GRANT` statement.

## Reading move status
<a name="databaseclustergroups-move-monitoring"></a>

At the routing endpoint, read the job's row in the move information view, filtered on the job identifier.

```sql
SELECT job_id, status, phase, progress_details, errors, warnings, cleanup_status, updated_at
  FROM dbclustergroup_data_movement.move_info
  WHERE job_id = '{{JOB_ID}}';
```

For information on the output, see [move\_info](databaseclustergroups-sysviews-move-info.md).

### Amazon RDS events for a move
<a name="databaseclustergroups-move-events-group-events"></a>

The database cluster group for Aurora also emits Amazon RDS events for a move, with the source type `db-cluster-group`. To read them, run `aws rds describe-events --source-type db-cluster-group`. For more information, see [Amazon RDS events for database cluster groups for Aurora](databaseclustergroups-group-events.md).

## Cutting over
<a name="databaseclustergroups-move-switch"></a>

For information about automatic and manual cutover, see [Cutover](databaseclustergroups-data-movement.md#databaseclustergroups-data-movement-cutover-writes).

### To cut over manually
<a name="databaseclustergroups-move-switch-manual"></a>

1. At the routing endpoint, read the job's row in `move_info`.

1. Check that the job is in the CDC Replication phase. The replication lag is in `progress_details`.

1. At the routing endpoint, run the cutover statement.

   ```sql
   ALTER DBCLUSTERGROUP CUTOVER JOB '{{JOB_ID}}' MAX_REPLICATION_LAG_SECONDS = {{SECONDS}};
   ```

   `MAX_REPLICATION_LAG_SECONDS` is optional. The cutover command is rejected unless the replication lag is below this bound. The accepted range is 1 to 300 seconds, with a default of 10 seconds.

1. At the routing endpoint, read `move_info` until the job reads `Completed`. The statement returns when the routing layer records the request, not when the cutover ends.

**Important**  
A large `MAX_REPLICATION_LAG_SECONDS` value can lengthen the time that reads and writes to the moving objects are rejected.

A query on the moving objects during the cutover fails. For how to handle that, see [Cutover](databaseclustergroups-data-movement.md#databaseclustergroups-data-movement-cutover-writes).

## Canceling a move
<a name="databaseclustergroups-move-cancel"></a>

Cancel a move to abandon it before the cutover. After the cutover begins, the routing layer rejects the cancel. Wait for the job to end.

**To cancel a move**

1. At the routing endpoint, read the job's `status` and `phase` in `move_info`. Continue only if `status` reads `Running` or `Paused` and the phase comes before `CutoverInit`.

1. At the routing endpoint, run the cancel statement.

   ```sql
   ALTER DBCLUSTERGROUP CANCEL JOB '{{JOB_ID}}';
   ```

1. At the routing endpoint, read `move_info` again to confirm that the job reads `Cancelled`. The statement returns when the routing layer records the request, not when the cancel ends. The job can read `Cancelled` before the cleanup completes, so read `cleanup_status` to see whether cleanup has finished.

A cancel drops the objects that the move created on the destination member cluster, and leaves the source objects and the data mappings as they were. You can't cancel or cut over a job that has ended.

## Related pages
<a name="databaseclustergroups-running-a-move-related"></a>

For related tasks and concepts, see the following pages:
+ [Moving data between member clusters](databaseclustergroups-data-movement.md)
+ [Data movement considerations](databaseclustergroups-move-considerations.md)
+ [Data movement statements](databaseclustergroups-sql-data-movement.md)
+ [Troubleshooting data moves and schema changes](databaseclustergroups-ts-operations.md)