[英]Python ODBC Connect to SQL Server with Service Account (trusted_connection=no)
I connect to my server no problem with SQL Alchemy or Pyodbc so long as I use a trusted connection:只要我使用受信任的连接,我就可以使用 SQL Alchemy 或 Pyodbc 连接到我的服务器:
pyodbc.connect("Driver={SQL Server};Server=myServer;Port=1433;Database=myDB;trusted_connection=yes")
sqlalchemy.create_engine('mssql://myServer/myDB?trusted_connection=yes&driver=SQL+Server')
But I need to connect with a service account with basic Windows Authentication.但我需要使用具有基本 Windows 身份验证的服务帐户进行连接。 When I try to add the UID/PWD like so:
当我尝试像这样添加 UID/PWD 时:
sqlalchemy.create_engine("mssql+pyodbc://myUserName:myPassWord@myServer?driver=SQL+Server?trusted_connection=no")
pyodbc.connect('DRIVER={SQL Server};SERVER=myServre;DATABASE=myDB;UID=myUserName;PWD=myPassword')
I get the error "Login failed for user 'myUserName'. (18456) (SQLDriverConnect); [28000] "我收到错误消息“用户‘myUserName’登录失败。(18456) (SQLDriverConnect); [28000]”
Bonus try:奖励尝试:
sqlalchemy.create_engine("mssql+pyodbc://myUserName:myPassword@myDB?driver=SQL+Server?trusted_connection=no")
Returns '[Microsoft][ODBC Driver Manager] Data source name too long (0) (SQLDriverConnect)'返回“[Microsoft][ODBC 驱动程序管理器] 数据源名称太长 (0) (SQLDriverConnect)”
Using SQL Server 2018.使用 SQL Server 2018。
I've verified that the service account has all the right permissions on the DB I'm trying to connect to.我已经验证服务帐户对我尝试连接的数据库具有所有正确的权限。 FWIW, I've also tried connecting using my own Windows credentials instead of the service account's (but with trusted_connection=no) and I get the same error messages.
FWIW,我还尝试使用我自己的 Windows 凭据而不是服务帐户的凭据(但使用 trust_connection=no)进行连接,但我收到了相同的错误消息。
get the error "Login failed for user 'myUserName'. (18456) (SQLDriverConnect); [28000] "
收到错误“用户‘myUserName’登录失败。(18456) (SQLDriverConnect); [28000]”
None of the Microsoft ODBC drivers support using Windows Integrated Authentication (NTLM or Kerberos) using provided credentials.没有任何 Microsoft ODBC 驱动程序支持使用提供的凭据使用 Windows 集成身份验证(NTLM 或 Kerberos)。 This connection string
这个连接字符串
DRIVER={SQL Server};SERVER=myServre;DATABASE=myDB;UID=myUserName;PWD=myPassword
is for SQL Auth, where you have a login and a database user created in SQL Server.用于 SQL 身份验证,其中您有一个登录名和一个在 SQL Server 中创建的数据库用户。
eg:例如:
use mydb
create login myUserName with password='myPassword'
create user myUserName for login myUserName
grant select to myUserName
To use Windows Auth with this driver you have to run your program as the target user, do Windows-level impersonation, store a credential in the Windows Credential store, or use runas /netonly
.要将 Windows 身份验证与此驱动程序一起使用,您必须以目标用户身份运行您的程序,执行 Windows 级别的模拟,将凭据存储在 Windows 凭据存储中,或使用
runas /netonly
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.