In dit artikel worden ondersteunde verificatiemethoden, clients en voorbeeldcode beschreven die u kunt gebruiken om uw apps te verbinden met Azure SQL Database met behulp van Service Connector. In dit artikel vindt u ook de standaardnaam, waarden en configuratie van omgevingsvariabelen die zijn verkregen bij het maken van serviceverbindingen.
Ondersteunde rekenservices
ServiceConnector kan worden gebruikt om de volgende rekenservices te verbinden met Azure SQL Database:
- Azure App Service
- Azure Container Apps
- Azure Functions
- Azure Kubernetes Service (AKS)
- Azure Spring Apps
Ondersteunde verificatietypen en clients
In de onderstaande tabel ziet u welke combinaties van verificatiemethoden en -clients worden ondersteund voor het verbinden van uw rekenservice met Azure SQL Database met behulp van Service Connector. Een 'Ja' geeft aan dat de combinatie wordt ondersteund, terwijl een 'Nee' aangeeft dat deze niet wordt ondersteund.
| Client-type |
Door het systeem toegewezen beheerde identiteit |
Door de gebruiker toegewezen beheerde identiteit |
Geheim/verbindingsreeks |
Service-principal |
| .NET |
Ja |
Ja |
Ja |
Ja |
| Ga |
Nee |
Nee |
Ja |
Nee |
| Java |
Ja |
Ja |
Ja |
Ja |
| Java - Spring Boot |
Ja |
Ja |
Ja |
Ja |
| Node.js |
Ja |
Ja |
Ja |
Ja |
| PHP |
Nee |
Nee |
Ja |
Nee |
| Python |
Ja |
Ja |
Ja |
Ja |
| Python - Django |
Nee |
Nee |
Ja |
Nee |
| Ruby |
Nee |
Nee |
Ja |
Nee |
| Geen |
Ja |
Ja |
Ja |
Ja |
Notitie
Door het systeem toegewezen beheerde identiteit, door de gebruiker toegewezen beheerde identiteit en verificatie van service-principals wordt alleen ondersteund in Azure CLI.
Namen van standaardomgevingsvariabelen of toepassingseigenschappen en voorbeeldcode
Gebruik de onderstaande verbindingsgegevens om rekenservices te verbinden met Azure SQL Database. Vervang voor elk voorbeeld hieronder de tijdelijke aanduidingen , <sql-server><sql-database>en <sql-username><sql-password> door uw eigen servernaam, databasenaam, gebruikers-id en wachtwoord. Raadpleeg het artikel Service Connector-interne gegevens voor meer informatie over naamgevingsconventies.
Door het systeem toegewezen beheerde identiteit
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database verbindingsreeks |
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;Authentication=ActiveDirectoryManagedIdentity |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database verbindingsreeks |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-database>;authentication=ActiveDirectoryMSI; |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
spring.datasource.url |
Azure SQL Database-gegevensbron-URL |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI; |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_SERVER |
Azure SQL-database-server |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database-poort |
1433 |
AZURE_SQL_DATABASE |
Azure SQL-database |
<sql-database> |
AZURE_SQL_AUTHENTICATION |
Azure SQL-verificatie |
ActiveDirectoryMsi |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeld waarde |
AZURE_SQL_SERVER |
Azure SQL Database-server |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database-poort |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database-database |
<sql-database> |
AZURE_SQL_AUTHENTICATIONTYPE |
Verificatietype azure SQL Database |
azure-active-directory-default |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_HOST |
Azure SQL Database-server |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database-poort |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database-database |
<sql-database> |
AZURE_SQL_AUTHENTICATION |
Verificatietype azure SQL Database |
azure-active-directory-default |
Voorbeeldcode
Raadpleeg de stappen en code hieronder om verbinding te maken met Azure SQL Database met behulp van een door het systeem toegewezen beheerde identiteit.
Installeer afhankelijkheden.
dotnet add package Microsoft.Data.SqlClient
Haal de Azure SQL Database-verbindingsreeks op uit de omgevingsvariabele die is toegevoegd door Service Connector.
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
Zie Active Directory Managed Identity-verificatie gebruiken voor meer informatie.
Voeg de volgende afhankelijkheden toe aan uw pom.xml-bestand :
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>10.2.0.jre11</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.7.0</version>
</dependency>
Haal de Azure SQL Database-verbindingsreeks op uit de omgevingsvariabele die is toegevoegd door Service Connector.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
public class Main {
public static void main(String[] args) {
// AZURE_SQL_CONNECTIONSTRING should be one of the following:
// For system-assigned managed identity: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};authentication=ActiveDirectoryMSI;"
// For user-assigned managed identity: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};msiClientId={UserAssignedMiClientId};authentication=ActiveDirectoryMSI;"
// For service principal: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};user={ServicePrincipalClientId};password={spSecret};authentication=ActiveDirectoryServicePrincipal;"
String connectionString = System.getenv("AZURE_SQL_CONNECTIONSTRING");
SQLServerDataSource ds = new SQLServerDataSource();
ds.setURL(connectionString);
try (Connection connection = ds.getConnection()) {
System.out.println("Connected successfully.");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Zie Verbinding maken met Azure-databases vanuit App Service zonder geheimen met behulp van een beheerde identiteit voor meer informatie.
Als u voor een Spring-toepassing een verbinding met de optie --client-type springboot maakt, stelt Service Connector de eigenschappen spring.datasource.url met waardeindeling jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI; in voor Azure Spring Apps.
Werk uw toepassing bij door de zelfstudie Migreer een Java-toepassing om gebruik te maken van wachtwoordloze verbindingen met Azure SQL Database te volgen. Vergeet niet om de spring.datasource.password configuratie-eigenschap te verwijderen als deze eerder is ingesteld en de juiste afhankelijkheden toe te voegen.
Installeer afhankelijkheden.
python -m pip install pyodbc
Haal de azure SQL Database-verbindingsconfiguraties op uit de omgevingsvariabele die is toegevoegd door Service Connector. Als u de onderstaande code gebruikt, moet u het gedeelte van het codefragment verwijderen voor het verificatietype dat u wilt gebruiken. Als u Azure Container Apps als rekenservice gebruikt of als de verbindingsreeks in het codefragment niet werkt, raadpleeg dan Een Python-toepassing migreren naar wachtwoordloze verbindingen met Azure SQL Database om met behulp van een toegangstoken verbinding te maken met Azure SQL Database.
import os
import pyodbc
server = os.getenv('AZURE_SQL_SERVER')
port = os.getenv('AZURE_SQL_PORT')
database = os.getenv('AZURE_SQL_DATABASE')
authentication = os.getenv('AZURE_SQL_AUTHENTICATION')
# Uncomment the following lines corresponding to the authentication type you want to use.
# For system-assigned managed identity.
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# For user-assigned managed identity.
# clientID = os.getenv('AZURE_SQL_USER')
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};UID={clientID};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# For service principal.
# user = os.getenv('AZURE_SQL_USER')
# password = os.getenv('AZURE_SQL_PASSWORD')
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};UID={user};PWD={password};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
conn = pyodbc.connect(connString)
- Installeer afhankelijkheden.
npm install mssql
- Haal de azure SQL Database-verbindingsconfiguraties op uit de omgevingsvariabelen die zijn toegevoegd door Service Connector. Als u de onderstaande code gebruikt, moet u het gedeelte van het codefragment verwijderen voor het verificatietype dat u wilt gebruiken.
import sql from 'mssql';
const server = process.env.AZURE_SQL_SERVER;
const database = process.env.AZURE_SQL_DATABASE;
const port = parseInt(process.env.AZURE_SQL_PORT);
const authenticationType = process.env.AZURE_SQL_AUTHENTICATIONTYPE;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned managed identity.
// const config = {
// server,
// port,
// database,
// authentication: {
// type: authenticationType
// },
// options: {
// encrypt: true
// }
// };
// For user-assigned managed identity.
// const clientId = process.env.AZURE_SQL_CLIENTID;
// const config = {
// server,
// port,
// database,
// authentication: {
// type: authenticationType
// },
// options: {
// encrypt: true,
// clientId: clientId
// }
// };
// For service principal.
// const clientId = process.env.AZURE_SQL_CLIENTID;
// const clientSecret = process.env.AZURE_SQL_CLIENTSECRET;
// const tenantId = process.env.AZURE_SQL_TENANTID;
// const config = {
// server,
// port,
// database,
// authentication: {
// type: authenticationType
// },
// options: {
// encrypt: true,
// clientId: clientId,
// clientSecret: clientSecret,
// tenantId: tenantId
// }
// };
this.poolconnection = await sql.connect(config);
Voor andere talen gebruikt u de verbindingseigenschappen die serviceconnector instelt op de omgevingsvariabelen om de database te verbinden. Zie Azure SQL Database integreren met serviceconnector voor meer informatie over omgevingsvariabelen.
Zie Startpagina voor clientprogrammering naar Microsoft SQL Server voor meer informatie.
Door de gebruiker toegewezen beheerde identiteit
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database verbindingsreeks |
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;User ID=<identity-client-ID>;Authentication=ActiveDirectoryManagedIdentity |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database verbindingsreeks |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-database>;msiClientId=<msiClientId>;authentication=ActiveDirectoryMSI; |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
spring.datasource.url |
Azure SQL Database-gegevensbron-URL |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;msiClientId=<msiClientId>;authentication=ActiveDirectoryMSI; |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_SERVER |
Azure SQL Database-server |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database-poort |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database-database |
<sql-database> |
AZURE_SQL_USER |
Azure SQL Database-gebruiker |
Object (principal) ID |
AZURE_SQL_AUTHENTICATION |
Azure SQL-verificatie |
ActiveDirectoryMsi |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_SERVER |
Azure SQL Database-server |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database-poort |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database-database |
<sql-database> |
AZURE_SQL_AUTHENTICATIONTYPE |
Verificatietype Azure SQL Database |
azure-active-directory-default |
AZURE_SQL_CLIENTID |
Azure SQL Database-client-id |
<identity-client-ID> |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_HOST |
Azure SQL Database-server |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database-poort |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database-database |
<sql-database> |
AZURE_SQL_AUTHENTICATION |
Authenticatietype voor Azure SQL Database |
azure-active-directory-default |
AZURE_SQL_USERNAME |
Azure SQL Database-client-id |
<your Client ID> |
Voorbeeldcode
Raadpleeg de stappen en code hieronder om verbinding te maken met Azure SQL Database met behulp van een door de gebruiker toegewezen beheerde identiteit.
Installeer afhankelijkheden.
dotnet add package Microsoft.Data.SqlClient
Haal de Azure SQL Database-verbindingsreeks op uit de omgevingsvariabele die is toegevoegd door Service Connector.
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
Zie Active Directory Managed Identity-verificatie gebruiken voor meer informatie.
Voeg de volgende afhankelijkheden toe aan uw pom.xml-bestand :
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>10.2.0.jre11</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.7.0</version>
</dependency>
Haal de Azure SQL Database-verbindingsreeks op uit de omgevingsvariabele die is toegevoegd door Service Connector.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
public class Main {
public static void main(String[] args) {
// AZURE_SQL_CONNECTIONSTRING should be one of the following:
// For system-assigned managed identity: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};authentication=ActiveDirectoryMSI;"
// For user-assigned managed identity: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};msiClientId={UserAssignedMiClientId};authentication=ActiveDirectoryMSI;"
// For service principal: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};user={ServicePrincipalClientId};password={spSecret};authentication=ActiveDirectoryServicePrincipal;"
String connectionString = System.getenv("AZURE_SQL_CONNECTIONSTRING");
SQLServerDataSource ds = new SQLServerDataSource();
ds.setURL(connectionString);
try (Connection connection = ds.getConnection()) {
System.out.println("Connected successfully.");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Zie Verbinding maken met Azure-databases vanuit App Service zonder geheimen met behulp van een beheerde identiteit voor meer informatie.
Als u voor een Spring-toepassing een verbinding met de optie --client-type springbootmaakt, worden de eigenschappen spring.datasource.url met waardeindeling jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI; ingesteld op Azure Spring Apps.
Werk uw toepassing bij door de zelfstudie Een Java-toepassing migreren naar het gebruik van wachtwoordloze verbindingen met Azure SQL Database te volgen. Vergeet niet om de spring.datasource.password configuratie-eigenschap te verwijderen als deze eerder is ingesteld en de juiste afhankelijkheden toe te voegen.
Installeer afhankelijkheden.
python -m pip install pyodbc
Haal de azure SQL Database-verbindingsconfiguraties op uit de omgevingsvariabele die is toegevoegd door Service Connector. Als u de onderstaande code gebruikt, moet u het gedeelte van het codefragment verwijderen voor het verificatietype dat u wilt gebruiken. Als u Azure Container Apps als rekenservice gebruikt of als de connectionstring in het codefragment niet werkt, raadpleegt u Een Python-toepassing migreren voor wachtwoordloze verbindingen met Azure SQL Database om verbinding te maken met Azure SQL Database met behulp van een toegangstoken.
import os
import pyodbc
server = os.getenv('AZURE_SQL_SERVER')
port = os.getenv('AZURE_SQL_PORT')
database = os.getenv('AZURE_SQL_DATABASE')
authentication = os.getenv('AZURE_SQL_AUTHENTICATION')
# Uncomment the following lines corresponding to the authentication type you want to use.
# For system-assigned managed identity.
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# For user-assigned managed identity.
# clientID = os.getenv('AZURE_SQL_USER')
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};UID={clientID};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# For service principal.
# user = os.getenv('AZURE_SQL_USER')
# password = os.getenv('AZURE_SQL_PASSWORD')
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};UID={user};PWD={password};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
conn = pyodbc.connect(connString)
- Installeer afhankelijkheden.
npm install mssql
- Haal de azure SQL Database-verbindingsconfiguraties op uit de omgevingsvariabelen die zijn toegevoegd door Service Connector. Als u de onderstaande code gebruikt, moet u het gedeelte van het codefragment verwijderen voor het verificatietype dat u wilt gebruiken.
import sql from 'mssql';
const server = process.env.AZURE_SQL_SERVER;
const database = process.env.AZURE_SQL_DATABASE;
const port = parseInt(process.env.AZURE_SQL_PORT);
const authenticationType = process.env.AZURE_SQL_AUTHENTICATIONTYPE;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned managed identity.
// const config = {
// server,
// port,
// database,
// authentication: {
// type: authenticationType
// },
// options: {
// encrypt: true
// }
// };
// For user-assigned managed identity.
// const clientId = process.env.AZURE_SQL_CLIENTID;
// const config = {
// server,
// port,
// database,
// authentication: {
// type: authenticationType
// },
// options: {
// encrypt: true,
// clientId: clientId
// }
// };
// For service principal.
// const clientId = process.env.AZURE_SQL_CLIENTID;
// const clientSecret = process.env.AZURE_SQL_CLIENTSECRET;
// const tenantId = process.env.AZURE_SQL_TENANTID;
// const config = {
// server,
// port,
// database,
// authentication: {
// type: authenticationType
// },
// options: {
// encrypt: true,
// clientId: clientId,
// clientSecret: clientSecret,
// tenantId: tenantId
// }
// };
this.poolconnection = await sql.connect(config);
Voor andere talen gebruikt u de verbindingseigenschappen die serviceconnector instelt op de omgevingsvariabelen om de database te verbinden. Zie Azure SQL Database integreren met serviceconnector voor meer informatie over omgevingsvariabelen.
Zie Startpagina voor clientprogrammering naar Microsoft SQL Server voor meer informatie.
Verbindingstekst
Waarschuwing
Microsoft raadt u aan de veiligste verificatiestroom te gebruiken die beschikbaar is. De verificatiestroom die in deze procedure wordt beschreven, vereist een zeer hoge mate van vertrouwen in de toepassing en brengt risico's met zich mee die niet aanwezig zijn in andere stromen. U moet deze stroom alleen gebruiken wanneer andere veiligere stromen, zoals beheerde identiteiten, niet haalbaar zijn.
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database verbindingsreeks |
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;Password=<sql-password> |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database verbindingsreeks |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-database>;user=<sql-username>;password=<sql-password>; |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
spring.datasource.url |
Azure SQL Database-gegevensbron-URL |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>; |
spring.datasource.username |
Gebruikersnaam voor Azure SQL Database-gegevensbron |
<sql-user> |
spring.datasource.password |
Wachtwoord voor Azure SQL Database-gegevensbron |
<sql-password> |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_SERVER |
Azure SQL Database-server |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database-poort |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database-database |
<sql-database> |
AZURE_SQL_USER |
Azure SQL Database-gebruiker |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database-wachtwoord |
<sql-password> |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_HOST |
Azure SQL Database-host |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database-poort |
1433 |
AZURE_SQL_NAME |
Azure SQL Database-naam |
<sql-database> |
AZURE_SQL_USER |
Azure SQL Database-gebruiker |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database-wachtwoord |
<sql-password> |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database verbindingsreeks |
server=<sql-server>.database.windows.net;port=1433;database=<sql-database>;user id=<sql-username>;password=<sql-password>; |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeld van waarde |
AZURE_SQL_SERVER |
Azure SQL Database-server |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database-poort |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database-database |
<sql-database> |
AZURE_SQL_USERNAME |
Azure SQL Database-gebruikersnaam |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database-wachtwoord |
<sql-password> |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_SERVERNAME |
Azure SQL Database-servernaam |
<sql-server>.database.windows.net,1433 |
AZURE_SQL_DATABASE |
Azure SQL Database-database |
<sql-database> |
AZURE_SQL_UID |
Unieke id van Azure SQL Database (UID) |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database-wachtwoord |
<sql-password> |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_HOST |
Azure SQL Database-host |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database-poort |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database-database |
<sql-database> |
AZURE_SQL_USERNAME |
Azure SQL Database-gebruikersnaam |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database-wachtwoord |
<sql-password> |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_HOST |
Azure SQL Databasehost |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database-poort |
1433 |
AZURE_SQL_DATABASE |
Azure SQL-database |
<sql-database> |
AZURE_SQL_USERNAME |
Azure SQL Database-gebruikersnaam |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database-wachtwoord |
<sql-password> |
Voorbeeldcode
Raadpleeg de stappen en code hieronder om verbinding te maken met Azure SQL Database met behulp van een verbindingsreeks.
Installeer afhankelijkheden.
dotnet add package Microsoft.Data.SqlClient
Haal de Azure SQL Database-verbindingsreeks op uit de omgevingsvariabele die is toegevoegd door Service Connector.
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
Voeg de volgende afhankelijkheden toe aan uw pom.xml-bestand :
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>10.2.0.jre11</version>
</dependency>
Haal de Azure SQL Database-verbindingsreeks op uit de omgevingsvariabele die is toegevoegd door Service Connector.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
public class Main {
public static void main(String[] args) {
String connectionString = System.getenv("AZURE_SQL_CONNECTIONSTRING");
SQLServerDataSource ds = new SQLServerDataSource();
ds.setURL(connectionString);
try (Connection connection = ds.getConnection()) {
System.out.println("Connected successfully.");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
- Voeg afhankelijkheid toe aan het bestand 'pom.xml':
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-dependencies</artifactId>
<version>5.20.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- Stel de Spring-toepassing in. De verbindingsconfiguraties worden toegevoegd aan Spring Apps by Service Connector.
Installeer afhankelijkheden.
python -m pip install pyodbc
Haal de azure SQL Database-verbindingsconfiguraties op uit de omgevingsvariabele die is toegevoegd door Service Connector.
import os;
import pyodbc
server = os.getenv('AZURE_SQL_SERVER')
port = os.getenv('AZURE_SQL_PORT')
database = os.getenv('AZURE_SQL_DATABASE')
user = os.getenv('AZURE_SQL_USER')
password = os.getenv('AZURE_SQL_PASSWORD')
connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={user};PWD={password};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
conn = pyodbc.connect(connString)
Installeer afhankelijkheden.
pip install django
pip install pyodbc
Haal in het instellingsbestand de azure SQL Database-verbindingsconfiguraties op uit de omgevingsvariabele die is toegevoegd door Service Connector.
# in your setting file, eg. settings.py
server = os.getenv('AZURE_SQL_HOST')
port = os.getenv('AZURE_SQL_PORT')
database = os.getenv('AZURE_SQL_NAME')
user = os.getenv('AZURE_SQL_USER')
password = os.getenv('AZURE_SQL_PASSWORD')
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': database,
'USER': user,
'PASSWORD': password,
'HOST': server,
'PORT': port,
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
},
},
}
Afhankelijkheid installeren.
go install github.com/microsoft/go-mssqldb@latest
Haal de Azure SQL Database-verbindingsreeks op uit de omgevingsvariabele die is toegevoegd door Service Connector.
import (
"context"
"database/sql"
"fmt"
"log"
"github.com/microsoft/go-mssqldb/azuread"
)
connectionString := os.Getenv("AZURE_SQL_CONNECTIONSTRING")
db, err = sql.Open(azuread.DriverName, connString)
if err != nil {
log.Fatal("Error creating connection pool: " + err.Error())
}
log.Printf("Connected!\n")
- Installeer afhankelijkheden.
npm install mssql
- Haal de azure SQL Database-verbindingsconfiguraties op uit de omgevingsvariabelen die zijn toegevoegd door Service Connector.
import sql from 'mssql';
const server = process.env.AZURE_SQL_SERVER;
const database = process.env.AZURE_SQL_DATABASE;
const port = parseInt(process.env.AZURE_SQL_PORT);
const username = process.env.AZURE_SQL_USERNAME;
const password = process.env.AZURE_SQL_PASSWORD;
const config = {
server,
port,
database,
user,
password,
options: {
encrypt: true
}
};
this.poolconnection = await sql.connect(config);
Download de Microsoft-stuurprogramma's voor PHP voor SQL Server. Raadpleeg Aan de slag met de Microsoft-stuurprogramma's voor PHP voor SQL Server voor meer informatie.
Haal de azure SQL Database-verbindingsconfiguraties op uit de omgevingsvariabelen die zijn toegevoegd door Service Connector.
<?php
$server = getenv("AZURE_SQL_SERVERNAME");
$database = getenv("AZURE_SQL_DATABASE");
$user = getenv("AZURE_SQL_UID");
$password = getenv("AZURE_SQL_PASSWORD");
$connectionOptions = array(
"Database" => database,
"Uid" => user,
"PWD" => password
);
$conn = sqlsrv_connect($serverName, $connectionOptions);
?>
Download Ruby Driver voor SQL Server. Raadpleeg De ontwikkelomgeving configureren voor Ruby-ontwikkeling voor meer informatie.
Haal de azure SQL Database-verbindingsconfiguraties op uit de omgevingsvariabelen die zijn toegevoegd door Service Connector.
client = TinyTds::Client.new username: ENV['AZURE_SQL_USERNAME'], password: ENV['AZURE_SQL_PASSWORD'],
host: ENV['AZURE_SQL_HOST'], port: ENV['AZURE_SQL_PORT'],
database: ENV['AZURE_SQL_DATABASE'], azure:true
Voor andere talen gebruikt u de verbindingseigenschappen die serviceconnector instelt op de omgevingsvariabelen om de database te verbinden. Zie Azure SQL Database integreren met serviceconnector voor meer informatie over omgevingsvariabelen.
Zie Startpagina voor clientprogrammering naar Microsoft SQL Server voor meer informatie.
Service-principal
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_CLIENTID |
Uw cliënt-ID |
<client-ID> |
AZURE_SQL_CLIENTSECRET |
Uw cliëntgeheim |
<client-secret> |
AZURE_SQL_TENANTID |
Uw huurder-ID |
<tenant-ID> |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database verbindingsreeks |
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;User ID=<client-Id>;Password=<client-secret>;Authentication=ActiveDirectoryServicePrincipal |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database verbindingsreeks |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-database>;user=<client-Id>;password=<client-secret>;authentication=ActiveDirectoryServicePrincipal; |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
spring.datasource.url |
Azure SQL Database-gegevensbron-URL |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryServicePrincipal; |
spring.datasource.username |
Gebruikersnaam voor Azure SQL Database-gegevensbron |
<client-Id> |
spring.datasource.password |
Wachtwoord voor Azure SQL Database-gegevensbron |
<client-Secret> |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_SERVER |
Azure SQL Database-server |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database-poort |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database-database |
<sql-database> |
AZURE_SQL_USER |
Azure SQL Database-gebruiker |
your Client Id |
AZURE_SQL_AUTHENTICATION |
Azure SQL-verificatie |
ActiveDirectoryServerPrincipal |
AZURE_SQL_PASSWORD |
Azure SQL Database-wachtwoord |
your Client Secret |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_SERVER |
Azure SQL Database-server |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database-poort |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database-database |
<sql-database> |
AZURE_SQL_AUTHENTICATIONTYPE |
Verificatietype Azure SQL Database |
azure-active-directory-default |
AZURE_SQL_CLIENTID |
Azure SQL Database client-ID |
<your Client ID> |
AZURE_SQL_CLIENTSECRET |
Azure SQL Database-clientgeheim |
<your Client Secret > |
AZURE_SQL_TENANTID |
Tenant-id van Azure SQL Database |
<your Tenant ID> |
| Naam van standaardomgevingsvariabele |
Beschrijving |
Voorbeeldwaarde |
AZURE_SQL_HOST |
Azure SQL Database-server |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database-poort |
1433 |
AZURE_SQL_DATABASE |
Azure SQL-database |
<sql-database> |
AZURE_SQL_AUTHENTICATION |
Verificatietype Azure SQL Database |
azure-active-directory-default |
AZURE_SQL_USERNAME |
Azure SQL Database-client-ID |
<your Client ID> |
AZURE_SQL_PASSWORD |
Azure SQL Database-clientgeheim |
<your Client Secret > |
Voorbeeldcode
Raadpleeg de stappen en code hieronder om verbinding te maken met Azure SQL Database met behulp van een service-principal.
Installeer afhankelijkheden.
dotnet add package Microsoft.Data.SqlClient
Haal de Azure SQL Database-verbindingsreeks op uit de omgevingsvariabele die is toegevoegd door Service Connector.
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
Zie Active Directory Managed Identity-verificatie gebruiken voor meer informatie.
Voeg de volgende afhankelijkheden toe aan uw pom.xml-bestand :
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>10.2.0.jre11</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.7.0</version>
</dependency>
Haal de Azure SQL Database-verbindingsreeks op uit de omgevingsvariabele die is toegevoegd door Service Connector.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
public class Main {
public static void main(String[] args) {
// AZURE_SQL_CONNECTIONSTRING should be one of the following:
// For system-assigned managed identity: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};authentication=ActiveDirectoryMSI;"
// For user-assigned managed identity: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};msiClientId={UserAssignedMiClientId};authentication=ActiveDirectoryMSI;"
// For service principal: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};user={ServicePrincipalClientId};password={spSecret};authentication=ActiveDirectoryServicePrincipal;"
String connectionString = System.getenv("AZURE_SQL_CONNECTIONSTRING");
SQLServerDataSource ds = new SQLServerDataSource();
ds.setURL(connectionString);
try (Connection connection = ds.getConnection()) {
System.out.println("Connected successfully.");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Zie Verbinding maken met Azure-databases vanuit App Service zonder geheimen met behulp van een beheerde identiteit voor meer informatie.
Als u voor een Spring-toepassing een verbinding met de optie --client-type springbootmaakt, worden de eigenschappen spring.datasource.url met waardeindeling jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI; ingesteld op Azure Spring Apps.
Werk uw toepassing bij volgens de handleiding Een Java-toepassing migreren om wachtwoordloze verbindingen met Azure SQL Database te gebruiken. Vergeet niet om de spring.datasource.password configuratie-eigenschap te verwijderen als deze eerder is ingesteld en de juiste afhankelijkheden toe te voegen.
Installeer afhankelijkheden.
python -m pip install pyodbc
Haal de azure SQL Database-verbindingsconfiguraties op uit de omgevingsvariabele die is toegevoegd door Service Connector. Als u de onderstaande code gebruikt, moet u het gedeelte van het codefragment verwijderen voor het verificatietype dat u wilt gebruiken. Als u Azure Container Apps als rekenservice gebruikt of als de verbindingsreeks in het codefragment niet werkt, raadpleegt u Een Python-toepassing migreren om wachtwoordloze verbindingen met Azure SQL Database te gebruiken om verbinding te maken met Azure SQL Database met behulp van een toegangstoken.
import os
import pyodbc
server = os.getenv('AZURE_SQL_SERVER')
port = os.getenv('AZURE_SQL_PORT')
database = os.getenv('AZURE_SQL_DATABASE')
authentication = os.getenv('AZURE_SQL_AUTHENTICATION')
# Uncomment the following lines corresponding to the authentication type you want to use.
# For system-assigned managed identity.
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# For user-assigned managed identity.
# clientID = os.getenv('AZURE_SQL_USER')
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};UID={clientID};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# For service principal.
# user = os.getenv('AZURE_SQL_USER')
# password = os.getenv('AZURE_SQL_PASSWORD')
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};UID={user};PWD={password};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
conn = pyodbc.connect(connString)
- Installeer afhankelijkheden.
npm install mssql
- Haal de azure SQL Database-verbindingsconfiguraties op uit de omgevingsvariabelen die zijn toegevoegd door Service Connector. Als u de onderstaande code gebruikt, moet u het gedeelte van het codefragment verwijderen voor het verificatietype dat u wilt gebruiken.
import sql from 'mssql';
const server = process.env.AZURE_SQL_SERVER;
const database = process.env.AZURE_SQL_DATABASE;
const port = parseInt(process.env.AZURE_SQL_PORT);
const authenticationType = process.env.AZURE_SQL_AUTHENTICATIONTYPE;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned managed identity.
// const config = {
// server,
// port,
// database,
// authentication: {
// type: authenticationType
// },
// options: {
// encrypt: true
// }
// };
// For user-assigned managed identity.
// const clientId = process.env.AZURE_SQL_CLIENTID;
// const config = {
// server,
// port,
// database,
// authentication: {
// type: authenticationType
// },
// options: {
// encrypt: true,
// clientId: clientId
// }
// };
// For service principal.
// const clientId = process.env.AZURE_SQL_CLIENTID;
// const clientSecret = process.env.AZURE_SQL_CLIENTSECRET;
// const tenantId = process.env.AZURE_SQL_TENANTID;
// const config = {
// server,
// port,
// database,
// authentication: {
// type: authenticationType
// },
// options: {
// encrypt: true,
// clientId: clientId,
// clientSecret: clientSecret,
// tenantId: tenantId
// }
// };
this.poolconnection = await sql.connect(config);
Voor andere talen gebruikt u de verbindingseigenschappen die serviceconnector instelt op de omgevingsvariabelen om de database te verbinden. Zie Azure SQL Database integreren met serviceconnector voor meer informatie over omgevingsvariabelen.
Zie Startpagina voor clientprogrammering naar Microsoft SQL Server voor meer informatie.
Volgende stappen
Volg de onderstaande zelfstudie voor meer informatie over Service Connector.