简体   繁体   English

Python ODBC 使用服务帐户连接到 SQL Server (trusted_connection=no)

[英]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.

相关问题 连接 Python 到 SQL 没有 ODBC 的服务器 - Connect Python to SQL Server without ODBC 无法使用python连接到SQL服务器ODBC - Unable to connect to SQL Server ODBC using python pyodbc 的 Cnn 字符串,带有“trusted_connection=yes”而不是用户 ID 和密码 - Cnn string for pyodbc with “trusted_connection=yes” instead of userid & password 如何使用FreeTDS ODBC连接到SQL Server - How to connect to SQL Server using FreeTDS ODBC 无法使用FreeTDS ODBC连接到SQL Server实例 - Not able to connect to SQL Server instance with FreeTDS ODBC 如何使用服务帐户(不是我自己的 AD 帐户)使用 pyodbc 连接到本地 SQL 服务器 - How to I connect to an On-Prem SQL server using pyodbc using a Service Account (not my own AD account) 如何使用 py(py)odbc 从 python 连接到远程 MS SQL Server - How to connect to remote MS SQL Server from python using py(py)odbc 如何为 SQL Server/Linux 堆栈的 python/pyodbc/unixODBC/MS ODBC Driver 11 设置数据库连接超时? - How to set a DB connection timeout for a python/pyodbc/unixODBC/MS ODBC Driver 11 for SQL Server/Linux stack? Python(Windows)中的SQL Connect错误:严重性9:\\ nAdaptive Server连接失败 - SQL Connect error in Python(Windows): severity 9:\nAdaptive Server connection failed 如何在SQL Server上使用python odbc删除表? - How to DROP TABLE with python odbc on SQL Server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM