

# Character sets and collations

When you connect to the routing endpoint of a database cluster group for Aurora MySQL, your session uses the `utf8mb4` character set. The session also uses one of the nine collations that the routing endpoint supports. If your client doesn't ask for a collation, the session uses `utf8mb4_0900_ai_ci`. That collation is the MySQL default for `utf8mb4`.

## Supported character set and collations
<a name="databaseclustergroups-character-sets-supported"></a>

The routing endpoint supports `utf8mb4` and the following collations:
+ `utf8mb4_0900_ai_ci` (the default)
+ `utf8mb4_0900_as_ci`
+ `utf8mb4_0900_as_cs`
+ `utf8mb4_0900_bin`
+ `utf8mb4_bin`
+ `utf8mb4_general_ci`
+ `utf8mb4_ja_0900_as_cs`
+ `utf8mb4_ja_0900_as_cs_ks`
+ `utf8mb4_unicode_ci`

## Setting the session character set and collation
<a name="databaseclustergroups-character-sets-connecting"></a>

The routing endpoint accepts only connections that use `utf8mb4`. Set `utf8mb4` explicitly in your client or driver. Each member cluster that your queries reach uses your session collation. The following steps set the character set and collation for a session.

**To set the character set and collation for a session**

1. Connect to the routing endpoint with `utf8mb4`. In the following `mysql` command, replace {{routing-endpoint}} with the routing endpoint of your database cluster group for Aurora. Replace {{router-user}} with the name of a router user.

   ```bash
   mysql -h {{routing-endpoint}} -P 3306 -u {{router-user}} -p \
     --ssl-mode=REQUIRED --default-character-set=utf8mb4
   ```

   For more information about router users and TLS connections, see [Router users and member cluster users](databaseclustergroups-router-users.md#databaseclustergroups-managing-router-users-and-member-cluster-users) and [TLS on the routing endpoint](databaseclustergroups-tls.md#databaseclustergroups-requiring-tls-on-the-routing-endpoint).

1. Set the session collation. The following example sets the session collation to `utf8mb4_0900_as_cs`:

   ```sql
   SET NAMES utf8mb4 COLLATE utf8mb4_0900_as_cs;
   ```

   Run `SET NAMES` on each new connection that needs a different collation.

1. To check the session collation, run the following statement:

   ```sql
   SELECT @@collation_connection;
   ```

   The result shows the collation that you set in the previous step.

For more information about character sets, collations, and client configuration, see [Character Sets, Collations, Unicode](https://dev.mysql.com/doc/refman/8.4/en/charset.html) on the MySQL website.

## Collations and string routing keys
<a name="databaseclustergroups-character-sets-routing-keys"></a>

The routing layer uses the collation of the scaleout table in the following cases:
+ When it matches string routing keys for a `COLUMN` routing function
+ When it matches the prefixes that it extracts for a `PREFIX` routing function
+ When it hashes string routing keys with `cityhash`, `xxhash`, or `md5` in a `HASH` routing function

For more information about routing function syntax, see [CREATE SCALEOUT ROUTING FUNCTION](databaseclustergroups-sql-create-scaleout-routing-function.md).

For the routing functions in the preceding list, `'apple'` and `'APPLE'` route the same way under the default collation, `utf8mb4_0900_ai_ci`. Under `utf8mb4_bin`, `'apple'` and `'APPLE'` might route to different member clusters. `utf8mb4_bin` also ignores trailing spaces, so `'m '` routes the same way as `'m'`.

**Note**  
If you omit `DEFAULT CHARACTER SET` and `DEFAULT COLLATE` from a `CREATE SCALEOUT TABLE` statement, the scaleout table uses the `utf8mb4_0900_ai_ci` collation. The scaleout table uses this collation even if the routing key column in the member tables uses another collation. We recommend that you set `DEFAULT COLLATE` to the collation of the routing key column in the member tables. For more information about the syntax, see [CREATE SCALEOUT TABLE](databaseclustergroups-sql-create-scaleout-table.md).  
If the collations differ, the routing layer and the member cluster don't compare values the same way. For example, suppose that a scaleout table uses a `COLUMN` routing function and the `utf8mb4_0900_ai_ci` collation. The routing key column in the member tables uses `utf8mb4_bin`. A query for `'APPLE'` reaches the member cluster that stores `'apple'`, but the query returns no rows.

## Listing the supported character set and collations
<a name="databaseclustergroups-character-sets-listing"></a>

Connect to the routing endpoint. To list the character set and collations, run `SHOW CHARACTER SET` and `SHOW COLLATION`.

## Matching character set and collation settings across member clusters
<a name="databaseclustergroups-character-sets-members"></a>

Keep the following settings the same on each member cluster:
+ The `character_set_server` and `collation_server` parameters. For more information about parameter groups, see [Parameter groups for Amazon Aurora](USER_WorkingWithParamGroups.md).
+ The default character set and collation of each database that is on more than one member cluster.
+ The character set and collation of each string routing key column in the member tables.

**To compare character set and collation settings across member clusters**

1. Connect to the cluster endpoint of a member cluster. To show the server, database, and column settings, run the following statements. Replace {{member-database-name}} with the name of the database on that member cluster. Replace {{routing-key-column}} with the name of a string routing key column.

   ```sql
   SELECT @@GLOBAL.character_set_server,
          @@GLOBAL.collation_server;
   
   SELECT SCHEMA_NAME,
          DEFAULT_CHARACTER_SET_NAME,
          DEFAULT_COLLATION_NAME
     FROM information_schema.SCHEMATA
    WHERE SCHEMA_NAME = '{{member-database-name}}';
   
   SELECT TABLE_NAME,
          COLUMN_NAME,
          CHARACTER_SET_NAME,
          COLLATION_NAME
     FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = '{{member-database-name}}'
      AND COLUMN_NAME  = '{{routing-key-column}}';
   ```

1. Repeat the previous step on each member cluster for each string routing key column. Then compare the results. To match each member table across member clusters, use the `TABLE_NAME` column in the results.

1. Connect to the routing endpoint. For each scaleout table with a string routing key, run `SHOW CREATE SCALEOUT TABLE {{database-name}}.{{table-name}}`. Replace {{database-name}} with the name of the database that contains the scaleout table. Replace {{table-name}} with the name of the scaleout table.

1. In the output, check that `DEFAULT COLLATE` matches the `COLLATION_NAME` value of each string routing key column in the member tables. For more information, see [SHOW CREATE SCALEOUT TABLE](databaseclustergroups-sql-show-create-scaleout-table.md).

For more information about other settings to check, see [Auditing member cluster configuration](databaseclustergroups-auditing.md#databaseclustergroups-auditing-member-cluster-consistency).