错误28000:使用pyodbc登录用户域\\用户失败

时间:2022-08-23 10:54:15

I am trying to use Python to connect to a SQL database by using Window authentication. I looked at some of the posts here (e.g., here), but the suggested methods didn't seem to work.

我正在尝试使用Python通过窗口身份验证连接到SQL数据库。我看了这里的一些文章(例如,这里),但是建议的方法似乎并不奏效。

For example, I used the following code:

例如,我使用了以下代码:

cnxn = pyodbc.connect(driver='{SQL Server Native Client 11.0}',
                      server='SERVERNAME', 
                      database='DATABASENAME',               
                      trusted_connection='yes')

But I got the following error:

但我犯了如下错误:

Error: ('28000', "[28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]
Login failed for user 'DOMAIN\\username'. (18456) (SQLDriverConnect); [28000] [Microsoft]
[SQL Server Native Client 11.0][SQL Server]Login failed for user 'DOMAIN\\username'. 
(18456)") 

(Note that I replaced the actual domain name and user name with DOMAIN and username respectively, in the error message above.)

(注意,在上面的错误消息中,我将实际的域名和用户名分别替换为域和用户名。)

I also tried using my UID and PWD, which led to the same error.

我还尝试使用UID和PWD,这导致了同样的错误。

Lastly, I tried to change the service account by following the suggestion from the link above, but on my computer, there was no Log On tab when I went to the Properties of services.msc.

最后,我尝试按照上面链接的建议更改服务帐户,但是在我的计算机上,当我访问services.msc的属性时,没有登录选项卡。

I wonder what I did wrong and how I can fix the problem.

我想知道我做错了什么,我怎样才能解决这个问题。

4 个解决方案

#1


8  

Trusted_connection=yes tells the SQL Server to use "Windows Authentication" and your script will attempt to log in to the SQL Server using the Windows credentials of the user running the script.

Trusted_connection=yes告诉SQL服务器使用“Windows身份验证”,您的脚本将尝试使用运行脚本的用户的Windows凭据登录到SQL服务器。

If you want to use "SQL Server Authentication" with a specific SQL Server login specified by UID and PWD then use Trusted_connection=no (or just omit the Trusted_connection parameter from your connection string).

如果您想对UID和PWD指定的特定SQL服务器登录使用“SQL Server身份验证”,那么使用Trusted_connection=no(或者从连接字符串中省略Trusted_connection参数)。

#2


0  

Try this cxn string:

试试这个cxn字符串:

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;PORT=1433;DATABASE=testdb;UID=me;PWD=pass')

http://mkleehammer.github.io/pyodbc/

http://mkleehammer.github.io/pyodbc/

#3


0  

I tried everything and this is what eventually worked for me:

我尝试了所有的方法,这最终对我起了作用:

import pyodbc
driver= '{SQL Server Native Client 11.0}'

cnxn = pyodbc.connect(
    Trusted_Connection='Yes',
    Driver='{ODBC Driver 11 for SQL Server}',
    Server='MyServer,1433',
    Database='MyDB'
)

#4


0  

I had similar issue while connecting to the default database (MSSQLSERVER). If you are connecting to the default database, please remove the

我在连接默认数据库(MSSQLSERVER)时遇到了类似的问题。如果您正在连接到默认数据库,请删除

database='DATABASENAME',

数据库=“数据库名”,

line from the connection parameters section and retry.

从连接参数部分行并重试。

Cheers, Deepak

欢呼,迪帕克

#1


8  

Trusted_connection=yes tells the SQL Server to use "Windows Authentication" and your script will attempt to log in to the SQL Server using the Windows credentials of the user running the script.

Trusted_connection=yes告诉SQL服务器使用“Windows身份验证”,您的脚本将尝试使用运行脚本的用户的Windows凭据登录到SQL服务器。

If you want to use "SQL Server Authentication" with a specific SQL Server login specified by UID and PWD then use Trusted_connection=no (or just omit the Trusted_connection parameter from your connection string).

如果您想对UID和PWD指定的特定SQL服务器登录使用“SQL Server身份验证”,那么使用Trusted_connection=no(或者从连接字符串中省略Trusted_connection参数)。

#2


0  

Try this cxn string:

试试这个cxn字符串:

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;PORT=1433;DATABASE=testdb;UID=me;PWD=pass')

http://mkleehammer.github.io/pyodbc/

http://mkleehammer.github.io/pyodbc/

#3


0  

I tried everything and this is what eventually worked for me:

我尝试了所有的方法,这最终对我起了作用:

import pyodbc
driver= '{SQL Server Native Client 11.0}'

cnxn = pyodbc.connect(
    Trusted_Connection='Yes',
    Driver='{ODBC Driver 11 for SQL Server}',
    Server='MyServer,1433',
    Database='MyDB'
)

#4


0  

I had similar issue while connecting to the default database (MSSQLSERVER). If you are connecting to the default database, please remove the

我在连接默认数据库(MSSQLSERVER)时遇到了类似的问题。如果您正在连接到默认数据库,请删除

database='DATABASENAME',

数据库=“数据库名”,

line from the connection parameters section and retry.

从连接参数部分行并重试。

Cheers, Deepak

欢呼,迪帕克