|
| 1 | +--- |
| 2 | +author: xiaofanzhou |
| 3 | +ms.service: service-connector |
| 4 | +ms.topic: include |
| 5 | +ms.date: 11/13/2023 |
| 6 | +ms.author: xiaofanzhou |
| 7 | +--- |
| 8 | + |
| 9 | +# [.NET](#tab/dotnet-mysql-mi) |
| 10 | +For .NET, get an access token for the managed identity using a client library such as [Azure.Identity](https://www.nuget.org/packages/Azure.Identity/). Then use the access token as a password to connect to the database. When using the code below, make sure you uncomment the part of the code snippet that corresponds to the authentication type you want to use. |
| 11 | + |
| 12 | +```csharp |
| 13 | +using Azure.Core; |
| 14 | +using Azure.Identity; |
| 15 | +using MySqlConnector; |
| 16 | + |
| 17 | +// Uncomment the following lines according to the authentication type. |
| 18 | +// For system-assigned managed identity. |
| 19 | +// var credential = new DefaultAzureCredential(); |
| 20 | +
|
| 21 | +// For user-assigned managed identity. |
| 22 | +// var credential = new DefaultAzureCredential( |
| 23 | +// new DefaultAzureCredentialOptions |
| 24 | +// { |
| 25 | +// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_MYSQL_CLIENTID"); |
| 26 | +// }); |
| 27 | +
|
| 28 | +var tokenRequestContext = new TokenRequestContext( |
| 29 | + new[] { "https://ossrdbms-aad.database.windows.net/.default" }); |
| 30 | +AccessToken accessToken = await credential.GetTokenAsync(tokenRequestContext); |
| 31 | +// Open a connection to the MySQL server using the access token. |
| 32 | +string connectionString = |
| 33 | + $"{Environment.GetEnvironmentVariable("AZURE_MYSQL_CONNECTIONSTRING")};Password={accessToken.Token}"; |
| 34 | + |
| 35 | +using var connection = new MySqlConnection(connectionString); |
| 36 | +Console.WriteLine("Opening connection using access token..."); |
| 37 | +await connection.OpenAsync(); |
| 38 | + |
| 39 | +// do something |
| 40 | +``` |
| 41 | + |
| 42 | +# [Java](#tab/java-mysql-mi) |
| 43 | + |
| 44 | +1. Add the following dependencies in your *pom.xml* file: |
| 45 | + |
| 46 | + ```xml |
| 47 | + <dependency> |
| 48 | + <groupId>mysql</groupId> |
| 49 | + <artifactId>mysql-connector-java</artifactId> |
| 50 | + <version>8.0.30</version> |
| 51 | + </dependency> |
| 52 | + <dependency> |
| 53 | + <groupId>com.azure</groupId> |
| 54 | + <artifactId>azure-identity-extensions</artifactId> |
| 55 | + <version>1.1.5</version> |
| 56 | + </dependency> |
| 57 | + ``` |
| 58 | + |
| 59 | +1. Get the connection string from the environment variable, and add the plugin name to connect to the database: |
| 60 | + |
| 61 | + ```java |
| 62 | + String url = System.getenv("AZURE_MYSQL_CONNECTIONSTRING"); |
| 63 | + String pluginName = "com.azure.identity.extensions.jdbc.mysql.AzureMysqlAuthenticationPlugin"; |
| 64 | + Connection connection = DriverManager.getConnection(url + "&defaultAuthenticationPlugin=" + |
| 65 | + pluginName + "&authenticationPlugins=" + pluginName); |
| 66 | + ``` |
| 67 | + |
| 68 | +For more information, see [Use Java and JDBC with Azure Database for MySQL - Flexible Server](../../../mysql/flexible-server/connect-java.md?tabs=passwordless). |
| 69 | + |
| 70 | +# [Python](#tab/python-mysql-mi) |
| 71 | + |
| 72 | +1. Install dependencies. |
| 73 | + |
| 74 | + ```bash |
| 75 | + pip install azure-identity |
| 76 | + # install Connector/Python https://dev.mysql.com/doc/connector-python/en/connector-python-installation.html |
| 77 | + pip install mysql-connector-python |
| 78 | + ``` |
| 79 | + |
| 80 | +1. Authenticate with an access token from the `azure-identity` library. Get the connection information from the environment variable added by Service Connector. When using the code below, make sure you uncomment the part of the code snippet that corresponds to the authentication type you want to use. |
| 81 | + |
| 82 | + ```python |
| 83 | + from azure.identity import ManagedIdentityCredential, ClientSecretCredential |
| 84 | + import mysql.connector |
| 85 | + import os |
| 86 | + |
| 87 | + # Uncomment the following lines according to the authentication type. |
| 88 | + # For system-assigned managed identity. |
| 89 | + # cred = ManagedIdentityCredential() |
| 90 | + |
| 91 | + # For user-assigned managed identity. |
| 92 | + # managed_identity_client_id = os.getenv('AZURE_MYSQL_CLIENTID') |
| 93 | + # cred = ManagedIdentityCredential(client_id=managed_identity_client_id) |
| 94 | + |
| 95 | + # acquire token |
| 96 | + accessToken = cred.get_token('https://ossrdbms-aad.database.windows.net/.default') |
| 97 | + |
| 98 | + # open connect to Azure MySQL with the access token. |
| 99 | + host = os.getenv('AZURE_MYSQL_HOST') |
| 100 | + database = os.getenv('AZURE_MYSQL_NAME') |
| 101 | + user = os.getenv('AZURE_MYSQL_USER') |
| 102 | + password = accessToken.token |
| 103 | + |
| 104 | + cnx = mysql.connector.connect(user=user, |
| 105 | + password=password, |
| 106 | + host=host, |
| 107 | + database=database) |
| 108 | + cnx.close() |
| 109 | + |
| 110 | + ``` |
| 111 | + |
| 112 | +# [NodeJS](#tab/nodejs-mysql-mi) |
| 113 | + |
| 114 | +1. Install dependencies. |
| 115 | + |
| 116 | + ```bash |
| 117 | + npm install --save @azure/identity |
| 118 | + npm install --save mysql2 |
| 119 | + ``` |
| 120 | + |
| 121 | +1. Get an access token using `@azure/identity` and the Azure MySQL database information from the environment variables added by Service Connector. When using the code below, make sure you uncomment the part of the code snippet that corresponds to the authentication type you want to use. |
| 122 | + |
| 123 | + ```javascript |
| 124 | + import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity"; |
| 125 | + |
| 126 | + const mysql = require('mysql2'); |
| 127 | + |
| 128 | + // Uncomment the following lines according to the authentication type. |
| 129 | + // for system-assigned managed identity |
| 130 | + // const credential = new DefaultAzureCredential(); |
| 131 | + |
| 132 | + // for user-assigned managed identity |
| 133 | + // const clientId = process.env.AZURE_MYSQL_CLIENTID; |
| 134 | + // const credential = new DefaultAzureCredential({ |
| 135 | + // managedIdentityClientId: clientId |
| 136 | + // }); |
| 137 | + |
| 138 | + // acquire token |
| 139 | + var accessToken = await credential.getToken('https://ossrdbms-aad.database.windows.net/.default'); |
| 140 | + |
| 141 | + const connection = mysql.createConnection({ |
| 142 | + host: process.env.AZURE_MYSQL_HOST, |
| 143 | + user: process.env.AZURE_MYSQL_USER, |
| 144 | + password: accessToken.token, |
| 145 | + database: process.env.AZURE_MYSQL_DATABASE, |
| 146 | + port: process.env.AZURE_MYSQL_PORT, |
| 147 | + ssl: process.env.AZURE_MYSQL_SSL |
| 148 | + }); |
| 149 | + |
| 150 | + connection.connect((err) => { |
| 151 | + if (err) { |
| 152 | + console.error('Error connecting to MySQL database: ' + err.stack); |
| 153 | + return; |
| 154 | + } |
| 155 | + console.log('Connected to MySQL database'); |
| 156 | + }); |
| 157 | + ``` |
| 158 | + |
| 159 | +----- |
| 160 | + |
| 161 | +For more code samples, see [Create a passwordless connection to a database service via Service Connector](/azure/service-connector/tutorial-passwordless?tabs=user%2Cappservice&pivots=mysql#connect-to-a-database-with-microsoft-entra-authentication). |
0 commit comments