Common causes of database access failures

Updated at:

Troubleshoot database access failures in Function Compute, whether the connection fails intermittently, fails on every attempt, or is blocked by a network issue.

Troubleshooting

Intermittent database connection failures

If database access fails intermittently, complete the following steps to identify the cause:

  1. When you close the database connection, check whether you use destroy() instead of end().

    Function instances are automatically destroyed after being idle for about 2 to 5 minutes. When you use the end() method, the connection to the MySQL server is closed only after the database returns the query result. If the connection to the database is not closed properly, the next connection attempt times out.

  2. When you access the database over a virtual private cloud (VPC), check whether a security group is configured.

    Accessing the database by using a security group causes unstable connections for your function. The recommended method is to configure an IP address whitelist to access the database.

  3. See Constant database connection failures and check each possible cause in turn.

Constant database connection failures

If database access fails on every attempt, complete the following steps to identify the cause:

  1. Search for connection records in the database that the function accesses, and check whether Function Compute sent connection requests at the time of the failure.

    • If no connection requests from Function Compute are found in the database, go to step 2.

    • If connection requests from Function Compute are found in the database, go to step 4.

  2. Check whether the username and password used to access the database are correct.

    • If the username and password are correct, go to step 3.

    • If the username or password is incorrect, enter the correct username or password.

  3. Log on to the instance and check whether you can use the telnet command to connect to the database.

    • If the connection succeeds, go to step 4.

    • If the connection fails, check for network issues. For details, see Network troubleshooting.

  4. Check whether the logic of your function code is correct.

If your issue remains unresolved after you complete the preceding steps, join the DingTalk group 64970014484 to communicate instantly with Function Compute engineers.

Network troubleshooting

  • Database access over a VPC

    • Make sure that the database instance you created and the function that accesses the database reside in the same region.

    • If the AccessDenied error is returned with error code 403, make sure that the operation permissions on the elastic network interface (ENI) are granted.

    • If the InvalidArgument error is returned with error code 400, make sure that VPC, vSwitch, and Security Group are correctly configured.

    • Check whether an IP address whitelist is configured for the database. If a whitelist is configured, add the private Classless Inter-Domain Routing (CIDR) blocks of the VPC and the vSwitch to the whitelist. The whitelist can be set to 0.0.0.0/0 to allow access from all IP addresses. For more information, see Accessing ApsaraDB RDS for MySQL.

    • Make sure that the outbound rules of the security group allow traffic on the port and to the private CIDR block of the database.

    • If the ResourceExhausted error is returned with error code 429, check whether the number of available IP addresses in the CIDR block of the vSwitch is insufficient. The automatically configured network prefix length is 24, which provides a total of 252 available IP addresses. If too many instances exist, the vSwitch may run out of available IP addresses. Manually adjust the CIDR block of the vSwitch and the corresponding security group.

  • Database access over the public network

    • Make sure that public network access is enabled in the network configuration of the service. For more information, see Configure network.

    • If a static IP address whitelist is configured for the database, make sure that a static public IP address is configured for Function Compute. In Function Compute, the instances that run functions are dynamically allocated, so their IP addresses are not fixed. To access the database over the public network, see Configure a static public IP address.

  • Network issues on the database side

    Configure a network environment similar to that of Function Compute in another service. Then, check whether you can connect to the database by using the telnet command. If the connection succeeds, network issues on the database side can be ruled out.