0

On my computer I have two virtual machines running using VMWARE Workstation.

One Ubuntu 2.10 machine where my python (v. 3.8.10) code is running, and one Windows 10 machine where my Microsoft SQL Server is located.

I want to query data from SQL Server using Python code on my Ubuntu machine.

This is my code:

import pyodbc

server = 'xxx.xxx.xxx.xxx' <- IP address of the Windows VM
database = 'TestDB' 
username = 'TestUser' 
password = 'test123' 
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()

I get this error in response:

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

I have tried using these credentials to log in to Microsoft SQL Server Management Studio and it work just fine.

Thanks for your help.

---------EDIT----------

Efter runing query xp_readerrorlog 0, 1, N'Server is listening on' in SMS i get the following results:

LogDate ProcessInfo Text
2021-09-09 13:24:44.380 spid33s Server is listening on [ 'any' 49677].
2021-09-09 13:24:44.390 spid33s Server is listening on [ 'any' 49677].
JabbaThePadd
  • 97
  • 1
  • 7
  • Are you running SSMS on the same virtual machine that is hosting the SQL Server instance? – Gord Thompson Sep 03 '21 at 18:24
  • Yes I am. @GordThompson – JabbaThePadd Sep 03 '21 at 22:01
  • 1
    Okay, so SSMS is confirming that the database instance is up and running, but it is not telling us if the instance is reachable from other machines. Can you successfully `ping` the IP address of the Windows VM from the Ubuntu VM? – Gord Thompson Sep 03 '21 at 22:52
  • 1
    Check the TCP/IP settings in the Server Protocols section of SQL Server Configuration Manager. Depending on how SQL Server Express was installed it may only be listening on localhost,1433 - if the TCP/IP protocol is even enabled at all. – AlwaysLearning Sep 03 '21 at 23:43
  • I checked the TCP/IP settings in the SQL Server Configuration Manager. It was disabled so I enabled it and restarted the service but still got the same error when running the code on my Ubuntu machine. @AlwaysLearning – JabbaThePadd Sep 09 '21 at 11:38
  • @GordThompson At first I could not ping the WIndows VM, but after disabling the firewall I could ping the Windows VM from both the Host and Ubuntu VM. Eventhough I could ping succesfully, I still got the same error as before. – JabbaThePadd Sep 09 '21 at 12:02
  • 1
    What do you see when you run the query `xp_readerrorlog 0, 1, N'Server is listening on'` in SSMS? – Gord Thompson Sep 09 '21 at 12:30
  • Now that you've enabled the TCP/IP protocol itself what address(es) is it configured to listen on? 127.0.0.1 is the localhost address and can only be connected to by the Windows VM guest itself. You'd have to ensure that other addresses are listening, the Windows firewall allows incoming connections to them, and that the VM guest actually exposes one/some of those addresses to the virtual network so that the Ubuntu VM guest can connect to it. – AlwaysLearning Sep 09 '21 at 12:52
  • @GordThompson See my edit in the post for the results. What does this mean? – JabbaThePadd Sep 09 '21 at 13:11
  • 1
    The instance is listening on port 49677, so on the Ubuntu machine you need to specify `SERVER=xxx.xxx.xxx.xxx,49677` – Gord Thompson Sep 09 '21 at 13:31
  • @GordThompson Now it works without errors. Thank you! – JabbaThePadd Sep 09 '21 at 14:19

0 Answers0