提问人:xbai15 提问时间:11/12/2023 更新时间:11/14/2023 访问量:91
无法使用 ODBC 驱动程序 17 和 Authentication=ActiveDirectoryMsi 连接到 Azure SQL 数据库
Failed to connect to Azure SQL Database using ODBC Driver 17 with Authentication=ActiveDirectoryMsi
问:
我尝试使用 Azure Active DIrectory 用户托管标识将 Azure 容器实例连接到我的 Azure SQL 数据库。但是我无法使用 ODBC Driver 17 建立连接。
错误信息:
[S1T00][unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
[FA004][unixODBC][Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Failed to authenticate the user '' in Active Directory (Authentication option is 'ActiveDirectoryMSI').
Error code 0xA190; state 41360
Required metadata header not specified or not correct
Required metadata header not specified or not correct
Required metadata header not specified or not correct
Required metadata header not specified or not correct
[CE275][unixODBC][Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Error requesting access token, HTTP status 400, expected 200
[08001][unixODBC][Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Timeout error [258].
[08001][unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Unable to complete login process due to delay in login response
配置详细信息:
docker 镜像中使用的 ODBC 驱动程序的版本为 17.10.5.1-1,
标识已分配给容器实例,并在表 [sys.database_principals] 中设置为数据库的外部用户
标识设置为数据库的参与者。
已检查并确认从 Azure 容器实例到 SQL Server 的网络连接。
根据将 Azure Active Directory 与 ODBC 驱动程序配合使用的 DNS 配置
[dns-name]
Driver=ODBC Driver 17 for SQL Server
Server=<server-name>
Database=<database-name>
UID=<object id of the assigned identity>
Authentication=ActiveDirectoryMsi
Encrypt=yes
任何人都可以帮助确定这些错误的原因或建议故障排除步骤吗?任何见解或建议将不胜感激。
答:
0赞
Pratik Lad
11/14/2023
#1
根据错误:您无法使用托管标识登录 SQL Server
在 Azure 容器实例上启用托管标识,并为其分配适当的角色以访问 SQL Server。
然后,将此托管标识添加到 SQL Server 的 Microsoft Entra ID,并为其创建相应的用户以用于登录
CREATE USER [<identity-name>] FROM EXTERNAL PROVIDER;
- 然后使用以下代码进行身份验证。
from azure.identity import DefaultAzureCredential
import pyodbc, struct
credential = DefaultAzureCredential() # system-assigned identity
credential = DefaultAzureCredential(managed_identity_client_id='<client-id-of-user-assigned-identity>') # user-assigned identity
# Get token for Azure SQL Database and convert to UTF-16-LE for SQL Server driver
token = credential.get_token("https://database.windows.net/.default").token.encode("UTF-16-LE")
token_struct = struct.pack(f'<I{len(token)}s', len(token), token)
# Connect with the token
SQL_COPT_SS_ACCESS_TOKEN = 1256
connString = f"Driver={{ODBC Driver 17 for SQL Server}};SERVER=<server-name>.database.windows.net;DATABASE=<database-name>"
conn = pyodbc.connect(connString, attrs_before={SQL_COPT_SS_ACCESS_TOKEN: token_struct})
评论
0赞
xbai15
11/15/2023
嗨,普拉蒂克,谢谢你回答我的问题!我在容器中运行的应用程序依赖于 DSN 来建立连接。有什么方法可以使用 ODBC.ini 中设置的 DSN 进行身份验证?
0赞
Pratik Lad
11/15/2023
尝试将服务器名称作为 DSN 中的名称,然后进行身份验证?Server = tcp:severname,1433
评论