Condividi tramite


Integrare Database di Azure per MySQL con Service Connessione or

Questa pagina mostra i metodi e i client di autenticazione supportati e mostra il codice di esempio che è possibile usare per connettersi Database di Azure per MySQL - Server flessibile ad altri servizi cloud tramite Service Connessione or. Questa pagina mostra anche i nomi e i valori predefiniti delle variabili di ambiente (o configurazione spring boot) che si ottengono quando si crea la connessione al servizio.

Importante

Database di Azure per MySQL server singolo si trova nel percorso di ritiro. È consigliabile eseguire l'aggiornamento a Database di Azure per MySQL server flessibile. Per altre informazioni sulla migrazione a Database di Azure per MySQL server flessibile, vedere Che cosa accade a Database di Azure per MySQL server singolo?

Servizi di calcolo supportati

Il Connessione or del servizio può essere usato per connettere i servizi di calcolo seguenti a Database di Azure per MySQL:

  • Servizio app di Azure
  • Funzioni di Azure
  • App contenitore di Azure
  • Azure Spring Apps

Tipi di autenticazione e tipi di client supportati

La tabella seguente illustra le combinazioni di metodi di autenticazione e client supportati per la connessione del servizio di calcolo a Database di Azure per MySQL tramite Service Connessione or. Un valore "Sì" indica che la combinazione è supportata, mentre "No" indica che non è supportata.

Tipo client Identità gestita assegnata dal sistema Identità gestita assegnata dall'utente Segreto/stringa di connessione Entità servizio
.NET
Go (go-sql-driver per mysql)
Java (JDBC)
Java - Spring Boot (JDBC)
Node.js (mysql)
Python (mysql-connector-python)
Python-Django
PHP (MySQLi)
Ruby (mysql2)
Nessuno

Questa tabella indica che tutte le combinazioni di tipi client e metodi di autenticazione nella tabella sono supportate. Tutti i tipi di client possono usare uno dei metodi di autenticazione per connettersi a Database di Azure per MySQL tramite Service Connessione or.

Nota

L'identità gestita assegnata dal sistema, l'identità gestita assegnata dall'utente e l'entità servizio sono supportate solo nell'interfaccia della riga di comando di Azure.

Nomi di variabili di ambiente predefiniti o proprietà dell'applicazione e codice di esempio

Fare riferimento ai dettagli della connessione e al codice di esempio nelle tabelle seguenti, in base al tipo di autenticazione e al tipo di client della connessione, per connettere i servizi di calcolo a Database di Azure per MySQL. Per altre informazioni sulle convenzioni di denominazione, vedere l'articolo Servizi Connessione or internals .

Identità gestita assegnata dal sistema

Nome variabile di ambiente predefinito Descrizione Valore di esempio
AZURE_MYSQL_CONNECTIONSTRING ADO.NET stringa di connessione MySQL Server=<MySQL-DB-name>.mysql.database.azure.com;Database=<MySQL-DB-name>;Port=3306;User Id=<MySQL-DBusername>;SSL Mode=Required;

Codice di esempio

Fare riferimento alla procedura e al codice seguente per connettersi a Database di Azure per MySQL usando un'identità gestita assegnata dal sistema.

Per .NET non è disponibile un plug-in o una libreria per supportare connessioni senza password. È possibile ottenere un token di accesso per l'identità gestita o l'entità servizio usando la libreria client come Azure.Identity. È quindi possibile usare il token di accesso come password per connettersi al database. Quando si usa il codice seguente, rimuovere il commento dalla parte del frammento di codice per il tipo di autenticazione che si vuole usare.

using Azure.Core;
using Azure.Identity;
using MySqlConnector;

// Uncomment the following lines according to the authentication type.
// 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

Per altri esempi di codice, vedere Connessione ai database di Azure da servizio app senza segreti usando un'identità gestita.

Identità gestita assegnata dall'utente

