Enable routing and add data mappings
In this step, you enable routing on your database cluster group for Aurora, map one database, and read it through the routing endpoint. Routing adds a path to your data, and direct access to each member cluster stays available.
Your applications keep their direct connections to member clusters until you point them at the routing endpoint, and you choose for each application. If you don't use routing, you can continue at Managing a database cluster group for Aurora.
Topics
Choosing where to start with routing
Use the following table to find the page for your situation.
| You want to | Start with |
|---|---|
| Map one database and make a first routed read | Enabling routing, on this page |
| Manage several clusters with data split across them by your application or by a proxy | Describing existing data partitions with data mappings |
| Send some or all of an application's traffic through the routing endpoint | Sending traffic through the routing layer |
On Aurora MySQL, you connect with the mysql client on port 3306. Router users sign in with a password; IAM database authentication for router users isn't available at preview, as described in IAM database authentication. You manage data mappings from a session on any database, and a routed read runs in query routing mode.
Enabling routing
You can enable routing when you create the group, or later with ModifyDBClusterGroup (aws rds modify-db-cluster-group). If you enabled routing in Step 1, skip to the next section. The routing endpoint listens on port 3306. You can't change the port. describe-db-cluster-groups returns it in RoutingEndpointConfigurationInfo.Port.
If the group has no administrative router user, pass --admin-username and the password with --enable-routing. You pass them only the first time that you enable routing. You can't change the username later, but you can change the password. For the network parameters for routing, see Routing endpoint networking.
Finding the member cluster aliases
A member cluster alias is the name that data mappings, session pins, and data moves use for a member cluster. You don't choose it: the group assigns it, and the routing layer lists the aliases in dbclustergroup_sys.member_cluster_mappings. At the routing endpoint, sign in as the administrative router user and list the member clusters.
The routing endpoint requires TLS 1.2 or TLS 1.3 and refuses a connection without TLS. On Aurora MySQL, the refused connection gets error 3159. The examples connect on the routing endpoint port, 3306. For client TLS settings, see Encryption in transit (TLS) and at rest.
Sign in with the MySQL client, with TLS required.
mysql -hROUTING_ENDPOINT-P 3306 -uADMIN_USER-p --ssl-mode=REQUIRED
At the routing endpoint, list the member clusters, and record the member_cluster_alias of each row. The db_cluster_identifier column names the Aurora DB cluster that each alias stands for.
SELECT member_cluster_alias, db_cluster_identifier FROM dbclustergroup_sys.member_cluster_mappings;
Copy each alias from this list, and don't derive it from a cluster identifier. The list shows a member cluster after the group finishes adding it. If a member cluster that you added isn't in the list, repeat the listing, and write no data mapping for it until it appears. The routing layer refuses a data mapping that names an alias that isn't in this list, and writes no row.
Creating a router user
The routing layer authenticates each sign-in as a router user, which you create at the routing endpoint. When a routed statement reaches a member cluster, it runs there as the member cluster user of the same name, and that member cluster checks the data privileges. A router user that has no member cluster user of the same name on a member cluster can't reach that member cluster. The group doesn't copy database users or roles from the member clusters, so you create both accounts. The administrative router user manages data mappings at the routing endpoint. To pin a session to a member cluster as that user, also create a member cluster user of the same name there.
To create a router user and its member cluster users
-
At the routing endpoint, as the administrative router user, create the router user.
CREATE USER 'app_user'@'%' IDENTIFIED BY 'PASSWORD'; -
On each member cluster, through its own cluster endpoint, create the member cluster user of the same name with read access to the database that you map next.
CREATE USER IF NOT EXISTS 'app_user'@'%' IDENTIFIED BY RANDOM PASSWORD; GRANT SELECT ON tenant_0001.* TO 'app_user'@'%';
Grant data privileges on the member clusters, not at the routing endpoint. Each member cluster checks them when a statement reaches it. A missing member cluster user or grant shows up as an error on the first statement routed there, not at sign-in. For authentication methods and grants, see Router users and routing layer privileges.
Mapping a database
A data mapping tells the routing layer which member cluster holds a database or a table. A database-level mapping places a whole database on one member cluster. You manage mappings with SQL at the routing endpoint. Run each mapping change outside a transaction, because data mapping changes aren't transactional. The routing layer doesn't check that the database exists on the member cluster, so you create it there first and keep it there.
Before you map an existing database
Before you map a database that already holds data, check the following:
-
You plan one collocation for each database. Keep databases that statements join or write together in one collocation. To find them, see Checking a captured workload against a group.
-
On the member cluster that holds the database, over a direct connection, no view, foreign key, trigger, routine, or event uses a database bound for another member cluster. Such an object fails after the databases are on different member clusters. Keep those databases in one collocation, or change the object first. The catalog queries follow this list.
-
Each application router user has its same-named member cluster user, with grants on the database, on the member cluster that holds it.
-
If you plan to move data later, the member clusters don't run with
binlog_formatset toSTATEMENTorMIXED. If the binary log is off on the source member cluster, a move turns it on withROW.
On the member cluster (direct), list the database's triggers, routines, events, and views.
SELECT TRIGGER_NAME, EVENT_OBJECT_TABLE FROM information_schema.TRIGGERS WHERE TRIGGER_SCHEMA = 'CLUSTER_DB'; SELECT ROUTINE_TYPE, ROUTINE_NAME FROM information_schema.ROUTINES WHERE ROUTINE_SCHEMA = 'CLUSTER_DB'; SELECT EVENT_NAME, STATUS FROM information_schema.EVENTS WHERE EVENT_SCHEMA = 'CLUSTER_DB'; SELECT VIEW_NAME, TABLE_SCHEMA, TABLE_NAME FROM information_schema.VIEW_TABLE_USAGE WHERE VIEW_SCHEMA = 'CLUSTER_DB';
A mapping doesn't create the database. If a database isn't on its member cluster, create it and load it there first, over a direct connection or through a session pinned at the routing endpoint. Before you change mappings, export them, as described in Exporting the mappings before and after a change.
To map one database
-
On the first member cluster, through its own cluster endpoint, create the database with one table and one row.
CREATE DATABASE tenant_0001; CREATE TABLE tenant_0001.customers (id BIGINT PRIMARY KEY, name VARCHAR(100)); INSERT INTO tenant_0001.customers VALUES (1, 'Example customer'); -
At the routing endpoint, as the administrative router user with autocommit on, insert one mapping row. Use the first member cluster's alias, and name a collocation for the database.
INSERT INTO dbclustergroup_sys.data_mappings (cluster_db_name, router_db_name, member_cluster_alias, router_collocation_group) VALUES ('tenant_0001', 'tenant_0001', 'MEMBER_CLUSTER_ALIAS_1', 'cg_tenant_0001'); -
At the routing endpoint, check that the statement affected one row, and read the mapping back. For the read, see SELECT FROM dbclustergroup_sys.data_mappings.
SELECT * FROM dbclustergroup_sys.data_mappings;
A collocation is a named group of tables and key values that stay on one member cluster and move together. If you leave out router_collocation_group, the routing layer uses the member cluster alias as the collocation, so the database joins the member cluster's default collocation and moves with everything else there. Name a collocation on every mapping row, so that each database or table can move on its own and a routing function can name it. For table-level mappings, see Using database-level and table-level mappings.
If the insert names an alias that dbclustergroup_sys.member_cluster_mappings doesn't list, the routing layer writes no row. It returns error 64004 (42000), Member cluster '. For the fix, see Routing and data mapping errors.alias' doesn't exist
Tables whose rows span member clusters
If one table's rows are split across member clusters, map each member cluster's copy of the table at table level, each in its own collocation, and create a scaleout table over those member tables. A routing function, MOD, HASH, PREFIX, or COLUMN, maps each routing key value to a collocation, and every statement on the scaleout table carries the routing key. Choose the key and the function before you load data, because a routing function has no ALTER statement. A dependent table, such as an orders table beside a customers table, carries the same key column and routes with its parent through USING ROUTING FUNCTION OF, in the same collocations. For the procedure, see Creating and loading a scaleout table. To choose a key, see Choosing a routing key and routing function.
A data mapping change reaches the routing layer within a short time. A statement that the routing layer routes on the older mapping gets error 1412 on Aurora MySQL. The session stays open, and a retry routes on the new mapping. Inside a transaction, a statement that needs a mapping changed after the transaction's first routed statement fails with error 63928 (Aurora MySQL), and the routing layer has already rolled the whole transaction back, so retry the whole transaction. For the error codes and the retry rule, see Applying data mapping changes.
Making a first routed read
Read the mapped table through the routing endpoint as app_user.
To make a first routed read
-
From your SQL host, sign in to the routing endpoint as
app_user, with TLS required.mysql -hROUTING_ENDPOINT-P 3306 -u app_user -p --ssl-mode=REQUIRED -
At the routing endpoint, read the table. The connection runs in query routing mode, and the routing layer sends the statement to the member cluster that the mapping for
tenant_0001names. That member cluster returns the row that you inserted.SELECT * FROM tenant_0001.customers;
A SELECT 1 that succeeds proves only the sign-in, because the routing layer answers it itself. To test the path to a member cluster, read a mapped table, as in this procedure. If the read fails with an error from the member cluster, check that app_user and its grant exist there. On Aurora MySQL, USE with a misspelled or unmapped name doesn't fail when you send it: the error arrives with your first query. For connection modes, see Sessions, transactions, and session pinning. For client settings, see Connecting to the routing endpoint.
Result: You read data through one endpoint, under the database name that your application already uses. Next, remove the resources that you created for the tutorial, in Cleaning up resources.