이 페이지에는 지원되는 인증 방법 및 클라이언트가 표시되며 서비스 커넥터를 사용하여 컴퓨팅 서비스를 Azure SQL Database에 연결하는 데 사용할 수 있는 샘플 코드가 표시됩니다. 다른 방법을 사용하여 Azure SQL Database에 계속 연결할 수 있습니다. 이 페이지에서는 서비스 연결을 만들 때 가져오는 기본 환경 변수 이름과 값도 보여 줍니다.
지원되는 컴퓨팅 서비스
서비스 커넥터를 사용하여 Azure SQL Database에 다음 컴퓨팅 서비스를 연결할 수 있습니다.
- Azure App Service
- Azure Container Apps
- Azure 기능
- AKS(Azure Kubernetes Service)
- Azure Spring Apps
지원되는 인증 유형 및 클라이언트
아래 표에서는 서비스 커넥터를 사용하여 컴퓨팅 서비스를 Azure SQL Database에 연결하는 데 지원되는 인증 방법과 클라이언트의 조합을 보여 줍니다. "예"는 조합이 지원됨을 나타내고 "아니오"는 지원되지 않음을 나타냅니다.
클라이언트 유형 |
시스템 할당 관리 ID |
사용자 할당 관리 ID |
비밀/연결 문자열 |
서비스 사용자 |
.NET |
예 |
예 |
예 |
예 |
Go |
아니요 |
아니요 |
예 |
아니요 |
Java |
예 |
예 |
예 |
예 |
Java - Spring Boot |
예 |
예 |
예 |
예 |
Node.JS |
예 |
예 |
예 |
예 |
PHP |
아니요 |
아니요 |
예 |
예 |
Python |
예 |
예 |
예 |
예 |
Python - Django |
아니요 |
아니요 |
예 |
아니요 |
Ruby |
아니요 |
아니요 |
예 |
아니요 |
None |
예 |
예 |
예 |
예 |
이 표는 비밀/연결 문자열 메서드가 모든 클라이언트 형식에 대해 지원됨을 나타냅니다. 시스템 할당 관리 ID, 사용자 할당 관리 ID 및 서비스 주체 메서드는 .NET, Java, Java - Spring Boot, Node.js, Python 및 None 클라이언트 유형에 대해 지원됩니다. 이러한 메서드는 Go, PHP, Django 및 Ruby 클라이언트 유형에 대해 지원되지 않습니다.
참고 항목
시스템 할당 관리 ID, 사용자 할당 관리 ID 및 서비스 주체는 Azure CLI에서만 지원됩니다.
기본 환경 변수 이름 또는 애플리케이션 속성 및 샘플 코드
아래의 연결 세부 정보를 사용하여 컴퓨팅 서비스를 Azure SQL Database에 연결합니다. 아래 각 예제에서 자리 표시자 텍스트 <sql-server>
, <sql-database>
, <sql-username>
및 <sql-password>
를 자체 서버 이름, 데이터베이스 이름, 사용자 ID 및 암호로 바꿉니다. 명명 규칙에 대한 자세한 내용은 서비스 커넥터 내부 문서를 확인하세요.
시스템이 할당한 관리 ID
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL 데이터베이스 연결 문자열 |
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;Authentication=ActiveDirectoryManagedIdentity |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL 데이터베이스 연결 문자열 |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-database>;authentication=ActiveDirectoryMSI; |
기본 환경 변수 이름 |
설명 |
샘플 값 |
spring.datasource.url |
Azure SQL 데이터베이스 데이터 원본 URL |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI; |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_SERVER |
Azure SQL Database 서버 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 포트 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 데이터베이스 |
<sql-database> |
AZURE_SQL_AUTHENTICATION |
Azure SQL 인증 |
ActiveDirectoryMsi |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_SERVER |
Azure SQL Database 서버 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 포트 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 데이터베이스 |
<sql-database> |
AZURE_SQL_AUTHENTICATIONTYPE |
Azure SQL Database 인증 유형 |
azure-active-directory-default |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_HOST |
Azure SQL Database 서버 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 포트 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 데이터베이스 |
<sql-database> |
AZURE_SQL_AUTHENTICATION |
Azure SQL Database 인증 유형 |
azure-active-directory-default |
샘플 코드
시스템 할당 관리 ID를 사용하여 Azure SQL Database에 연결하려면 아래 단계 및 코드를 참조하세요.
종속성을 설치합니다.
dotnet add package Microsoft.Data.SqlClient
서비스 커넥터에서 추가한 환경 변수에서 Azure SQL Database 연결 문자열을 가져옵니다.
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
자세한 내용은 Active Directory 관리 ID 인증 사용을 참조하세요.
pom.xml 파일에서 다음 종속성을 추가합니다.
<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>
서비스 커넥터에서 추가한 환경 변수에서 Azure SQL Database 연결 문자열을 가져옵니다.
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();
}
}
}
자세한 내용은 관리 ID를 사용하여 비밀 없이 App Service에서 Azure 데이터베이스에 연결을 참조하세요.
Spring 애플리케이션의 경우 --client-type springboot
옵션을 사용하여 연결을 만드는 경우 서비스 커넥터는 값 형식이 jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI;
인 spring.datasource.url
속성을 Azure Spring Apps로 설정합니다.
Azure SQL Database에서 암호 없는 연결을 사용하도록 Java 애플리케이션 마이그레이션 자습서에 따라 애플리케이션을 업데이트합니다. 이전에 설정된 경우 spring.datasource.password
구성 속성을 제거하고 올바른 종속성을 추가해야 합니다.
종속성을 설치합니다.
python -m pip install pyodbc
서비스 커넥터가 추가한 환경 변수에서 Azure SQL Database 연결 구성을 가져옵니다. 아래 코드를 사용하는 경우 사용하려는 인증 유형에 대한 코드 조각 부분의 주석 처리를 제거합니다. Azure Container Apps를 컴퓨팅 서비스로 사용하고 있거나 코드 조각의 연결 문자열이 작동하지 않는 경우 Azure SQL Database에서 암호 없는 연결을 사용하도록 Python 애플리케이션 마이그레이션을 참조하여 액세스 토큰을 사용해 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)
- 종속성을 설치합니다.
npm install mssql
- 서비스 커넥터가 추가한 환경 변수에서 Azure SQL Database 연결 구성을 가져옵니다. 아래 코드를 사용하는 경우 사용하려는 인증 유형에 대한 코드 조각 부분의 주석 처리를 제거합니다.
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);
자세한 내용은 Microsoft SQL Server로의 클라이언트 프로그래밍 홈페이지를 참조하세요.
사용자 할당 관리 ID
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL 데이터베이스 연결 문자열 |
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;User ID=<identity-client-ID>;Authentication=ActiveDirectoryManagedIdentity |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL 데이터베이스 연결 문자열 |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-database>;msiClientId=<msiClientId>;authentication=ActiveDirectoryMSI; |
기본 환경 변수 이름 |
설명 |
샘플 값 |
spring.datasource.url |
Azure SQL 데이터베이스 데이터 원본 URL |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;msiClientId=<msiClientId>;authentication=ActiveDirectoryMSI; |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_SERVER |
Azure SQL Database 서버 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 포트 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 데이터베이스 |
<sql-database> |
AZURE_SQL_USER |
Azure SQL Database 이름 |
Object (principal) ID |
AZURE_SQL_AUTHENTICATION |
Azure SQL 인증 |
ActiveDirectoryMsi |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_SERVER |
Azure SQL Database 서버 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 포트 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 데이터베이스 |
<sql-database> |
AZURE_SQL_AUTHENTICATIONTYPE |
Azure SQL Database 인증 유형 |
azure-active-directory-default |
AZURE_SQL_CLIENTID |
Azure SQL Database 클라이언트 ID |
<identity-client-ID> |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_HOST |
Azure SQL Database 서버 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 포트 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 데이터베이스 |
<sql-database> |
AZURE_SQL_AUTHENTICATION |
Azure SQL Database 인증 유형 |
azure-active-directory-default |
AZURE_SQL_USERNAME |
Azure SQL Database 클라이언트 ID |
<your Client ID> |
샘플 코드
사용자 할당 관리 ID를 사용하여 Azure SQL Database에 연결하려면 아래 단계 및 코드를 참조하세요.
종속성을 설치합니다.
dotnet add package Microsoft.Data.SqlClient
서비스 커넥터에서 추가한 환경 변수에서 Azure SQL Database 연결 문자열을 가져옵니다.
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
자세한 내용은 Active Directory 관리 ID 인증 사용을 참조하세요.
pom.xml 파일에서 다음 종속성을 추가합니다.
<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>
서비스 커넥터에서 추가한 환경 변수에서 Azure SQL Database 연결 문자열을 가져옵니다.
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();
}
}
}
자세한 내용은 관리 ID를 사용하여 비밀 없이 App Service에서 Azure 데이터베이스에 연결을 참조하세요.
Spring 애플리케이션의 경우 --client-type springboot
옵션을 사용하여 연결을 만드는 경우 서비스 커넥터는 값 형식이 jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI;
인 spring.datasource.url
속성을 Azure Spring Apps로 설정합니다.
Azure SQL Database에서 암호 없는 연결을 사용하도록 Java 애플리케이션 마이그레이션 자습서에 따라 애플리케이션을 업데이트합니다. 이전에 설정된 경우 spring.datasource.password
구성 속성을 제거하고 올바른 종속성을 추가해야 합니다.
종속성을 설치합니다.
python -m pip install pyodbc
서비스 커넥터가 추가한 환경 변수에서 Azure SQL Database 연결 구성을 가져옵니다. 아래 코드를 사용하는 경우 사용하려는 인증 유형에 대한 코드 조각 부분의 주석 처리를 제거합니다. Azure Container Apps를 컴퓨팅 서비스로 사용하고 있거나 코드 조각의 연결 문자열이 작동하지 않는 경우 Azure SQL Database에서 암호 없는 연결을 사용하도록 Python 애플리케이션 마이그레이션을 참조하여 액세스 토큰을 사용해 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)
- 종속성을 설치합니다.
npm install mssql
- 서비스 커넥터가 추가한 환경 변수에서 Azure SQL Database 연결 구성을 가져옵니다. 아래 코드를 사용하는 경우 사용하려는 인증 유형에 대한 코드 조각 부분의 주석 처리를 제거합니다.
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);
자세한 내용은 Microsoft SQL Server로의 클라이언트 프로그래밍 홈페이지를 참조하세요.
연결 문자열
Warning
사용 가능한 가장 안전한 인증 흐름을 사용하는 것이 권장됩니다. 이 절차에서 설명된 인증 흐름은 다른 흐름에는 없는 위험을 전달하며, 애플리케이션에서 매우 높은 신뢰 수준을 요구합니다. 이 흐름은 관리 ID와 같은 보다 안전한 다른 흐름을 실행할 수 없는 경우에만 사용되어야 합니다.
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL 데이터베이스 연결 문자열 |
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;Password=<sql-password> |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL 데이터베이스 연결 문자열 |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-database>;user=<sql-username>;password=<sql-password>; |
기본 환경 변수 이름 |
설명 |
샘플 값 |
spring.datasource.url |
Azure SQL 데이터베이스 데이터 원본 URL |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>; |
spring.datasource.username |
Azure SQL 데이터베이스 데이터 원본 사용자 이름 |
<sql-user> |
spring.datasource.password |
Azure SQL 데이터베이스 데이터 원본 암호 |
<sql-password> |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_SERVER |
Azure SQL Database 서버 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 포트 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 데이터베이스 |
<sql-database> |
AZURE_SQL_USER |
Azure SQL Database 이름 |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL 데이터베이스 암호 |
<sql-password> |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_HOST |
Azure SQL 데이터베이스 호스트 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 포트 |
1433 |
AZURE_SQL_NAME |
Azure SQL 데이터베이스 이름 |
<sql-database> |
AZURE_SQL_USER |
Azure SQL Database 이름 |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL 데이터베이스 암호 |
<sql-password> |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL 데이터베이스 연결 문자열 |
server=<sql-server>.database.windows.net;port=1433;database=<sql-database>;user id=<sql-username>;password=<sql-password>; |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_SERVER |
Azure SQL Database 서버 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 포트 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 데이터베이스 |
<sql-database> |
AZURE_SQL_USERNAME |
Azure SQL 데이터베이스 사용자 이름 |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL 데이터베이스 암호 |
<sql-password> |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_SERVERNAME |
Azure SQL 데이터베이스 서버 이름 |
<sql-server>.database.windows.net,1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 데이터베이스 |
<sql-database> |
AZURE_SQL_UID |
Azure SQL 데이터베이스 UID(고유 식별자) |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL 데이터베이스 암호 |
<sql-password> |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_HOST |
Azure SQL 데이터베이스 호스트 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 포트 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 데이터베이스 |
<sql-database> |
AZURE_SQL_USERNAME |
Azure SQL 데이터베이스 사용자 이름 |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL 데이터베이스 암호 |
<sql-password> |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_HOST |
Azure SQL 데이터베이스 호스트 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 포트 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 데이터베이스 |
<sql-database> |
AZURE_SQL_USERNAME |
Azure SQL 데이터베이스 사용자 이름 |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL 데이터베이스 암호 |
<sql-password> |
샘플 코드
연결 문자열을 사용하여 Azure SQL Database에 연결하려면 아래 단계 및 코드를 참조하세요.
종속성을 설치합니다.
dotnet add package Microsoft.Data.SqlClient
서비스 커넥터에서 추가한 환경 변수에서 Azure SQL Database 연결 문자열을 가져옵니다.
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
pom.xml 파일에서 다음 종속성을 추가합니다.
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>10.2.0.jre11</version>
</dependency>
서비스 커넥터에서 추가한 환경 변수에서 Azure SQL Database 연결 문자열을 가져옵니다.
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();
}
}
}
- 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>
- Spring 애플리케이션을 설정합니다. 연결 구성은 서비스 커넥터를 통해 Spring Apps에 추가됩니다.
종속성을 설치합니다.
python -m pip install pyodbc
서비스 커넥터가 추가한 환경 변수에서 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')
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)
종속성을 설치합니다.
pip install django
pip install pyodbc
설정 파일에서 서비스 커넥터가 추가한 환경 변수에서 Azure SQL Database 연결 구성을 가져옵니다.
# 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': databse,
'USER': user,
'PASSWORD': password,
'HOST': server,
'PORT': port,
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
},
},
}
종속성을 설치합니다.
go install github.com/microsoft/go-mssqldb@latest
서비스 커넥터에서 추가한 환경 변수에서 Azure SQL Database 연결 문자열을 가져옵니다.
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")
- 종속성을 설치합니다.
npm install mssql
- 서비스 커넥터가 추가한 환경 변수에서 Azure SQL Database 연결 구성을 가져옵니다.
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);
SQL Server용 Microsoft Drivers for PHP를 다운로드합니다. 자세한 내용은 SQL Server용 Microsoft Drivers for PHP 시작을 참조하세요.
서비스 커넥터가 추가한 환경 변수에서 Azure SQL Database 연결 구성을 가져옵니다.
<?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);
?>
SQL Server용 Ruby Driver를 다운로드합니다. 자세한 내용은 Ruby 개발을 위한 개발 환경 구성을 확인하세요.
서비스 커넥터가 추가한 환경 변수에서 Azure SQL Database 연결 구성을 가져옵니다.
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
자세한 내용은 Microsoft SQL Server로의 클라이언트 프로그래밍 홈페이지를 참조하세요.
서비스 주체
기본 환경 변수 이름 |
설명 |
예제 값 |
AZURE_SQL_CLIENTID |
클라이언트 ID |
<client-ID> |
AZURE_SQL_CLIENTSECRET |
클라이언트 암호 |
<client-secret> |
AZURE_SQL_TENANTID |
테넌트 ID |
<tenant-ID> |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL 데이터베이스 연결 문자열 |
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;User ID=<client-Id>;Password=<client-secret>;Authentication=ActiveDirectoryServicePrincipal |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL 데이터베이스 연결 문자열 |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-database>;user=<client-Id>;password=<client-secret>;authentication=ActiveDirectoryServicePrincipal; |
기본 환경 변수 이름 |
설명 |
샘플 값 |
spring.datasource.url |
Azure SQL 데이터베이스 데이터 원본 URL |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryServicePrincipal; |
spring.datasource.username |
Azure SQL 데이터베이스 데이터 원본 사용자 이름 |
<client-Id> |
spring.datasource.password |
Azure SQL 데이터베이스 데이터 원본 암호 |
<client-Secret> |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_SERVER |
Azure SQL Database 서버 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 포트 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 데이터베이스 |
<sql-database> |
AZURE_SQL_USER |
Azure SQL Database 이름 |
your Client Id |
AZURE_SQL_AUTHENTICATION |
Azure SQL 인증 |
ActiveDirectoryServerPrincipal |
AZURE_SQL_PASSWORD |
Azure SQL 데이터베이스 암호 |
your Client Secret |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_SERVER |
Azure SQL Database 서버 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 포트 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 데이터베이스 |
<sql-database> |
AZURE_SQL_AUTHENTICATIONTYPE |
Azure SQL Database 인증 유형 |
azure-active-directory-default |
AZURE_SQL_CLIENTID |
Azure SQL Database 클라이언트 ID |
<your Client ID> |
AZURE_SQL_CLIENTSECRET |
Azure SQL Database 클라이언트 암호 |
<your Client Secret > |
AZURE_SQL_TENANTID |
Azure SQL Database 테넌트 ID |
<your Tenant ID> |
기본 환경 변수 이름 |
설명 |
샘플 값 |
AZURE_SQL_HOST |
Azure SQL Database 서버 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 포트 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 데이터베이스 |
<sql-database> |
AZURE_SQL_AUTHENTICATION |
Azure SQL Database 인증 유형 |
azure-active-directory-default |
AZURE_SQL_USERNAME |
Azure SQL Database 클라이언트 ID |
<your Client ID> |
AZURE_SQL_PASSWORD |
Azure SQL Database 클라이언트 암호 |
<your Client Secret > |
샘플 코드
서비스 주체를 사용하여 Azure SQL Database에 연결하려면 아래 단계 및 코드를 참조하세요.
종속성을 설치합니다.
dotnet add package Microsoft.Data.SqlClient
서비스 커넥터에서 추가한 환경 변수에서 Azure SQL Database 연결 문자열을 가져옵니다.
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
자세한 내용은 Active Directory 관리 ID 인증 사용을 참조하세요.
pom.xml 파일에서 다음 종속성을 추가합니다.
<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>
서비스 커넥터에서 추가한 환경 변수에서 Azure SQL Database 연결 문자열을 가져옵니다.
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();
}
}
}
자세한 내용은 관리 ID를 사용하여 비밀 없이 App Service에서 Azure 데이터베이스에 연결을 참조하세요.
Spring 애플리케이션의 경우 --client-type springboot
옵션을 사용하여 연결을 만드는 경우 서비스 커넥터는 값 형식이 jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI;
인 spring.datasource.url
속성을 Azure Spring Apps로 설정합니다.
Azure SQL Database에서 암호 없는 연결을 사용하도록 Java 애플리케이션 마이그레이션 자습서에 따라 애플리케이션을 업데이트합니다. 이전에 설정된 경우 spring.datasource.password
구성 속성을 제거하고 올바른 종속성을 추가해야 합니다.
종속성을 설치합니다.
python -m pip install pyodbc
서비스 커넥터가 추가한 환경 변수에서 Azure SQL Database 연결 구성을 가져옵니다. 아래 코드를 사용하는 경우 사용하려는 인증 유형에 대한 코드 조각 부분의 주석 처리를 제거합니다. Azure Container Apps를 컴퓨팅 서비스로 사용하고 있거나 코드 조각의 연결 문자열이 작동하지 않는 경우 Azure SQL Database에서 암호 없는 연결을 사용하도록 Python 애플리케이션 마이그레이션을 참조하여 액세스 토큰을 사용해 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)
- 종속성을 설치합니다.
npm install mssql
- 서비스 커넥터가 추가한 환경 변수에서 Azure SQL Database 연결 구성을 가져옵니다. 아래 코드를 사용하는 경우 사용하려는 인증 유형에 대한 코드 조각 부분의 주석 처리를 제거합니다.
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);
자세한 내용은 Microsoft SQL Server로의 클라이언트 프로그래밍 홈페이지를 참조하세요.
다음 단계
서비스 커넥터에 대해 자세히 알아보려면 아래에 나와 있는 자습서를 따르세요.