You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
authentication = os.getenv('AZURE_SQL_AUTHENTICATION')# The value should be 'ActiveDirectoryMsi'
89
89
90
90
# Uncomment the following lines according to the authentication type.
91
91
# For system-assigned managed identity.
92
92
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};Authentication={authentication};Encrypt=yes;'
93
93
94
94
# For user-assigned managed identity.
95
-
#user = os.getenv('AZURE_SQL_USER')
96
-
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={user};Authentication={authentication};Encrypt=yes;'
95
+
#client_id = os.getenv('AZURE_SQL_USER')
96
+
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={client_id};Authentication={authentication};Encrypt=yes;'
97
97
98
98
conn = pyodbc.connect(connString)
99
99
```
100
-
100
+
For an alternative method, you can also connect to Azure SQL Database using an access token, refer to [Migrate a Python application to use passwordless connections with Azure SQL Database](/azure/azure-sql/database/azure-sql-passwordless-migration-python).
101
101
102
102
### [NodeJS](#tab/nodejs)
103
103
@@ -135,13 +135,11 @@ For more information, see [Connect using Microsoft Entra authentication](/sql/co
Copy file name to clipboardExpand all lines: articles/app-service/tutorial-connect-msi-azure-database.md
+16-12Lines changed: 16 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -188,27 +188,31 @@ If you encounter any problem when creating a connection, refer to [Troubleshooti
188
188
189
189
## 3. Modify your code
190
190
191
-
In this section, connectivity to the Azure database in your code follows the `DefaultAzureCredential` pattern for all language stacks. `DefaultAzureCredential` is flexible enough to adapt to both the development environment and the Azure environment. When running locally, it can retrieve the logged-in Azure user from the environment of your choice (Visual Studio, Visual Studio Code, Azure CLI, or Azure PowerShell). When running in Azure, it retrieves the managed identity. So it's possible to have connectivity to database both at development time and in production. The pattern is as follows:
192
-
193
-
1. Instantiate a `DefaultAzureCredential` from the Azure Identity client library. If you're using a user-assigned identity, specify the client ID of the identity.
194
-
2. Get an access token for the resource URI respective to the database type.
195
-
* For Azure SQL Database: `https://database.windows.net/.default`
196
-
* For Azure Database for MySQL: `https://ossrdbms-aad.database.windows.net/.default`
197
-
* For Azure Database for PostgreSQL: `https://ossrdbms-aad.database.windows.net/.default`
198
-
3. Add the token to your connection string.
199
-
4. Open the connection.
200
-
201
191
# [Azure SQL Database](#tab/sqldatabase-sc)
202
192
203
-
[!INCLUDE [code sample for postgres managed identity authentication connection](./includes/tutorial-connect-msi-azure-database/code-sql-mi.md)]
193
+
[!INCLUDE [code sample for sql managed identity authentication connection](./includes/tutorial-connect-msi-azure-database/code-sql-mi.md)]
204
194
205
195
# [Azure Database for MySQL](#tab/mysql-sc)
206
196
197
+
Connectivity to the Azure Database for MySQL in your code follows the `DefaultAzureCredential` pattern for all language stacks. `DefaultAzureCredential` is flexible enough to adapt to both the development environment and the Azure environment. When running locally, it can retrieve the logged-in Azure user from the environment of your choice (Visual Studio, Visual Studio Code, Azure CLI, or Azure PowerShell). When running in Azure, it retrieves the managed identity. So it's possible to have connectivity to database both at development time and in production. The pattern is as follows:
198
+
199
+
1. Instantiate a `DefaultAzureCredential` from the Azure Identity client library. If you're using a user-assigned identity, specify the client ID of the identity.
200
+
2. Get an access token for Azure Database for MySQL: `https://ossrdbms-aad.database.windows.net/.default`.
201
+
3. Add the token to your connection string.
202
+
4. Open the connection.
203
+
207
204
[!INCLUDE [code sample for mysql managed identity authentication connection](./includes/tutorial-connect-msi-azure-database/code-mysql-mi.md)]
208
205
209
206
# [Azure Database for PostgreSQL](#tab/postgresql-sc)
210
207
211
-
[!INCLUDE [code sample for sql managed identity authentication connection](./includes/tutorial-connect-msi-azure-database/code-postgres-mi.md)]
208
+
Connectivity to the Azure Database for PostgreSQL in your code follows the `DefaultAzureCredential` pattern for all language stacks. `DefaultAzureCredential` is flexible enough to adapt to both the development environment and the Azure environment. When running locally, it can retrieve the logged-in Azure user from the environment of your choice (Visual Studio, Visual Studio Code, Azure CLI, or Azure PowerShell). When running in Azure, it retrieves the managed identity. So it's possible to have connectivity to database both at development time and in production. The pattern is as follows:
209
+
210
+
1. Instantiate a `DefaultAzureCredential` from the Azure Identity client library. If you're using a user-assigned identity, specify the client ID of the identity.
211
+
2. Get an access token for Azure Database for PostgreSQL: `https://ossrdbms-aad.database.windows.net/.default`.
212
+
3. Add the token to your connection string.
213
+
4. Open the connection.
214
+
215
+
[!INCLUDE [code sample for postgres managed identity authentication connection](./includes/tutorial-connect-msi-azure-database/code-postgres-mi.md)]
Copy file name to clipboardExpand all lines: articles/service-connector/includes/code-sql-me-id.md
+16-19Lines changed: 16 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -85,9 +85,10 @@ Update your application following the tutorial [Migrate a Java application to us
85
85
python -m pip install pyodbc
86
86
```
87
87
88
-
1. Get the Azure SQL Database connection configurations from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
88
+
1. Get the Azure SQL Database connection configurations from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet forthe authentication type you want to use. If you are using Azure Container Apps as compute service or the connection stringin the code snippet doesn't work, refer to [Migrate a Python application to use passwordless connections with Azure SQL Database](/azure/azure-sql/database/azure-sql-passwordless-migration-python) to connect to Azure SQL Database using an access token.
89
+
89
90
```python
90
-
import os;
91
+
import os
91
92
import pyodbc
92
93
93
94
server = os.getenv('AZURE_SQL_SERVER')
@@ -97,16 +98,16 @@ Update your application following the tutorial [Migrate a Java application to us
97
98
98
99
# Uncomment the following lines according to the authentication type.
99
100
# For system-assigned managed identity.
100
-
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};Authentication={authentication};Encrypt=yes;'
101
+
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
101
102
102
103
# For user-assigned managed identity.
103
-
#user = os.getenv('AZURE_SQL_USER')
104
-
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={user};Authentication={authentication};Encrypt=yes;'
104
+
# clientID = os.getenv('AZURE_SQL_USER')
105
+
# 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'
105
106
106
107
# For service principal.
107
108
# user = os.getenv('AZURE_SQL_USER')
108
109
# password = os.getenv('AZURE_SQL_PASSWORD')
109
-
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={user};PWD={password};Authentication={authentication};Encrypt=yes;'
110
+
# 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'
110
111
111
112
conn = pyodbc.connect(connString)
112
113
```
@@ -133,7 +134,7 @@ Update your application following the tutorial [Migrate a Java application to us
133
134
// port,
134
135
// database,
135
136
// authentication: {
136
-
// authenticationType
137
+
// type: authenticationType
137
138
// },
138
139
// options: {
139
140
// encrypt: true
@@ -147,13 +148,11 @@ Update your application following the tutorial [Migrate a Java application to us
147
148
// port,
148
149
// database,
149
150
// authentication: {
150
-
// type: authenticationType,
151
-
// options: {
152
-
// clientId: clientId
153
-
// }
151
+
// type: authenticationType
154
152
// },
155
153
// options: {
156
-
// encrypt: true
154
+
// encrypt: true,
155
+
// clientId: clientId
157
156
// }
158
157
// };
159
158
@@ -166,15 +165,13 @@ Update your application following the tutorial [Migrate a Java application to us
0 commit comments