You can enable Database Proxy for your RDS for MySQL instances, so read and write requests can be automatically forwarded through a proxy address. To reduce read pressure of the primary instance, write requests are forwarded to the primary instance and read requests to read replicas based on the routing policy of the database proxy.
This section describes how to use a database proxy to implement read/write splitting. The process is as follows:
Keep in mind the following notes on database proxies:
in the upper left corner and select a region and a project.
in the upper left corner of the page and choose Database > Relational Database Service.
Parameter | Description |
|---|---|
Proxy Name | The proxy name must start with a letter and consist of 4 to 64 characters. Only letters (case-sensitive), digits, hyphens (-), and underscores (_) are allowed. |
Role |
|
Routing Policy |
You can change the routing policy after the database proxy is created. For details, see Configuring the Delay Threshold and Routing Policy. |
Reads Allowed on Primary | This parameter is available only when Load balancing is selected.
|
New Instance Class | Select specifications for the proxy instance based on service requirements. You can change the specifications after the proxy instance is created. For details, see Changing the Instance Class of a Proxy Instance. For details about performance metrics, see Table 2. |
Subnet | Select the subnet where the proxy is located. |
Proxy Nodes | Enter an integer from 2 to 8. You can change the nodes after the proxy instance is created. For details, see Changing the Number of Proxy Nodes. You are advised to set proxy nodes to the quantity of read replicas, with one proxy node for one read replica. |
Set Read Weight | This parameter is only available if Weighted is selected. Select the primary instance and read replicas to which you want to assign weights. Rules for configuring read weights
|
Select DB Instances for Load Balancing | This parameter is available only when Load balancing is selected. Select the DB instances for load balancing. After Load balancing is selected, the proxy forwards read requests to database nodes with fewer active connections. You can change the DB instances for load balancing after Database Proxy is enabled. For details, see Configuring the Delay Threshold and Routing Policy. |
You can view the read/write splitting address on the Basic Information page. Connect your application to the DB instance using this address to split read and write requests.
The read/write splitting address and the floating IP address of the DB instance are in the same VPC and subnet and are independent of each other.
Before using a database proxy to connect to an RDS for MySQL instance, ensure that the current database account has the permission to access the proxy address.
You can perform the following steps to check and grant an account the permission to access a proxy address.
SELECT user,host FROM mysql.user;

If the address is %, the host contains all CIDR blocks.
For example, if you want to connect to an RDS for MySQL instance from the IP address range starting with 192.168.0 as the root user, you can set host of the account to 192.168.% on the user management page of Data Admin Service (DAS).
Ensure that the inbound and outbound rules allow access of the read/write splitting address. The default port is 3306.
If there is no such rule, click Add Rule to add one.
For details, see section "ECS Login" in Elastic Cloud Server User Guide.
mysql -h <hostIP> -P <port> -u <userName> -p
Example:
mysql -h 192.168.0.61 -P 3306 -u root -p
Enter the password of the database account if the following information is displayed:
When you use a MySQL 8.0 client to access a database proxy, the error "auth user failed" may be displayed.
Add --default-auth=mysql_native_password when connecting to the database.
You can run the show last route command to check the routing result after you perform a read operation.
The following uses a read operation as an example to describe how to check the routing result of read requests.
Example:
select 1;
Figure 1 Performing a read operation

show last route;
Figure 2 Query result

The IP address in the command output is the IP address of the node to which the read operation is routed. Read operations are routed to the primary node or read replicas based on the specified routing policy.
Do not include show last route in service code or multi-statement requests.