Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Using the SQL Server Management Studio (SSMS) Express, I can find the database and connect without problems.

But when I use pyodbc to connect to the same server using:

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:

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.

enter image description here

like image 718
Hangon Avatar asked Jan 06 '16 14:01

Hangon


People also ask

What is the alternative to Pyodbc?

Another alternative is pypyodbc which was written in pure Python. it can been seen as a re-implemenation of the pyodbc module – with only around 1800 lines code, which is good for maintenance.

How do I connect to a remote database in SQL Server Management Studio?

Start the SQL Server, in the dialog window for the Server name enters the name of the instance that you want to connect with. From the Authentication drop down box, select the SQL Server Authentication and for the field Login and the Password enter your credentials then click the Connect button.

How do I get SQL Server to accept remote connections?

Using SQL Server Management Studio In Object Explorer, right-click a server and select Properties. Select the Connections node. Under Remote server connections, select or clear the Allow remote connections to this server check box.


3 Answers

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.

like image 80
FlipperPA Avatar answered Oct 06 '22 04:10

FlipperPA


"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

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:

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. 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.

The most common scenario:

  • I want to connect to 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.
  • 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".
  • I am able to connect via SSMS (which caches the YOUR-PC\SQLEXPRESS port in the MRU). My python app can connect, too.
  • 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).
like image 32
Gord Thompson Avatar answered Oct 06 '22 02:10

Gord Thompson


Try changing the Driver from 'SQL Server' to 'SQL Server Native Client 11.0'.

I had the same error message and this fixed it for me.

like image 45
louise Avatar answered Oct 06 '22 03:10

louise