Nome variabile di ambiente predefinito Descrizione Valore di esempio
AZURE_MYSQL_CLIENTID Your client ID <identity-client-ID>
AZURE_MYSQL_CONNECTIONSTRING ADO.NET stringa di connessione MySQL Server=<MySQL-DB-name>.mysql.database.azure.com;Database=<MySQL-DB-name>;Port=3306;User Id=<MySQL-DBusername>;SSL Mode=Required;

Codice di esempio

Fare riferimento alla procedura e al codice seguente per connettersi a Database di Azure per MySQL usando un'identità gestita assegnata dall'utente.

Per .NET non è disponibile un plug-in o una libreria per supportare connessioni senza password. È possibile ottenere un token di accesso per l'identità gestita o l'entità servizio usando la libreria client come Azure.Identity. È quindi possibile usare il token di accesso come password per connettersi al database. Quando si usa il codice seguente, rimuovere il commento dalla parte del frammento di codice per il tipo di autenticazione che si vuole usare.

using Azure.Core;
using Azure.Identity;
using MySqlConnector;

// Uncomment the following lines according to the authentication type.
// 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

Per altri esempi di codice, vedere Connessione ai database di Azure da servizio app senza segreti usando un'identità gestita.

Stringa di connessione

Nome variabile di ambiente predefinito Descrizione Valore di esempio
AZURE_MYSQL_CONNECTIONSTRING ADO.NET stringa di connessione MySQL Server=<MySQL-DB-name>.mysql.database.azure.com;Database=<MySQL-DB-name>;Port=3306;User Id=<MySQL-DBusername>;Password=<MySQL-DB-password>;SSL Mode=Required

Codice di esempio

Fare riferimento alla procedura e al codice seguente per connettersi a Database di Azure per MySQL usando un stringa di connessione.

  1. Installare le dipendenze. Seguire le indicazioni per installare il connettore/NET MySQL
  2. Nel codice ottenere il stringa di connessione MySQL dalle variabili di ambiente aggiunte dal servizio service Connessione or. Per stabilire una connessione crittografata al server MySQL tramite SSL, vedere questi passaggi.
    using System;
    using System.Data;
    using MySql.Data.MySqlClient;
    
    string connectionString = Environment.GetEnvironmentVariable("AZURE_MYSQL_CONNECTIONSTRING");
    using (MySqlConnection connection = new MySqlConnection(connectionString))
    {
        connection.Open();
    }
    

Entità servizio

Nome variabile di ambiente predefinito Descrizione Valore di esempio
AZURE_MYSQL_CLIENTID Your client ID <client-ID>
AZURE_MYSQL_CLIENTSECRET Segreto client <client-secret>
AZURE_MYSQL_TENANTID ID del tenant. <tenant-ID>
AZURE_MYSQL_CONNECTIONSTRING ADO.NET stringa di connessione MySQL Server=<MySQL-DB-name>.mysql.database.azure.com;Database=<MySQL-DB-name>;Port=3306;User Id=<MySQL-DBusername>;SSL Mode=Required

Codice di esempio

Per connettersi a Database di Azure per MySQL usando un'entità servizio, vedere i passaggi e il codice seguenti.

Per .NET non è disponibile un plug-in o una libreria per supportare connessioni senza password. È possibile ottenere un token di accesso per l'identità gestita o l'entità servizio usando la libreria client come Azure.Identity. È quindi possibile usare il token di accesso come password per connettersi al database. Quando si usa il codice seguente, rimuovere il commento dalla parte del frammento di codice per il tipo di autenticazione che si vuole usare.

using Azure.Core;
using Azure.Identity;
using MySqlConnector;

// Uncomment the following lines according to the authentication type.
// 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

Per altri esempi di codice, vedere Connessione ai database di Azure da servizio app senza segreti usando un'identità gestita.

Passaggi successivi

Seguire le documentazioni per altre informazioni su Service Connessione or.