How to connect to a MS SQL Server 2019 on another machine with Python?

Question:

The MS SQL server I am trying to connect with is in another machine within my corporate network. Many laboratory external devices write on the SQL service without issues, so I know that the server is configured to accept external connections. In the "Sql Server Configuration Manager" I can see that for the DB of interest port 1434 is enabled, and with netstat I see that indeed SQL Browser is active and listening to the port. The server is configured to use windows authentication. Yet when I try to connect from my computer I get errors. This is the test script I am using:

import pyodbc as sql

def main():

    print("Let's test!")

    try:
        DB = sql.connect('DRIVER={ODBC Driver 17 for SQL Server}; SERVER=xxx.xxx.xxx.xxx; PORT=1434; DATABASE=DB; Trusted_Connection=yes;')
    except Exception as ex:
        print("It occurred a problem!")
        print("Exception: {0}. Arguments: {1!r}".format(type(ex).__name__, ex.args))
        return 0
    print("It worked! Congrats")
    return 1

if __name__ == '__main__':
    main()

This is the output I get:

Let's test!
It occurred a problem!
Exception: OperationalError. Arguments: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [2].  (2) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (2)')

I suspect that I have formatted the string in connect wrong, but I can’t happen to find any documentation with examples to hook with external servers, only on localhost. I have tried also to give ADDRESS instead of SERVER, tried to pass the IP instead of the instance name, tried various formatting choices, passed the port directly… To no avail. Does anybody have an idea of what goes wrong?

EDIT: I have been running some diagnostics on the network and it seems that there is something between my computer and the SQL server that times out before establishing connection with the SQL server. The tracert printout is this:

Tracing route to hostname.company.com [xxx.xxx.xxx.xxx]
over a maximum of 30 hops:

  1     7 ms     2 ms     2 ms  routername.company.com [xxx.xxx.xxx.xxx] 
  2     1 ms     1 ms     2 ms  xxx.xxx.xxx.xxx
  3     2 ms     2 ms     2 ms  xxx.xxx.xxx.xxx
  4     2 ms     2 ms     2 ms  xxx.xxx.xxx.xxx
  5     *        *        *     Request timed out.
  6     2 ms     2 ms     2 ms  hostname.company.com [xxx.xxx.xxx.xxx] 

Trace complete.

When I do establish connection from another server that sits next to the SQL server in the network I have no issues whatsoever, and the tracert dump shows that the connection doesn’t go through *… That’s a firewall I suspect? My error prompt is indeed a time out:

It occurred a problem!
Exception: OperationalError. Arguments: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: The wait operation timed out.rn (258) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (258)')
Asked By: Fulvio

||

Answers:

As I mentioned in my first edit above, it turned out that the problem was not one, but a series of firewalls in the network path between my machine and the SQL server. Running the script from the application server right next to the SQL was successful. In my case, the connection string formatting that worked is this:

"DRIVER={ODBC Driver 17 for SQL Server}; SERVER={tcp:xxx.xxx.xxx.xxx,1434}; DATABASE={DB_name}; Trusted_Connection=yes;"

Answered By: Fulvio
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.