This topic describes the common causes of database access failures and how to troubleshoot database access failures in Function Compute.
Common causes
Database access fails occasionally
If database access failures occur occasionally, perform the following steps to resolvethe issue:
Check whether the
end()method instead of thedestroy()method is used when the database is disconnected.Function instances are automatically destroyed after they are idle for approximately 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 specified when you access the database by using a virtual private cloud (VPC).
If a security group is specified 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.
Refer to Database access fails every time to troubleshoot the issue.
Database access fails every time
If an error occurs every time you try to access the database, perform the following steps to resolve the issue:
Analyze the connection records in the database to check whether connection requests from Function Compute exist within the corresponding time period.
Check whether the username and password that are used to access the database are valid.
If the username and password are valid, go to 3.
If the username or password is invalid, enter a valid username and password.
Log on to the instance and checkwhether you can connect to the database by using the
telnetcommand.If the connection can be established by using telnet, go to 4.
If the connection cannot be established by using telnet, check the network configurations. For more information, see Network issues.
Check whether the logic of the function code is valid.
Function Compute provides sample code that you can directly use. For more information, see Access other Alibaba Cloud services.
If the issue persists, Join the DingTalk group 11721331 to communicate instantly with Function Compute engineers.
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
AccessDeniederror is reported and the error code is 403, check whether you have granted permissions on the elastic network interface (ENI).If the
InvalidArgumenterror 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 block and vSwitch CIDR block are configured. You can add 0.0.0.0/0 to the whitelist to allow accesses from all IP addresses. For more information, see Step 1: Configure the IP address whitelist for the database.
Make sure that the security group allows outbound traffic from the private CIDR block and port of the database.
If the
ResourceExhaustederror 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 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, and the IP address is not fixed. If you want to access the database over the Internet, you must configure a static public IP address. For more information, see Assign a static public IP address.
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
telnetcommand. If a connection can be established, the network on the database side is normal.