So I have an Azure Function where I want to query from my Azure Database MySQL Flexi Server. A bit of details on my architecture, My database is currently attach to the vnet and have MySQL Subnet. I attach my function to the same vnet with different subnet. Here is my code for my 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();
}
}
Then I attach to the azure function environment such as below:
for details, here is my connection string format:
Server=tcp:my-server.mysql.database.azure.com,1433;User [email protected];Password=myPassword;Database=mydbname
However, when I trigger the httpTrigger, it results in this error:
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)
Attempt that I had made:
- Tried to nslookup my dbhostname in the function bash. Result: resolved db hostname
- Tried to print the sqlConnection string. Result: able to print connection string
- Tried to directly put ip address instead of hostname. Result: same error appeared
Additional info:
- Language in java
- Package tier: Premium V3
Really hope for help!!!!
2
Answers
Can you check the firewall rules on the subnets and make sure that the traffic (outgoing from Function and incoming to SQL DB) is allowed. Also please do a telnet from the Function bash to DB on port 1433.
@mianuar
Are you able to access the SQL DB from a client?
Telnet is not supported on Kudu, but Azure provides a tool tcpping. ex: tcpping http://www.google.com:80. Please refer to this .
You can write a small method in the Java function app to explicitly connect to SQL DB using JDBC and see if it can connect.