Unable to connect using pymssql with windows authe

2019-07-07 07:28发布

问题:

While trying to connect to MSSQL Server 2012 using pymssql, I get the following error.

My server name in Windows Authentication is SARATH,User Name is Sarath\SarathShanker and I did not set a password.

Code:

mssql_conn=pymssql.connect(host='SARATH',user='Sarath\SarathShanker',password='',database='matrix')

Error:

Traceback (most recent call last):
File "", line 1, in
File "pymssql.pyx", line 556, in pymssql.connect (pymssql.c:7990)

pymssql.OperationalError: (18452, 'Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.DB-Lib error message 18452 , severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB -Lib error message 20002, severity 9:\nAdaptive Server connection failed\nDB-Lib error message 18452, severity 14:\nGeneral SQL Server error: Check messages fro m the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server conne ction failed\n')

How should I modify my script in order to connect to MSSQL Server using pymssql.

P.S I have already imported pymssql as well. (Not Shown in Code above)

回答1:

Try this:

conn = pymssql.connect(host='myhost', database='mydb')

This is with Python Version 3.4 and for Windows Authentication.



回答2:

If on RHEL, try

os.environ["FREETDSCONF"] = "/etc/freetds.conf"


回答3:

If you are using the latest version of pymssql (I use 2.1.3),

pymssql.connect(server='<TEST_SERVER>', database='<TEST_DB>')

Simply replace the '' and '' with your server and DB name.