この記事では、Service Connector を使用してアプリを Azure SQL Database に接続するために使用できる、サポートされている認証方法とクライアントについて説明します。 サポートされている各メソッドについて、サンプル コードを提供し、サービス接続の作成時に取得した既定の環境変数の名前、値、および構成について説明します。
サポートされているコンピューティング サービス
サービス コネクタを使うと、次のコンピューティング サービスを Azure SQL Database に接続できます。
- Azure App Service
- Azure Container Apps
- Azure Functions
- Azure Kubernetes Service (AKS)
- Azure Spring Apps
サポートされている認証の種類とクライアント
次の表は、Service Connector を使用してコンピューティング サービスを Azure SQL Database に接続するためにサポートされる認証方法とクライアントの組み合わせを示しています。
[はい] は組み合わせがサポートされていることを示し、No はサポートされていないことを示します。
| クライアントの種類 |
システム割り当てマネージド ID |
ユーザー割り当てマネージド ID |
シークレット/接続文字列 |
サービス プリンシパル |
| .NET |
はい |
はい |
はい |
はい |
| Go |
いいえ |
いいえ |
はい |
いいえ |
| Java |
はい |
はい |
はい |
はい |
| Java - Spring Boot |
はい |
はい |
はい |
はい |
| Node.js |
はい |
はい |
はい |
はい |
| PHP |
いいえ |
いいえ |
はい |
いいえ |
| Python |
はい |
はい |
はい |
はい |
| Python - Django |
いいえ |
いいえ |
はい |
いいえ |
| Ruby |
いいえ |
いいえ |
はい |
いいえ |
| なし |
はい |
はい |
はい |
はい |
Note
システム割り当てマネージド ID、ユーザー割り当てマネージド ID、サービス プリンシパル認証は、Azure CLI でのみサポートされます。
既定の環境変数名またはアプリケーション プロパティとサンプル コード
コンピューティング サービスを Azure SQL Database に接続するには、次の接続の詳細を使用します。 各例で、 <sql-server>、 <sql-database>、 <sql-username>、 <sql-password> のプレースホルダー テキストを、独自のサーバー名、データベース名、ユーザー ID、パスワードに置き換えます。 名前付け規則の詳細については、「Service Connector の内部構造」を参照してください。
システム割り当てマネージド ID
| 既定の環境変数名 |
説明 |
値の例 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database の接続文字列 |
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;Authentication=ActiveDirectoryManagedIdentity |
| 既定の環境変数名 |
説明 |
値の例 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database の接続文字列 |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-database>;authentication=ActiveDirectoryMSI; |
| 既定の環境変数名 |
説明 |
値の例 |
spring.datasource.url |
Azure SQL Database データソースの 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
Service Connector によって追加された環境変数から Azure SQL Database 接続文字列を取得します。
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
詳細については、「Active Directory Managed Identity 認証の使用」を参照してください。
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>
Service Connector によって追加された環境変数から 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 を使用して接続を作成すると、Service Connector によって、プロパティ spring.datasource.url が値形式 jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI; を使用して Azure Spring Apps に設定されます。
チュートリアル「Azure SQL Database でパスワードレス接続を使用するように Java アプリケーションを移行する」に従って、アプリケーションを更新します。
spring.datasource.password 構成プロパティが以前に設定されている場合は必ず削除し、正しい依存関係を追加します。
依存関係をインストールします。
python -m pip install mssql-python python-dotenv
Service Connector によって追加された環境変数から Azure SQL Database 接続構成を取得します。 次のコードを使用する場合は、コード スニペットで、使用する認証の種類に対応する部分をコメント解除します。
import os
from mssql_python import connect
server = os.getenv('AZURE_SQL_SERVER')
port = os.getenv('AZURE_SQL_PORT')
database = os.getenv('AZURE_SQL_DATABASE')
# Uncomment the following lines corresponding to the authentication type you want to use.
# For system-assigned managed identity.
# connection_string = f'Server={server},{port};Database={database};Authentication=ActiveDirectoryMSI;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# For user-assigned managed identity.
# client_id = os.getenv('AZURE_SQL_USER')
# connection_string = f'Server={server},{port};Database={database};UID={client_id};Authentication=ActiveDirectoryMSI;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# For service principal.
# user = os.getenv('AZURE_SQL_USER')
# password = os.getenv('AZURE_SQL_PASSWORD')
# connection_string = f'Server={server},{port};Database={database};UID={user};PWD={password};Authentication=ActiveDirectoryServicePrincipal;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
conn = connect(connection_string)
- 依存関係をインストールします。
npm install mssql
- Service Connector によって追加された環境変数から 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 Database の接続文字列 |
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;User ID=<identity-client-ID>;Authentication=ActiveDirectoryManagedIdentity |
| 既定の環境変数名 |
説明 |
値の例 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database の接続文字列 |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-database>;msiClientId=<msiClientId>;authentication=ActiveDirectoryMSI; |
| 既定の環境変数名 |
説明 |
値の例 |
spring.datasource.url |
Azure SQL Database データソースの 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
Service Connector によって追加された環境変数から Azure SQL Database 接続文字列を取得します。
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
詳細については、「Active Directory Managed Identity 認証の使用」を参照してください。
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>
Service Connector によって追加された環境変数から 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 を使用して接続を作成すると、Service Connector によって、プロパティ spring.datasource.url が値形式 jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI; を使用して Azure Spring Apps に設定されます。
チュートリアル「Azure SQL Database でパスワードレス接続を使用するように Java アプリケーションを移行する」に従って、アプリケーションを更新します。
spring.datasource.password 構成プロパティが以前に設定されている場合は必ず削除し、正しい依存関係を追加します。
依存関係をインストールします。
python -m pip install mssql-python python-dotenv
Service Connector によって追加された環境変数から Azure SQL Database 接続構成を取得します。 次のコードを使用する場合は、コード スニペットで、使用する認証の種類に対応する部分をコメント解除します。
import os
from mssql_python import connect
server = os.getenv('AZURE_SQL_SERVER')
port = os.getenv('AZURE_SQL_PORT')
database = os.getenv('AZURE_SQL_DATABASE')
# Uncomment the following lines corresponding to the authentication type you want to use.
# For system-assigned managed identity.
# connection_string = f'Server={server},{port};Database={database};Authentication=ActiveDirectoryMSI;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# For user-assigned managed identity.
# client_id = os.getenv('AZURE_SQL_USER')
# connection_string = f'Server={server},{port};Database={database};UID={client_id};Authentication=ActiveDirectoryMSI;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# For service principal.
# user = os.getenv('AZURE_SQL_USER')
# password = os.getenv('AZURE_SQL_PASSWORD')
# connection_string = f'Server={server},{port};Database={database};UID={user};PWD={password};Authentication=ActiveDirectoryServicePrincipal;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
conn = connect(connection_string)
- 依存関係をインストールします。
npm install mssql
- Service Connector によって追加された環境変数から 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 に対するクライアント プログラミングのホーム ページ」を参照してください。
接続文字列
警告
Microsoft では、利用可能な最も安全な認証フローを使用することをお勧めします。 この手順で説明する認証フローでは、アプリケーションに対する高度な信頼が必要であり、他のフローに存在しないリスクが伴います。 このフローは、マネージド ID など、より安全な他のフローが利用できない場合にのみ使用してください。
| 既定の環境変数名 |
説明 |
値の例 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database の接続文字列 |
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;Password=<sql-password> |
| 既定の環境変数名 |
説明 |
値の例 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database の接続文字列 |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-database>;user=<sql-username>;password=<sql-password>; |
| 既定の環境変数名 |
説明 |
値の例 |
spring.datasource.url |
Azure SQL Database データソースの URL |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>; |
spring.datasource.username |
Azure SQL Database データソースのユーザー名 |
<sql-user> |
spring.datasource.password |
Azure SQL Database データソースのパスワード |
<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 Database のパスワード |
<sql-password> |
| 既定の環境変数名 |
説明 |
値の例 |
AZURE_SQL_HOST |
Azure SQL Database のホスト |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database のポート |
1433 |
AZURE_SQL_NAME |
Azure SQL Database の名前 |
<sql-database> |
AZURE_SQL_USER |
Azure SQL Database のユーザー |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database のパスワード |
<sql-password> |
| 既定の環境変数名 |
説明 |
値の例 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database の接続文字列 |
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 Database のユーザー名 |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database のパスワード |
<sql-password> |
| 既定の環境変数名 |
説明 |
値の例 |
AZURE_SQL_SERVERNAME |
Azure SQL Database のサーバー名 |
<sql-server>.database.windows.net,1433 |
AZURE_SQL_DATABASE |
Azure SQL Database のデータベース |
<sql-database> |
AZURE_SQL_UID |
Azure SQL Database の一意識別子 (UID) |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database のパスワード |
<sql-password> |
| 既定の環境変数名 |
説明 |
値の例 |
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_USERNAME |
Azure SQL Database のユーザー名 |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database のパスワード |
<sql-password> |
| 既定の環境変数名 |
説明 |
値の例 |
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_USERNAME |
Azure SQL Database のユーザー名 |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database のパスワード |
<sql-password> |
サンプル コード
接続文字列を使用して Azure SQL Database に接続するには、次の手順とサンプル コードを参照してください。
依存関係をインストールします。
dotnet add package Microsoft.Data.SqlClient
Service Connector によって追加された環境変数から 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>
Service Connector によって追加された環境変数から 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();
}
}
}
- logstash の依存関係を '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 mssql-python python-dotenv
Service Connector によって追加された環境変数から Azure SQL Database 接続構成を取得します。
import os
from mssql_python import connect
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')
connection_string = f'Server={server},{port};Database={database};UID={user};PWD={password};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
conn = connect(connection_string)
依存関係をインストールします。
pip install django
pip install mssql-django pyodbc
設定ファイルで、Service Connector によって追加された環境変数から 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': 'mssql',
'NAME': database,
'USER': user,
'PASSWORD': password,
'HOST': server,
'PORT': port,
'OPTIONS': {
'driver': 'ODBC Driver 18 for SQL Server',
},
},
}
依存関係をインストールする。
go install github.com/microsoft/go-mssqldb@latest
Service Connector によって追加された環境変数から 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
- Service Connector によって追加された環境変数から 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);
Microsoft SQL Server 用 Drivers for PHP をダウンロードする。 詳細については、「SQL Server 用 Microsoft Drivers for PHP の概要」を参照してください。
Service Connector によって追加された環境変数から 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 ドライバーをダウンロードします。 詳しくは、「Ruby 開発用に開発環境を構成する」をご覧ください。
Service Connector によって追加された環境変数から 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 Database の接続文字列 |
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 Database の接続文字列 |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-database>;user=<client-Id>;password=<client-secret>;authentication=ActiveDirectoryServicePrincipal; |
| 既定の環境変数名 |
説明 |
値の例 |
spring.datasource.url |
Azure SQL Database データソースの URL |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryServicePrincipal; |
spring.datasource.username |
Azure SQL Database データソースのユーザー名 |
<client-Id> |
spring.datasource.password |
Azure SQL Database データソースのパスワード |
<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 Database のパスワード |
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
Service Connector によって追加された環境変数から Azure SQL Database 接続文字列を取得します。
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
詳細については、「Active Directory Managed Identity 認証の使用」を参照してください。
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>
Service Connector によって追加された環境変数から 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 を使用して接続を作成すると、Service Connector によって、プロパティ spring.datasource.url が値形式 jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI; を使用して Azure Spring Apps に設定されます。
チュートリアル「Azure SQL Database でパスワードレス接続を使用するように Java アプリケーションを移行する」に従って、アプリケーションを更新します。
spring.datasource.password 構成プロパティが以前に設定されている場合は必ず削除し、正しい依存関係を追加します。
依存関係をインストールします。
python -m pip install mssql-python python-dotenv
Service Connector によって追加された環境変数から Azure SQL Database 接続構成を取得します。 次のコードを使用する場合は、コード スニペットで、使用する認証の種類に対応する部分をコメント解除します。
import os
from mssql_python import connect
server = os.getenv('AZURE_SQL_SERVER')
port = os.getenv('AZURE_SQL_PORT')
database = os.getenv('AZURE_SQL_DATABASE')
# Uncomment the following lines corresponding to the authentication type you want to use.
# For system-assigned managed identity.
# connection_string = f'Server={server},{port};Database={database};Authentication=ActiveDirectoryMSI;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# For user-assigned managed identity.
# client_id = os.getenv('AZURE_SQL_USER')
# connection_string = f'Server={server},{port};Database={database};UID={client_id};Authentication=ActiveDirectoryMSI;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# For service principal.
# user = os.getenv('AZURE_SQL_USER')
# password = os.getenv('AZURE_SQL_PASSWORD')
# connection_string = f'Server={server},{port};Database={database};UID={user};PWD={password};Authentication=ActiveDirectoryServicePrincipal;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
conn = connect(connection_string)
- 依存関係をインストールします。
npm install mssql
- Service Connector によって追加された環境変数から 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 に対するクライアント プログラミングのホーム ページ」を参照してください。
次のステップ
Service Connector の詳細については、次のチュートリアルを参照してください。