问题描述
我正在尝试使用 MSI身份验证(没有用户名和密码)连接 Azure机器学习服务中的 Azure SQL数据库。
I am trying to connect the Azure SQL Database from Azure Machine Learning Service with MSI Authentication (Without a username and password).
我试图在azure机器学习服务上建立机器学习模型,目的是需要数据,这就是为什么我想使用MSI身份验证从Azure机器学习服务连接Azure SQL数据库的原因。
I am trying to Machine learning model on azure Machine learning service that purpose I need data that' why I want to connect Azure SQL Database from Azure Machine Learning Service using MSI Authentication.
但是我遇到了以下错误:-
But I got below error:-
"error": {"message": "Activity Failed:\n{\n \"error\": {\n \"code\": \"UserError\",\n \"message\": \"User program failed with KeyError: 'MSI_ENDPOINT'\",\n
请检查以下我用于数据库连接的代码。
Please check the below code that I have used for the database connection.
import logging
import struct
import pyodbc
import os
import requests
class AzureDbConnect:
def __init__(self):
print("Inside msi database")
msi_endpoint = os.environ["MSI_ENDPOINT"]
msi_secret = os.environ["MSI_SECRET"]
resource_uri = 'https://database.windows.net/'
logging.info(msi_endpoint)
print(msi_endpoint)
logging.info(msi_secret)
print(msi_secret)
print("Inside token")
token_auth_uri = f"{msi_endpoint}?resource={resource_uri}&api-version=2017-09-01"
head_msi = {'Secret': msi_secret}
resp = requests.get(token_auth_uri, headers=head_msi)
access_token = resp.json()['access_token']
logging.info(access_token)
print("Token is :- ")
print(access_token)
accesstoken = bytes(access_token, 'utf-8')
exptoken = b""
for i in accesstoken:
exptoken += bytes({i})
exptoken += bytes(1)
tokenstruct = struct.pack("=i", len(exptoken)) + exptoken
conn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};"
"Server=tcp:<Server Name>"
"1433;Database=<Database Name>",
attrs_before={1256: bytearray(tokenstruct)})
print(conn)
self.sql_db = conn.cursor()
是否有任何方法可以连接Azure,SQL数据库是通过具有MSI身份验证的Azure机器学习服务提供的?
推荐答案
当前不支持MSI身份验证来连接Azure SQL DB Azure ML,将在将来添加。通常,这可以与在数据库中设置服务主体有关,附件是逐步指导,用于为Azure ML进行此设置。
Currently MSI Authentication is not supported to connect Azure SQL DB from Azure ML, It's on road map to add in future. You can Usually this is to do with setting up service principals in the DB, Attached is a step-by-step guide for getting this setup for Azure ML.
这篇关于尝试使用MSI身份验证从Azure ML服务连接Azure SQL数据库(不使用用户名和密码连接Azure数据库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!