Error 28000: Login failed for user DOMAIN \\ user with pyodbc

I am trying to use Python to connect to an SQL database using Windows authentication. I looked at some of the posts here (like here ), but the suggested methods didn't seem to work.

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 username DOMAIN and username respectively in the error message above.)

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

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

Interestingly, what I did wrong, and how I can solve the problem.

+8
python sql-server pyodbc
source share
3 answers

Trusted_connection=yes tells SQL Server to use "Windows Authentication", and your script will try to log into SQL Server with the Windows credentials of the user script.

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

+6
source share

Try this cxn line:

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

http://mkleehammer.imtqy.com/pyodbc/

0
source share

I tried everything and this is what ultimately 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' ) 
0
source share

All Articles