我正在尝试使用 Azure Data Studio 连接到未本地托管的 SQL Server 实例。我在 pyodbc 中使用以下连接字符串:
python
Copy code
pyodbc.connect(
`Trusted_Connection='No',
Authentication='ActiveDirectoryPassword',
UID=username,
PWD=password,
Driver=driver,
Server=server,
Database=database)`
但是,我在尝试建立连接时遇到错误。我正在使用我的 Microsoft 帐户和 Intra ID 在 Azure Data Studio 中进行身份验证。
有人可以帮我解决这个问题吗?我是否应该检查特定设置或配置以确保连接成功?
谢谢您的帮助!
错误:('28000',“[28000] [Microsoft][SQL Server ********][SQL Server] 用户“域\用户名”登录失败。 (18456) (SQLDriverConnect); [28000] [微软] [SQL Server *****][SQL Server]用户“域\用户名”登录失败。 (18456)")
Error: ('28000', "[28000] [Microsoft][SQL Server ********][SQL Server] Login failed for user 'DOMAIN\username'. (18456) (SQLDriverConnect); [28000] [Microsoft] [SQL Server *****][SQL Server]Login failed for user 'DOMAIN\username'. (18456)")
如果用户无权访问数据库或者提供的凭据不正确,那么您可能会收到上述错误。确保用户可以访问数据库并提供正确的凭据,即用户名和密码。根据本文,MFA 是所有 Azure 租户的默认登录方法。因此,当您尝试使用 ActiveDirectoryPassword 身份验证连接 Azure SQL 数据库时,您可能会收到以下错误:
('FA004', "[FA004] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Failed to authenticate the user '****' in Active Directory (Authentication option is 'ActiveDirectoryPassword').\r\nError code 0xCAA2000C; state 10\r\nAADSTS50076: Due to a configuration change made by your administrator, or because you moved to a new location, you must use multi-factor authentication to access '****'. Trace ID: 9827ec0f-865a-4b15-9c70-207409726100 Correlation ID: 16bf58ec-0c82-45e6-bdbc-9eca4a0a9e11 Timestamp: 2024-08-12 05:46:53Z (0) (SQLDriverConnect); [FA004] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Failed to authenticate the user '****' in Active Directory (Authentication option is 'ActiveDirectoryPassword').\r\nError code 0xCAA2000C; state 10\r\nAADSTS50076: Due to a configuration change made by your administrator, or because you moved to a new location, you must use multi-factor authentication to access '022907d3-0f1b-48f7-badc-1ba6abab6d66'. Trace ID: 9827ec0f-865a-4b15-9c70-207409726100 Correlation ID: 16bf58ec-0c82-45e6-bdbc-9eca4a0a9e11 Timestamp: 2024-08-12 05:46:53Z (0)")
因此,使用MFA身份验证连接Azure SQL db python ActiveDirectoryInteractive 使用以下代码的身份验证方法:
import pyodbc
server = 'tcp:<serverName>.database.windows.net'
database = ',dbName>'
username = '<EntraId>'
auth = 'ActiveDirectoryInteractive'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';Authentication='+auth+';UID='+username+';ENCRYPT=yes;')
cursor = cnxn.cursor()
cursor.execute("SELECT * FROM <tableName>")
rows = cursor.fetchall()
for row in rows:
print(row)
它将连接到 SQL 数据库并从 Data Studio 成功读取表,如下所示: