简体   繁体   English

远程连接到 MS SQL - 使用 pyodbc 时出错与使用 SQL Server Management Studio 成功

[英]Remote connection to MS SQL - Error using pyodbc vs success using SQL Server Management Studio

I have a MS SQL database in the same network but in other computer.我在同一网络中但在其他计算机上有一个 MS SQL 数据库。 Using the SQL Server Management Studio (SSMS) Express, I can find the database and connect without problems.使用 SQL Server Management Studio (SSMS) Express,我可以毫无问题地找到数据库并进行连接。

But when I use pyodbc to connect to the same server using:但是当我使用 pyodbc 连接到同一台服务器时:

import pyodbc

server = r"xxxER\xxxSQLSERV"
db = "xxxDB"
user = "xxx"
password = "xxxx"
conn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server + ';DATABASE=' + db +';UID=' + user + ';PWD=' + password)

I get following error:我收到以下错误:

pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC SQL Server Driver]Login timeout expired (0) (SQLDriverConnect)')

OBS: I guess that the server string should be right, since if I change it I get always the following error: OBS:我想服务器字符串应该是正确的,因为如果我更改它,我总是会收到以下错误:

pyodbc.Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (53)')

Here the image showing success while using SQL Server Studio Express to connect remotely.此处显示使用 SQL Server Studio Express 进行远程连接时成功的图像。

在此处输入图片说明

"But why ...?" “但为什么 ...?”

For those interested in why SQL Server Management Studio (SSMS) can connect to servername\\instance while other applications (like our pyodbc apps) cannot, it's because SSMS keeps an MRU (Most Recently Used) list of port numbers in the Windows registry at对于那些对为什么SQL Server Management Studio (SSMS) 可以连接到servername\\instance而其他应用程序(如我们的 pyodbc 应用程序)不能连接感兴趣的人,这是因为 SSMS 在 Windows 注册表中保留了一个 MRU(最近使用的)端口号列表

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib\LastConnect

HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\MSSQLServer\Client\SuperSocketNetLib\LastConnect

Each MRU entry (registry value) looks something like this:每个 MRU 条目(注册表值)如下所示:

Name: PANORAMA\SQLEXPRESS
Type: REG_SZ 
Data: -1006030326:tcp:PANORAMA,52865

Once SSMS has successfully connected by instance name via the SQL Browser service on the remote machine, it can continue to connect by instance name even if the SQL Browser is no longer running on the remote machine, provided that the port number has not changed.一旦 SSMS 通过远程计算机上的 SQL Browser 服务按实例名称成功连接后,即使 SQL Browser 不再在远程计算机上运行,​​只要端口号未更改,它也可以继续按实例名称进行连接。 Apps that don't use this MRU list (like our pyodbc app) need to have the SQL Browser service running on the remote machine every time they want to connect by instance name.不使用此 MRU 列表的应用程序(如我们的 pyodbc 应用程序)需要在每次要按实例名称连接时在远程计算机上运行 SQL Browser 服务。

The most common scenario:最常见的场景:

  • I want to connect to YOUR-PC\\SQLEXPRESS .我想连接到YOUR-PC\\SQLEXPRESS I try doing that from SSMS on MY-PC , but it doesn't work because the SQL Browser was installed with "Start Mode" set to "Manual" on YOUR-PC .我尝试从MY-PC上的 SSMS 执行此操作,但它不起作用,因为 SQL 浏览器安装时在YOUR-PC上将“启动模式”设置为“手动”。
  • I ask you to start the SQL Browser service on YOUR-PC , and you kindly comply, but you just start the service and forget to change the "Start Mode" setting to "Automatic".我要求您在YOUR-PC上启动 SQL 浏览器服务,并且请您遵守,但您只是启动了该服务而忘记将“启动模式”设置更改为“自动”。
  • I am able to connect via SSMS (which caches the YOUR-PC\\SQLEXPRESS port in the MRU).我能够通过 SSMS(它在 MRU 中缓存YOUR-PC\\SQLEXPRESS端口)进行连接。 My python app can connect, too.我的 python 应用程序也可以连接。
  • After the next time YOUR-PC restarts, I can connect via SSMS (via the MRU) but my python app cannot (because the SQL Browser service is no longer running on YOUR-PC ).下次YOUR-PC重新启动后,我可以通过 SSMS(通过 MRU)进行连接,但我的 python 应用程序不能(因为 SQL Browser 服务不再在YOUR-PC上运行)。

Try specifying the port:尝试指定端口:

import pyodbc

server = r"xxxER\xxxSQLSERV"
db = "xxxDB"
user = "xxx"
password = "xxxx"
port = "1433"
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=' + server + ';PORT=' + port + ';DATABASE=' + db +';UID=' + user + ';PWD=' + password)

If you're still having issues, try using the IP or FQDN of the server.如果仍有问题,请尝试使用服务器的 IP 或 FQDN。

Try changing the Driver from 'SQL Server' to 'SQL Server Native Client 11.0'.尝试将驱动程序从“SQL Server”更改为“SQL Server Native Client 11.0”。

I had the same error message and this fixed it for me.我有相同的错误消息,这为我修复了它。

I have this problem.I can connect with Management Studio (SSMS) but not work with pyodbc.我有这个问题。我可以与 Management Studio (SSMS) 连接,但不能与 pyodbc 一起使用。 I add version odbc of sql and worked.我添加了 sql 的 odbc 版本并工作了。

change your code to:将您的代码更改为:

conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server + ';DATABASE=' + db +';UID=' + user + ';PWD=' + password)

If not work change version 17 to 13 if not to 11 .如果不起作用,请将版本 17 更改为 13 如果不是 11 。 List versions of ODBC. 列出 ODBC 的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM