Mengintegrasikan Azure Database for MySQL dengan Konektor Layanan
Artikel
Halaman ini memperlihatkan metode dan klien autentikasi yang didukung, dan memperlihatkan kode sampel yang dapat Anda gunakan untuk menyambungkan Azure Database for MySQL - Server Fleksibel ke layanan cloud lain menggunakan Konektor Layanan. Halaman ini juga menunjukkan nama dan nilai variabel lingkungan default (atau konfigurasi Spring Boot) yang Anda dapatkan saat membuat koneksi layanan.
Penting
Server tunggal Azure Database for MySQL berada di jalur penghentian. Kami sangat menyarankan Agar Anda meningkatkan ke server fleksibel Azure Database for MySQL. Untuk informasi selengkapnya tentang migrasi ke server fleksibel Azure Database for MySQL, lihat Apa yang terjadi pada Server Tunggal Azure Database for MySQL?
Layanan komputasi yang didukung
Konektor Layanan dapat digunakan untuk menyambungkan layanan komputasi berikut ke Azure Database for MySQL:
Azure App Service
Azure Container Apps
Azure Functions
Azure Kubernetes Service (AKS)
Azure Spring Apps
Jenis Autentikasi yang Didukung serta jenis klien
Tabel di bawah ini memperlihatkan kombinasi metode autentikasi dan klien mana yang didukung untuk menyambungkan layanan komputasi Anda ke Azure Database for MySQL menggunakan Konektor Layanan. "Ya" menunjukkan bahwa kombinasi didukung, sementara "Tidak" menunjukkan bahwa kombinasi tersebut tidak didukung.
Jenis klien
Identitas terkelola yang ditetapkan sistem
Identitas terkelola yang ditetapkan pengguna
Rahasia/string koneksi
Perwakilan layanan
.NET
Ya
Ya
Ya
Ya
Go (go-sql-driver for mysql)
Ya
Ya
Ya
Ya
Java (JDBC)
Ya
Ya
Ya
Ya
Java - Spring Boot (JDBC)
Ya
Ya
Ya
Ya
Node.js (mysql)
Ya
Ya
Ya
Ya
Python (mysql-connector-python)
Ya
Ya
Ya
Ya
Python-Django
Ya
Ya
Ya
Ya
PHP (MySQLi)
Ya
Ya
Ya
Ya
Ruby (mysql2)
Ya
Ya
Ya
Ya
Tidak
Ya
Ya
Ya
Ya
Tabel ini menunjukkan bahwa semua kombinasi jenis klien dan metode autentikasi dalam tabel didukung. Semua jenis klien dapat menggunakan salah satu metode autentikasi untuk menyambungkan ke Azure Database for MySQL menggunakan Konektor Layanan.
Catatan
Identitas terkelola yang ditetapkan sistem, Identitas terkelola yang ditetapkan pengguna, dan Perwakilan layanan hanya didukung di Azure CLI.
Nama variabel lingkungan default atau properti aplikasi dan kode sampel
Referensikan detail koneksi dan kode sampel dalam tabel berikut, sesuai dengan jenis autentikasi koneksi dan jenis klien Anda, untuk menyambungkan layanan komputasi ke Azure Database for MySQL. Untuk informasi selengkapnya tentang konvensi penamaan, periksa artikel internal Konektor Layanan.
Untuk .NET, tidak ada plugin atau pustaka untuk mendukung koneksi tanpa kata sandi. Anda bisa mendapatkan token akses untuk identitas terkelola atau perwakilan layanan menggunakan pustaka klien seperti Azure.Identity. Kemudian Anda dapat menggunakan token akses sebagai kata sandi untuk menyambungkan ke database. Saat menggunakan kode di bawah ini, batalkan komentar bagian cuplikan kode untuk jenis autentikasi yang ingin Anda gunakan.
using Azure.Core;
using Azure.Identity;
using MySqlConnector;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned managed identity.
// var credential = new DefaultAzureCredential();
// For user-assigned managed identity.
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_MYSQL_CLIENTID");
// });
// For service principal.
// var tenantId = Environment.GetEnvironmentVariable("AZURE_MYSQL_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_MYSQL_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_MYSQL_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var tokenRequestContext = new TokenRequestContext(
new[] { "https://ossrdbms-aad.database.windows.net/.default" });
AccessToken accessToken = await credential.GetTokenAsync(tokenRequestContext);
// Open a connection to the MySQL server using the access token.
string connectionString =
$"{Environment.GetEnvironmentVariable("AZURE_MYSQL_CONNECTIONSTRING")};Password={accessToken.Token}";
using var connection = new MySqlConnection(connectionString);
Console.WriteLine("Opening connection using access token...");
await connection.OpenAsync();
// do something
Tambahkan dependensi berikut dalam file pom.xml Anda:
Untuk aplikasi Spring, jika Anda membuat koneksi dengan opsi --client-type springboot, Konektor Layanan mengatur properti spring.datasource.azure.passwordless-enabled, spring.datasource.url, dan spring.datasource.username ke Azure Spring Apps.
Autentikasi dengan token akses dapatkan melalui azure-identity pustaka dan dapatkan informasi koneksi dari variabel lingkungan yang ditambahkan oleh Konektor Layanan. Saat menggunakan kode di bawah ini, batalkan komentar bagian cuplikan kode untuk jenis autentikasi yang ingin Anda gunakan.
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
import mysql.connector
import os
# Uncomment the following lines corresponding to the authentication type you want to use.
# For system-assigned managed identity.
# cred = ManagedIdentityCredential()
# For user-assigned managed identity.
# managed_identity_client_id = os.getenv('AZURE_MYSQL_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# For service principal
# tenant_id = os.getenv('AZURE_MYSQL_TENANTID')
# client_id = os.getenv('AZURE_MYSQL_CLIENTID')
# client_secret = os.getenv('AZURE_MYSQL_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
# acquire token
accessToken = cred.get_token('https://ossrdbms-aad.database.windows.net/.default')
# open connect to Azure MySQL with the access token.
host = os.getenv('AZURE_MYSQL_HOST')
database = os.getenv('AZURE_MYSQL_NAME')
user = os.getenv('AZURE_MYSQL_USER')
password = accessToken.token
cnx = mysql.connector.connect(user=user,
password=password,
host=host,
database=database)
cnx.close()
Instal dependensi.
pip install azure-identity
Dapatkan token akses melalui azure-identity pustaka dengan variabel lingkungan yang ditambahkan oleh Konektor Layanan. Saat menggunakan kode di bawah ini, batalkan komentar bagian cuplikan kode untuk jenis autentikasi yang ingin Anda gunakan.
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
import os
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_MYSQL_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_MYSQL_TENANTID')
# client_id = os.getenv('AZURE_MYSQL_CLIENTID')
# client_secret = os.getenv('AZURE_MYSQL_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
# acquire token
accessToken = cred.get_token('https://ossrdbms-aad.database.windows.net/.default')
Dalam mengatur file, dapatkan informasi database Azure MySQL dari variabel lingkungan yang ditambahkan oleh layanan Konektor Layanan. Gunakan accessToken yang diperoleh pada langkah sebelumnya untuk mengakses database.
# in your setting file, eg. settings.py
host = os.getenv('AZURE_MYSQL_HOST')
database = os.getenv('AZURE_MYSQL_NAME')
user = os.getenv('AZURE_MYSQL_USER')
password = accessToken.token # this is accessToken acquired from above step.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': database,
'USER': user,
'PASSWORD': password,
'HOST': host
}
}
Instal dependensi.
go get "github.com/go-sql-driver/mysql"
go get "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
go get "github.com/Azure/azure-sdk-for-go/sdk/azcore"
Dalam kode, dapatkan token akses melalui azidentity, lalu sambungkan ke Azure MySQL dengan token. Saat menggunakan kode di bawah ini, batalkan komentar bagian cuplikan kode untuk jenis autentikasi yang ingin Anda gunakan.
import (
"context"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/go-sql-driver/mysql"
)
func main() {
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// for user-assigned managed identity
// clientid := os.Getenv("AZURE_MYSQL_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// for service principal
// clientid := os.Getenv("AZURE_MYSQL_CLIENTID")
// tenantid := os.Getenv("AZURE_MYSQL_TENANTID")
// clientsecret := os.Getenv("AZURE_MYSQL_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
if err != nil {
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
token, err := cred.GetToken(ctx, policy.TokenRequestOptions{
Scopes: []string("https://ossrdbms-aad.database.windows.net/.default"),
})
connectionString := os.Getenv("AZURE_MYSQL_CONNECTIONSTRING") + ";Password=" + token.Token
db, err := sql.Open("mysql", connectionString)
}
Dapatkan token akses menggunakan @azure/identity dan informasi database Azure MySQL dari variabel lingkungan yang ditambahkan oleh layanan Konektor Layanan. Saat menggunakan kode di bawah ini, batalkan komentar bagian cuplikan kode untuk jenis autentikasi yang ingin Anda gunakan.
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
const mysql = require('mysql2');
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_MYSQL_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_MYSQL_TENANTID;
// const clientId = process.env.AZURE_MYSQL_CLIENTID;
// const clientSecret = process.env.AZURE_MYSQL_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
// acquire token
var accessToken = await credential.getToken('https://ossrdbms-aad.database.windows.net/.default');
const connection = mysql.createConnection({
host: process.env.AZURE_MYSQL_HOST,
user: process.env.AZURE_MYSQL_USER,
password: accessToken.token,
database: process.env.AZURE_MYSQL_DATABASE,
port: process.env.AZURE_MYSQL_PORT,
ssl: process.env.AZURE_MYSQL_SSL
});
connection.connect((err) => {
if (err) {
console.error('Error connecting to MySQL database: ' + err.stack);
return;
}
console.log('Connected to MySQL database');
});
Untuk bahasa lain, gunakan string koneksi dan nama pengguna yang diatur Konektor Layanan ke variabel lingkungan untuk menyambungkan database. Untuk detail variabel lingkungan, lihat Mengintegrasikan Azure Database for MySQL dengan Konektor Layanan.
Untuk bahasa lain, gunakan string koneksi dan nama pengguna yang diatur Konektor Layanan ke variabel lingkungan untuk menyambungkan database. Untuk detail variabel lingkungan, lihat Mengintegrasikan Azure Database for MySQL dengan Konektor Layanan.
Untuk bahasa lain, gunakan properti koneksi yang diatur Konektor Layanan ke variabel lingkungan untuk menyambungkan database. Untuk detail variabel lingkungan, lihat Mengintegrasikan Azure Database for MySQL dengan Konektor Layanan.
Untuk .NET, tidak ada plugin atau pustaka untuk mendukung koneksi tanpa kata sandi. Anda bisa mendapatkan token akses untuk identitas terkelola atau perwakilan layanan menggunakan pustaka klien seperti Azure.Identity. Kemudian Anda dapat menggunakan token akses sebagai kata sandi untuk menyambungkan ke database. Saat menggunakan kode di bawah ini, batalkan komentar bagian cuplikan kode untuk jenis autentikasi yang ingin Anda gunakan.
using Azure.Core;
using Azure.Identity;
using MySqlConnector;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned managed identity.
// var credential = new DefaultAzureCredential();
// For user-assigned managed identity.
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_MYSQL_CLIENTID");
// });
// For service principal.
// var tenantId = Environment.GetEnvironmentVariable("AZURE_MYSQL_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_MYSQL_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_MYSQL_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var tokenRequestContext = new TokenRequestContext(
new[] { "https://ossrdbms-aad.database.windows.net/.default" });
AccessToken accessToken = await credential.GetTokenAsync(tokenRequestContext);
// Open a connection to the MySQL server using the access token.
string connectionString =
$"{Environment.GetEnvironmentVariable("AZURE_MYSQL_CONNECTIONSTRING")};Password={accessToken.Token}";
using var connection = new MySqlConnection(connectionString);
Console.WriteLine("Opening connection using access token...");
await connection.OpenAsync();
// do something
Tambahkan dependensi berikut dalam file pom.xml Anda:
Untuk aplikasi Spring, jika Anda membuat koneksi dengan opsi --client-type springboot, Konektor Layanan mengatur properti spring.datasource.azure.passwordless-enabled, spring.datasource.url, dan spring.datasource.username ke Azure Spring Apps.
Autentikasi dengan token akses dapatkan melalui azure-identity pustaka dan dapatkan informasi koneksi dari variabel lingkungan yang ditambahkan oleh Konektor Layanan. Saat menggunakan kode di bawah ini, batalkan komentar bagian cuplikan kode untuk jenis autentikasi yang ingin Anda gunakan.
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
import mysql.connector
import os
# Uncomment the following lines corresponding to the authentication type you want to use.
# For system-assigned managed identity.
# cred = ManagedIdentityCredential()
# For user-assigned managed identity.
# managed_identity_client_id = os.getenv('AZURE_MYSQL_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# For service principal
# tenant_id = os.getenv('AZURE_MYSQL_TENANTID')
# client_id = os.getenv('AZURE_MYSQL_CLIENTID')
# client_secret = os.getenv('AZURE_MYSQL_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
# acquire token
accessToken = cred.get_token('https://ossrdbms-aad.database.windows.net/.default')
# open connect to Azure MySQL with the access token.
host = os.getenv('AZURE_MYSQL_HOST')
database = os.getenv('AZURE_MYSQL_NAME')
user = os.getenv('AZURE_MYSQL_USER')
password = accessToken.token
cnx = mysql.connector.connect(user=user,
password=password,
host=host,
database=database)
cnx.close()
Instal dependensi.
pip install azure-identity
Dapatkan token akses melalui azure-identity pustaka dengan variabel lingkungan yang ditambahkan oleh Konektor Layanan. Saat menggunakan kode di bawah ini, batalkan komentar bagian cuplikan kode untuk jenis autentikasi yang ingin Anda gunakan.
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
import os
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_MYSQL_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_MYSQL_TENANTID')
# client_id = os.getenv('AZURE_MYSQL_CLIENTID')
# client_secret = os.getenv('AZURE_MYSQL_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
# acquire token
accessToken = cred.get_token('https://ossrdbms-aad.database.windows.net/.default')
Dalam mengatur file, dapatkan informasi database Azure MySQL dari variabel lingkungan yang ditambahkan oleh layanan Konektor Layanan. Gunakan accessToken yang diperoleh pada langkah sebelumnya untuk mengakses database.
# in your setting file, eg. settings.py
host = os.getenv('AZURE_MYSQL_HOST')
database = os.getenv('AZURE_MYSQL_NAME')
user = os.getenv('AZURE_MYSQL_USER')
password = accessToken.token # this is accessToken acquired from above step.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': database,
'USER': user,
'PASSWORD': password,
'HOST': host
}
}
Instal dependensi.
go get "github.com/go-sql-driver/mysql"
go get "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
go get "github.com/Azure/azure-sdk-for-go/sdk/azcore"
Dalam kode, dapatkan token akses melalui azidentity, lalu sambungkan ke Azure MySQL dengan token. Saat menggunakan kode di bawah ini, batalkan komentar bagian cuplikan kode untuk jenis autentikasi yang ingin Anda gunakan.
import (
"context"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/go-sql-driver/mysql"
)
func main() {
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// for user-assigned managed identity
// clientid := os.Getenv("AZURE_MYSQL_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// for service principal
// clientid := os.Getenv("AZURE_MYSQL_CLIENTID")
// tenantid := os.Getenv("AZURE_MYSQL_TENANTID")
// clientsecret := os.Getenv("AZURE_MYSQL_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
if err != nil {
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
token, err := cred.GetToken(ctx, policy.TokenRequestOptions{
Scopes: []string("https://ossrdbms-aad.database.windows.net/.default"),
})
connectionString := os.Getenv("AZURE_MYSQL_CONNECTIONSTRING") + ";Password=" + token.Token
db, err := sql.Open("mysql", connectionString)
}
Dapatkan token akses menggunakan @azure/identity dan informasi database Azure MySQL dari variabel lingkungan yang ditambahkan oleh layanan Konektor Layanan. Saat menggunakan kode di bawah ini, batalkan komentar bagian cuplikan kode untuk jenis autentikasi yang ingin Anda gunakan.
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
const mysql = require('mysql2');
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_MYSQL_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_MYSQL_TENANTID;
// const clientId = process.env.AZURE_MYSQL_CLIENTID;
// const clientSecret = process.env.AZURE_MYSQL_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
// acquire token
var accessToken = await credential.getToken('https://ossrdbms-aad.database.windows.net/.default');
const connection = mysql.createConnection({
host: process.env.AZURE_MYSQL_HOST,
user: process.env.AZURE_MYSQL_USER,
password: accessToken.token,
database: process.env.AZURE_MYSQL_DATABASE,
port: process.env.AZURE_MYSQL_PORT,
ssl: process.env.AZURE_MYSQL_SSL
});
connection.connect((err) => {
if (err) {
console.error('Error connecting to MySQL database: ' + err.stack);
return;
}
console.log('Connected to MySQL database');
});
Untuk bahasa lain, gunakan string koneksi dan nama pengguna yang diatur Konektor Layanan ke variabel lingkungan untuk menyambungkan database. Untuk detail variabel lingkungan, lihat Mengintegrasikan Azure Database for MySQL dengan Konektor Layanan.
Untuk bahasa lain, gunakan string koneksi dan nama pengguna yang diatur Konektor Layanan ke variabel lingkungan untuk menyambungkan database. Untuk detail variabel lingkungan, lihat Mengintegrasikan Azure Database for MySQL dengan Konektor Layanan.
Untuk bahasa lain, gunakan properti koneksi yang diatur Konektor Layanan ke variabel lingkungan untuk menyambungkan database. Untuk detail variabel lingkungan, lihat Mengintegrasikan Azure Database for MySQL dengan Konektor Layanan.
Microsoft menyarankan agar Anda menggunakan alur autentikasi paling aman yang tersedia. Alur autentikasi yang dijelaskan dalam prosedur ini membutuhkan tingkat kepercayaan yang sangat tinggi pada aplikasi, dan membawa risiko yang tidak ada dalam alur lain. Anda hanya boleh menggunakan alur ini ketika alur lain yang lebih aman, seperti identitas terkelola, tidak layak.
Setelah membuat springboot koneksi jenis klien, layanan Konektor Layanan akan secara otomatis menambahkan properti spring.datasource.url, , spring.datasource.usernamespring.datasource.password. Jadi aplikasi boot Spring dapat menambahkan kacang secara otomatis.
Dalam kode, dapatkan mySQL string koneksi dari variabel lingkungan yang ditambahkan oleh layanan Konektor Layanan. Untuk membuat koneksi terenkripsi ke server MySQL melalui SSL, lihat langkah-langkah ini.
using System;
using System.Data;
using MySql.Data.MySqlClient;
string connectionString = Environment.GetEnvironmentVariable("AZURE_MYSQL_CONNECTIONSTRING");
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
}
Dalam kode, dapatkan mySQL string koneksi dari variabel lingkungan yang ditambahkan oleh layanan Konektor Layanan. Untuk membuat koneksi terenkripsi ke server MySQL melalui SSL, lihat langkah-langkah ini.
Siapkan aplikasi Spring App normal, detail selengkapnya di bagian ini. Untuk membuat koneksi terenkripsi ke server MySQL melalui SSL, lihat langkah-langkah ini.
Dalam kode, dapatkan informasi koneksi MySQL dari variabel lingkungan yang ditambahkan oleh layanan Konektor Layanan. Untuk membuat koneksi terenkripsi ke server MySQL melalui SSL, lihat langkah-langkah ini.
Dalam mengatur file, dapatkan informasi database MySQL dari variabel lingkungan yang ditambahkan oleh layanan Konektor Layanan. Untuk membuat koneksi terenkripsi ke server MySQL melalui SSL, lihat langkah-langkah ini.
Dalam kode, dapatkan mySQL string koneksi dari variabel lingkungan yang ditambahkan oleh layanan Konektor Layanan. Untuk membuat koneksi terenkripsi ke server MySQL melalui SSL, lihat langkah-langkah ini.
Dalam kode, dapatkan informasi koneksi MySQL dari variabel lingkungan yang ditambahkan oleh layanan Konektor Layanan. Untuk membuat koneksi terenkripsi ke server MySQL melalui SSL, lihat langkah-langkah ini.
const mysql = require('mysql2')
const connection = mysql.createConnection({
host: process.env.AZURE_MYSQL_HOST,
user: process.env.AZURE_MYSQL_USER,
password: process.env.AZURE_MYSQL_PASSWORD,
database: process.env.AZURE_MYSQL_DATABASE,
port: Number(process.env.AZURE_MYSQL_PORT) ,
// ssl: process.env.AZURE_MYSQL_SSL
});
connection.connect((err) => {
if (err) {
console.error('Error connecting to MySQL database: ' + err.stack);
return;
}
console.log('Connected to MySQL database.');
});
Dalam kode, dapatkan informasi koneksi MySQL dari variabel lingkungan yang ditambahkan oleh layanan Konektor Layanan. Untuk membuat koneksi terenkripsi ke server MySQL melalui SSL, lihat langkah-langkah ini.
Dalam kode, dapatkan informasi koneksi MySQL dari variabel lingkungan yang ditambahkan oleh layanan Konektor Layanan. Untuk membuat koneksi terenkripsi ke server MySQL melalui SSL, lihat langkah-langkah ini.
Untuk bahasa lain, gunakan properti koneksi yang diatur Konektor Layanan ke variabel lingkungan untuk menyambungkan database. Untuk detail variabel lingkungan, lihat Mengintegrasikan Azure Database for MySQL dengan Konektor Layanan.
Untuk .NET, tidak ada plugin atau pustaka untuk mendukung koneksi tanpa kata sandi. Anda bisa mendapatkan token akses untuk identitas terkelola atau perwakilan layanan menggunakan pustaka klien seperti Azure.Identity. Kemudian Anda dapat menggunakan token akses sebagai kata sandi untuk menyambungkan ke database. Saat menggunakan kode di bawah ini, batalkan komentar bagian cuplikan kode untuk jenis autentikasi yang ingin Anda gunakan.
using Azure.Core;
using Azure.Identity;
using MySqlConnector;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned managed identity.
// var credential = new DefaultAzureCredential();
// For user-assigned managed identity.
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_MYSQL_CLIENTID");
// });
// For service principal.
// var tenantId = Environment.GetEnvironmentVariable("AZURE_MYSQL_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_MYSQL_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_MYSQL_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var tokenRequestContext = new TokenRequestContext(
new[] { "https://ossrdbms-aad.database.windows.net/.default" });
AccessToken accessToken = await credential.GetTokenAsync(tokenRequestContext);
// Open a connection to the MySQL server using the access token.
string connectionString =
$"{Environment.GetEnvironmentVariable("AZURE_MYSQL_CONNECTIONSTRING")};Password={accessToken.Token}";
using var connection = new MySqlConnection(connectionString);
Console.WriteLine("Opening connection using access token...");
await connection.OpenAsync();
// do something
Tambahkan dependensi berikut dalam file pom.xml Anda:
Untuk aplikasi Spring, jika Anda membuat koneksi dengan opsi --client-type springboot, Konektor Layanan mengatur properti spring.datasource.azure.passwordless-enabled, spring.datasource.url, dan spring.datasource.username ke Azure Spring Apps.
Autentikasi dengan token akses dapatkan melalui azure-identity pustaka dan dapatkan informasi koneksi dari variabel lingkungan yang ditambahkan oleh Konektor Layanan. Saat menggunakan kode di bawah ini, batalkan komentar bagian cuplikan kode untuk jenis autentikasi yang ingin Anda gunakan.
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
import mysql.connector
import os
# Uncomment the following lines corresponding to the authentication type you want to use.
# For system-assigned managed identity.
# cred = ManagedIdentityCredential()
# For user-assigned managed identity.
# managed_identity_client_id = os.getenv('AZURE_MYSQL_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# For service principal
# tenant_id = os.getenv('AZURE_MYSQL_TENANTID')
# client_id = os.getenv('AZURE_MYSQL_CLIENTID')
# client_secret = os.getenv('AZURE_MYSQL_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
# acquire token
accessToken = cred.get_token('https://ossrdbms-aad.database.windows.net/.default')
# open connect to Azure MySQL with the access token.
host = os.getenv('AZURE_MYSQL_HOST')
database = os.getenv('AZURE_MYSQL_NAME')
user = os.getenv('AZURE_MYSQL_USER')
password = accessToken.token
cnx = mysql.connector.connect(user=user,
password=password,
host=host,
database=database)
cnx.close()
Instal dependensi.
pip install azure-identity
Dapatkan token akses melalui azure-identity pustaka dengan variabel lingkungan yang ditambahkan oleh Konektor Layanan. Saat menggunakan kode di bawah ini, batalkan komentar bagian cuplikan kode untuk jenis autentikasi yang ingin Anda gunakan.
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
import os
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_MYSQL_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_MYSQL_TENANTID')
# client_id = os.getenv('AZURE_MYSQL_CLIENTID')
# client_secret = os.getenv('AZURE_MYSQL_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
# acquire token
accessToken = cred.get_token('https://ossrdbms-aad.database.windows.net/.default')
Dalam mengatur file, dapatkan informasi database Azure MySQL dari variabel lingkungan yang ditambahkan oleh layanan Konektor Layanan. Gunakan accessToken yang diperoleh pada langkah sebelumnya untuk mengakses database.
# in your setting file, eg. settings.py
host = os.getenv('AZURE_MYSQL_HOST')
database = os.getenv('AZURE_MYSQL_NAME')
user = os.getenv('AZURE_MYSQL_USER')
password = accessToken.token # this is accessToken acquired from above step.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': database,
'USER': user,
'PASSWORD': password,
'HOST': host
}
}
Instal dependensi.
go get "github.com/go-sql-driver/mysql"
go get "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
go get "github.com/Azure/azure-sdk-for-go/sdk/azcore"
Dalam kode, dapatkan token akses melalui azidentity, lalu sambungkan ke Azure MySQL dengan token. Saat menggunakan kode di bawah ini, batalkan komentar bagian cuplikan kode untuk jenis autentikasi yang ingin Anda gunakan.
import (
"context"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/go-sql-driver/mysql"
)
func main() {
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// for user-assigned managed identity
// clientid := os.Getenv("AZURE_MYSQL_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// for service principal
// clientid := os.Getenv("AZURE_MYSQL_CLIENTID")
// tenantid := os.Getenv("AZURE_MYSQL_TENANTID")
// clientsecret := os.Getenv("AZURE_MYSQL_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
if err != nil {
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
token, err := cred.GetToken(ctx, policy.TokenRequestOptions{
Scopes: []string("https://ossrdbms-aad.database.windows.net/.default"),
})
connectionString := os.Getenv("AZURE_MYSQL_CONNECTIONSTRING") + ";Password=" + token.Token
db, err := sql.Open("mysql", connectionString)
}
Dapatkan token akses menggunakan @azure/identity dan informasi database Azure MySQL dari variabel lingkungan yang ditambahkan oleh layanan Konektor Layanan. Saat menggunakan kode di bawah ini, batalkan komentar bagian cuplikan kode untuk jenis autentikasi yang ingin Anda gunakan.
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
const mysql = require('mysql2');
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_MYSQL_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_MYSQL_TENANTID;
// const clientId = process.env.AZURE_MYSQL_CLIENTID;
// const clientSecret = process.env.AZURE_MYSQL_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
// acquire token
var accessToken = await credential.getToken('https://ossrdbms-aad.database.windows.net/.default');
const connection = mysql.createConnection({
host: process.env.AZURE_MYSQL_HOST,
user: process.env.AZURE_MYSQL_USER,
password: accessToken.token,
database: process.env.AZURE_MYSQL_DATABASE,
port: process.env.AZURE_MYSQL_PORT,
ssl: process.env.AZURE_MYSQL_SSL
});
connection.connect((err) => {
if (err) {
console.error('Error connecting to MySQL database: ' + err.stack);
return;
}
console.log('Connected to MySQL database');
});
Untuk bahasa lain, gunakan string koneksi dan nama pengguna yang diatur Konektor Layanan ke variabel lingkungan untuk menyambungkan database. Untuk detail variabel lingkungan, lihat Mengintegrasikan Azure Database for MySQL dengan Konektor Layanan.
Untuk bahasa lain, gunakan string koneksi dan nama pengguna yang diatur Konektor Layanan ke variabel lingkungan untuk menyambungkan database. Untuk detail variabel lingkungan, lihat Mengintegrasikan Azure Database for MySQL dengan Konektor Layanan.
Untuk bahasa lain, gunakan properti koneksi yang diatur Konektor Layanan ke variabel lingkungan untuk menyambungkan database. Untuk detail variabel lingkungan, lihat Mengintegrasikan Azure Database for MySQL dengan Konektor Layanan.