PymSQL windows身份验证

2024-05-18 18:57:52 发布

您现在位置:Python中文网/ 问答频道 /正文

用于支持windows身份验证的pymssql模块。现在看来不是了。尽管在某些地方它仍然显示出它应该起作用。我一直找不到这个问题的确切答案,也找不到解决办法。最相关的链接:

https://groups.google.com/forum/#!topic/pymssql/QDMLTGBNeU0

pymssql 1.0 supported it because it made use of and depended on the MS-provided DLL which was part of the SQL Server client stack. This stack was in charge of handling all of the NTLM negotiation and such. This meant, among other things that it was a Windows-only solution.

我可以模拟多种网络环境,所以我尝试了许多不同的设置。我正在尝试使用此脚本使用windows身份验证连接到远程MSSQL服务器。这就是问题所在。


根据我的研究,包括上面的链接,有两种方法可以在pymssql模块中使用windows身份验证,即假定工作。

第一种方法:使用当前用户凭据:

pymssql.connect(server='server') 
# credentials come from active windows session
# some research shows that a "trusted=True" keyword should be provided.

第二种方法:使用给定的用户凭据:

pymssql.connect(server='server', user=r'domain\user', password='pass') 
# credentials are given in code and somehow converted to a 
# windows authentication in the background
# some research shows that a "trusted=True" keyword should be provided.

使用_mssql模块也是如此。


注意:

  • Python版本:2.7.8
  • 我正在使用的PymSql版本:2.1.1
  • 用于支持windows身份验证的PymSQL版本:1.x
  • 我用(所有64位)进行了测试:
    • windows 7专业版
    • windows 7家庭高级版
    • windows服务器2012
    • windows服务器2012R2

本专题的其他问题:

pymssql: How to use windows authentication when running on a non-windows box

Unable to connect using pymssql with windows authentication

https://stackoverflow.com/questions/27692366/mssql-python-windows-authentication


Tags: 模块andofthein服务器身份验证authentication
3条回答

我能够使用python 2.7.11 64位、pymssql 2.1.1 win 64、windows 10、sqlserver 2012和windows身份验证解决此问题:

conn = pymssql.connect(server = 'EDDESKTOP', database = 'baseballData')

以及在Sql Server配置管理器中启用tcp/ip连接;Sql Server网络配置->;MSSQLSERVER协议->;启用tcp/ip

我最近也遇到过同样的挑战。一开始我还使用了Python2.7和windows身份验证。我唯一能够连接的方法是使用IronPython并导入clr模块。我不知道为什么它会起作用,我希望能得到一个对此问题有知识的人的解释。一些不同之处在于,我的服务器是本地的,其中的数据库是用“实体框架代码优先”形成的。这是最终将我连接到服务器的代码。

import clr
clr.AddReference('System.Data')
from System.Data.SqlClient import *

Conn_string = 'data source=Server_Name; initial catalog=Database_Name; trusted_connection=True'
ScheduleConn = SqlConnection(Conn_string)
ScheduleConn.Open()

如果这不能解决你的问题,我希望它能让你更接近你的解决方案。

看来现在有用了。 Python3.6, 窗口10。

conn = pymssql.connect(server='(local)', database='DbName')

相关问题 更多 >

    热门问题