如何使用Python连接到另一台机器上的MS SQL Server 2019?

huangapple go评论62阅读模式
英文:

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

问题

以下是您要翻译的内容:

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.\r\n (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)')
英文:

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.\r\n (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)')

答案1

得分: 0

如我在上面的第一个编辑中提到的,问题实际上不是一个问题,而是位于我的计算机和SQL服务器之间网络路径上的一系列防火墙。在紧邻SQL服务器旁边的应用服务器上运行脚本是成功的。在我的情况下,有效的连接字符串格式如下:

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

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;"

huangapple
  • 本文由 发表于 2023年3月3日 20:31:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定