Changing data mappings
In a database cluster group for Aurora, you change data mappings with INSERT, UPDATE, and DELETE on dbclustergroup_sys.data_mappings at the routing endpoint. A mapping write moves no data. It changes only where the routing layer sends statements. To put data on another member cluster, see Moving data between member clusters.
Mapping changes are incremental, not transactional. A multi-row INSERT applies all of its rows or none, but BEGIN and COMMIT don't group separate mapping statements, and a failed mapping change doesn't roll back the earlier ones. Stage a large edit so that each intermediate state still routes: put each set of mappings that must appear together into one multi-row INSERT. For when a change reaches the routing layer, what an open transaction sees, and when to retry, see Applying data mapping changes. For the statement grammar, see Data mapping statements.
Before you change mappings
Before every data mapping change, check the following:
-
Connect to the routing endpoint in query routing mode, in a session that isn't pinned, as a router user that has
dbclustergroup_metadata_admin. The same privilege covers scaleout table and routing function definitions. A pinned session sends data mapping statements to the member cluster, where they fail. -
Run each data mapping write outside a transaction. The routing layer rejects a write to
dbclustergroup_sys.data_mappingsinside an open transaction. For how Aurora MySQL treats an open transaction, see the Aurora MySQL information after this list. -
Check that no data movement job of the collocation is running. While a move runs, and until its cleanup completes, the routing layer refuses a change to the mappings of the collocation that it moves. The statement fails with error
1205and the messageMetadata for collocation group '<NAME>' is being changed by another operation(orMetadata for data mapping '<NAME>' is being changed by another operation). Wait untilcleanup_statusreadsCompleteinmove_info, then run the statement again. Mappings of other collocations aren't affected. -
Copy each member cluster alias from
dbclustergroup_sys.member_cluster_mappings. Don't derive an alias from a DB cluster identifier or from a cluster's position in the group. Check that each member cluster alias that the change names appears in that listing. The routing layer rejects at theINSERTa mapping that names an alias that the listing doesn't show, with Aurora MySQL error64004(SQLSTATE42000). For the error text, see Error codes for database cluster groups for Aurora. -
Write each statement in the simple form that the routing layer accepts. A
WHEREclause takes one column equal to one value, or one tuple of columns equal to one tuple of values. The routing layer rejectsAND,OR,INlists, comparison operators other than=, and modifiers such asIGNORE,ON DUPLICATE KEY,REPLACE INTO,LIMIT, andORDER BY. Put every column that you filter on into one tuple. -
Export the data mappings, as described in the following section. The export gives you the member cluster aliases and cluster names from
dbclustergroup_sys.member_cluster_mappingsanddbclustergroup_sys.data_mappings.
Exporting the mappings before and after a change
No API restores the data mappings on their own. You export them and import them again at the routing endpoint. Export the data mappings before and after every data mapping change, move, schema change, upgrade, and restore, and keep the export as your undo.
To export the mappings
-
At the routing endpoint, read the data mappings, the member cluster mappings, and the definition of each scaleout table and routing function, as shown in the following tabs.
-
In your client, save the output to a file with your change record. Keep the database name next to each scaleout table definition. Sort the rows in your client, because the routing layer refuses
ORDER BYon its system tables. Read each table with oneSELECTand noLIMIT, so that the export is complete. -
After the change, export again at the routing endpoint, and compare the two files row by row.
SELECT * FROM dbclustergroup_sys.data_mappings; SELECT * FROM dbclustergroup_sys.member_cluster_mappings; SHOW SCALEOUT TABLES; SHOW SCALEOUT ROUTING FUNCTIONS; SHOW CREATE SCALEOUT TABLESCALEOUT_TABLE; -- once per scaleout table SHOW CREATE SCALEOUT ROUTING FUNCTIONROUTING_FUNCTION; -- once per routing function
Only the rows that you meant to add, change, or remove should differ. Any other difference means that someone else changed the data mappings at the same time or that a statement matched more rows than you meant. Find the cause before you continue.
To restore from the export, run one statement per row, outside a transaction:
-
A deleted row:
INSERTit back with every writable column, includingrouter_collocation_group. Leave outdb_cluster_identifier, which is read-only. -
A collocation on the wrong member cluster: repoint it back by
router_collocation_group, with the member cluster alias from the export. -
A wrong cluster name:
UPDATEthe row back.
Writes that the application made while a data mapping was wrong are on the member cluster that the data mapping named at the time. Find them by ID or timestamp, and copy them to where they belong.
Adding a mapping
Add a data mapping to place a database or a table on a member cluster. Give each data mapping a collocation in router_collocation_group. A row without one joins the member cluster's default collocation and moves with everything else in it.
To add a data mapping
-
On the member cluster, through a session pin or a direct connection, create the database or table. The routing layer doesn't check the data mapping against the member cluster. A data mapping that names a missing object registers without error, and the first routed statement that uses it fails.
-
At the routing endpoint, insert the rows. Put rows that must appear together into one multi-row
INSERT. -
If a write fails, stop. Read the data mapping table again, and fix the cause before you continue. A failed data mapping change doesn't roll back the earlier ones.
-
At the routing endpoint, read the rows back with
SELECT * FROM dbclustergroup_sys.data_mappings WHERE (router_db_name, router_table_name) = (', and run one routed read of each new name. For the read, see SELECT FROM dbclustergroup_sys.data_mappings.DB', 'TABLE')
While a database has a database-level mapping, the routing layer refuses a table-level mapping in it. To change the level, see Changing from database-level to table-level mappings.
Repointing mappings by collocation
A repoint sends a collocation's statements to another member cluster but copies no rows. Repoint only when every object in the collocation already has a correct, current copy on the target, for example because you copied the data yourself. Otherwise, move the collocation with data movement.
A member table of a scaleout table moves only with its whole collocation when the table has a routing function and the member is in a collocation that the function targets, either by a repoint of the collocation, as in the following procedure, or by data movement. The routing layer refuses an UPDATE that moves one member table to another collocation when that would break the scaleout table's placement. On Aurora MySQL, the statement fails with error 64024 when the target collocation already holds a member table of the same scaleout table. It fails with error 64065 when the UPDATE moves two member tables of one scaleout table into one collocation. It fails with error 64066 when the scaleout table would have no member table in a collocation that its routing function targets.
To repoint a collocation
-
In the application, stop writes to the collocation's objects. Then, over direct connections, copy what changed since your copy started. For details, see Moving data yourself.
-
Over direct connections, compare row counts and checksums on the source and the target. In your export, count the collocation's mapping rows.
-
At the routing endpoint, repoint every row of the collocation in one statement. Check that the number of rows affected equals the collocation's mapping row count.
-
At the routing endpoint, from a new connection, run one routed read of each object in the collocation. Then resume writes.
To repoint the collocation in step 3, run the following SQL UPDATE statement. It sets the member cluster alias of every mapping in the collocation.
UPDATE dbclustergroup_sys.data_mappings SET member_cluster_alias = 'TARGET_ALIAS' WHERE router_collocation_group = 'COLLOCATION_GROUP';
The WHERE clause follows the simple form described in Before you change mappings. For the full grammar and errors, see UPDATE dbclustergroup_sys.data_mappings.
To repoint one object that's alone in its collocation, set member_cluster_alias and router_collocation_group together in one UPDATE that matches exactly that row. The routing layer refuses a change to either column alone on a single mapping. To take one object out of a collocation that other objects share, set both columns together to a new collocation on the target member cluster, and move its data first.
To rename a collocation, set router_collocation_group in an UPDATE that matches the collocation by its current name, as in the following statement. The rename moves no data and keeps the collocation on its member cluster. The rows affected count is the number of mappings in the collocation. Routing functions that target the collocation follow the rename, so you don't change them. Collocation names are case-sensitive.
UPDATE dbclustergroup_sys.data_mappings SET router_collocation_group = 'NEW_COLLOCATION_GROUP' WHERE router_collocation_group = 'COLLOCATION_GROUP';
Copy each table with its full definition, not with CREATE TABLE ... AS SELECT. Before you repoint, compare the schemas on both member clusters.
Renaming an object and keeping its mappings
To point a router name at another database or table on the same member cluster, UPDATE its row in place. If you delete the row and insert a corrected one, the router name has no mapping between the two writes.
Under a table-level mapping, the router name stays the same when you rename the table on the member cluster. If you rename the table first and then update the mapping, routed statements fail with error 1146 until the mapping changes. To rename the table with no such interval, point the mapping at a view under the new name first.
To rename a table under a table-level mapping
-
On the member cluster, through a session pin or a direct connection, create a view under the new name that selects every column of the table, for example
CREATE VIEW. A view over all the columns of one table accepts reads and DML. Until you swap the view and the table,NEW_TABLEAS SELECT * FROMOLD_TABLETRUNCATEon the new name fails with error 1146 andALTER TABLEon the new name fails with error 1347, because the new name is a view. -
On the member cluster, if member cluster users have table-level privileges on the table, grant the same privileges on the new name. Otherwise, statements through the view fail with error 1142, and the grants stay on the old name after the swap.
-
At the routing endpoint, set
cluster_table_nameto the new name with theUPDATEshown earlier. Routed statements now reach the table through the view. Before the next step, wait until the change reaches the routing layer, as described in Applying data mapping changes. -
On the member cluster, swap the view and the table in one statement, for example
RENAME TABLE. The statement renames both at once, so the new name always exists.NEW_TABLETOBRIDGE_VIEW,OLD_TABLETONEW_TABLE -
On the member cluster, drop the view
BRIDGE_VIEW.
Under a database-level mapping, the cluster table name is the name that the application uses. A rename is then an application change with no step at the routing endpoint. The table answers under its new name after the rename, and the old name returns an unknown-table error. When the table is on several member clusters, rename it on one member cluster at a time, as described in Rolling out a schema change.
Removing a mapping
Remove a mapping after the application stops using its router name. Remove the row at the routing endpoint before you drop the object on the member cluster so that no mapping names a missing object. The delete leaves the object on its member cluster.
Give every DELETE a WHERE clause that matches exactly one row, and check that the statement affects one row. A WHERE clause on router_collocation_group or member_cluster_alias matches every row with that value. The routing layer refuses to remove a row that a scaleout table uses until you drop the scaleout table.
A DELETE on dbclustergroup_sys.data_mappings with no WHERE clause removes data mappings from across the group, and every object whose mapping it removes becomes unreachable through the routing endpoint. One statement removes up to 1,000 mappings, table-level mappings first. The affected-rows count shows how many it removed, and each repeat removes the next mappings. The statement keeps the mappings that a scaleout table uses, and when only those are left, it fails with error 1235. If a data movement job holds a lock on any mapping, the statement fails with error 1205 and removes nothing.
To recover, insert the rows again from your export. Leave out the empty table columns of a database-level row, and split a restore of more than 1,000 rows into several INSERT statements.
Checking a mapping change
After every data mapping change, compare a new export with the one from before the change, and check the following:
-
The object exists on the member cluster that each changed row names.
-
At the routing endpoint, from a new connection, a routed read of each changed name succeeds, and so does one read on each member cluster whose data mappings didn't change.
Related pages
For related tasks and concepts, see the following pages: