This topic describes the common causes of database access failures and how to troubleshoot database access failures in Function Compute.
Troubleshoot common causes
Database access occasionally fails
If the database access failure occasionally occurs, perform the following steps to troubleshoot the causes:
Check whether you use
end()
instead ofdestroy()
to disconnect your database.Function instances are automatically destroyed after they are idle for about 2 to 5 minutes. If you use the
end()
method, the MySQL server is disconnected after the database receives query results. If the database is not properly disconnected, the next connection times out.Check whether a security group is configured when you access the database over a VPC.
If a security group is configured when you access the database, the connection of the function may be unstable. We recommend that you configure an IP address whitelist when you access the database.
Check the cause of the access failure by referring to Database connection constantly fails.
Database connection constantly fails
If the connection constantly fails when you access a database, perform the following steps to troubleshoot the causes:
Query connection records in the requested database and check whether connection requests from Function Compute exist in the corresponding time period.
Check whether the username and password that are used to access the database are correct.
If the username and password are correct, go to step 3.
If the username or password is invalid, enter a valid username and password.
Log on to the instance and see whether you can use the
telnet
command to connect to the database.If the connection can be established by using telnet, go to step 4.
If the connection cannot be established by using telnet, check the network configurations. For more information, see Troubleshoot network issues.
Check whether the logic of your function code is correct.
If the issue persists after you perform troubleshooting by using the preceding methods, join the DingTalk user group 11721331 for technical support.
Troubleshoot network issues
Access the database over a VPC
Make sure that the created database instance is in the same region as the function that accesses the database.
If the
AccessDenied
error is reported and the error code is 403, check whether you have granted permissions on the elastic network interface (ENI).If the
InvalidArgument
error is reported and the error code is 400, check whether the configurations for VPC, vSwitch, and Security Group are valid.Check whether an IP address whitelist is configured for the database. If a whitelist is configured, make sure that private VPC CIDR blocks and vSwitch CIDR blocks are configured. You can add 0.0.0.0/0 to the whitelist to allow accesses from all IP addresses.
Make sure that the security group allows outbound traffic from the private CIDR block and port of the database.
If the
ResourceExhausted
error is reported and the error code is 429, check whether available IP addresses in the IP CIDR block of the vSwitch is insufficient. The prefix length of the CIDR block that is automatically configured is 24, and the total number of available IP addresses is 252. If the number of instances is too large, the limit may be reached and you must manually modify the CIDR block of the vSwitch and the security group.
Access the database over the Internet
Make sure that Internet access is enabled for the service. For more information, see Configure network settings.
If a whitelist of static IP addresses is configured for the database, make sure that a static public IP address is configured in Function Compute. In Function Compute, the instance that runs a function is dynamically allocated by Function Compute, and the IP address is not fixed. To access the database over the Internet, you must configure a public IP address. For more information, see Configure static public IP addresses.
Network issues on the database side
You can configure a network environment similar to the network environment of Function Compute in another service. Then, check whether you can connect to the database by using the
telnet
command. If the connection can be established, the network on the database side is normal.