Azure Function通过SQL Binding连接Azure Database MySQL Flexi Server失败求助
Azure Function 无法连接 Azure Database MySQL Flexi Server 问题
架构细节
- Azure Database MySQL Flexi Server已关联到VNet并配置专属MySQL子网
- Azure Function关联到同一个VNet的另一个子网
Java Function 代码
package org.testingfunction.azure.functions; import com.microsoft.azure.functions.*; import com.microsoft.azure.functions.annotation.AuthorizationLevel; import com.microsoft.azure.functions.annotation.FunctionName; import com.microsoft.azure.functions.annotation.HttpTrigger; import com.microsoft.azure.functions.sql.annotation.CommandType; import com.microsoft.azure.functions.sql.annotation.SQLInput; import java.util.Optional; public class getLearningModule { /** * Visit https://aka.ms/sqlbindingsinput to learn how to use this input binding * * These tasks should be completed prior to running: * 1. Add com.microsoft.azure.functions:azure-functions-java-library-sql to your project dependencies * 2. Add an app setting named "SqlConnectionString" containing the connection string to use for the SQL connection * 3. Change the bundle name in host.json to "Microsoft.Azure.Functions.ExtensionBundle" and the version to "[4.*, 5.0.0)" */ @FunctionName("getLearningModule") public HttpResponseMessage run( @HttpTrigger( name = "req", methods = {HttpMethod.GET}, authLevel = AuthorizationLevel.FUNCTION, route = "getLearningModule") HttpRequestMessage<Optional<String>> request, final ExecutionContext context, @SQLInput( name = "result", commandText = "SELECT * FROM learning_module LIMIT 10", commandType = CommandType.Text, connectionStringSetting = "sqlConnectionString") Object[] result) { return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(result).build(); } }
Azure Function 环境关联配置

连接字符串格式
Server=tcp:my-server.mysql.database.azure.com,1433;User ID=secret_username@my-server.mysql.database.azure.com;Password=myPassword;Database=mydbname
触发HttpTrigger时的错误信息
Sending exception event: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)
已尝试的操作
- 在Function的bash终端执行
nslookup查询数据库主机名,可成功解析 - 打印
sqlConnectionString配置项,能正常输出完整连接字符串 - 将连接字符串中的主机名替换为数据库IP地址,仍出现相同连接错误
额外信息
- 开发语言:Java
- 套餐层级:Premium V3
需要解决上述连接问题,恳请提供帮助!
内容的提问来源于stack exchange,提问作者mianuar
相关产品推荐
相关产品推荐

