【问题标题】:Connect Python to SQL Server from Ubuntu从 Ubuntu 将 Python 连接到 SQL Server
【发布时间】:2021-08-02 00:36:34
【问题描述】:

我的操作系统是 Windows 10

我的机器上安装了 SQL Server

我正在尝试从 Ubuntu bash 访问 SQL Server

这是我的代码

import pyodbc 

ConnectionString = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=DELLG3;DATABASE=R3;UID=sa;PWD=******;"

conn = pyodbc.connect(ConnectionString)

我收到了这个错误

asmgx@DELLG3:~$ python3 /mnt/c/py/28autosk.py
Traceback (most recent call last):
  File "/mnt/c/py/28autosk.py", line 58, in <module>
    conn = pyodbc.connect(ConnectionString)
pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')

我尝试了所有这些链接来帮助我解决问题,并且一直在谷歌搜索并尝试解决问题

https://www.microsoft.com/en-us/sql-server/developer-get-started/python/ubuntu/?rtc=1

https://askubuntu.com/questions/1147280/how-to-install-mssql-server-in-ubuntu-19-04

https://askubuntu.com/questions/462094/unable-to-install-libssl1-0-0i386-due-to-unmet-dependencies

谁能帮我从 Ubuntu 连接到 SQL Server?

【问题讨论】:

  • 如果你尝试从 Ubuntu 框中ping DELLG3 会发生什么?
  • @GordThompson Pings 好的。 PING DELLG3.localdomain (127.0.1.1) 56(84) 字节数据。来自 DELLG3.localdomain (127.0.1.1) 的 64 字节:icmp_seq=1 ttl=128 time=0.139 ms
  • 所以DELLG3 解析为 127.0.1.1,但这可能不是运行 SQL Server 实例的机器的 IP 地址。您能找出另一个 IP 地址是什么并尝试将其作为 SERVER= 参数吗?
  • Ubuntu bash 在我的 Windows 10 中运行。所以 ip 127.0.1.1 是 SQL Server IP
  • 嗯,好的,如果您在 bash 中运行 ifconfig eth0 并在 Windows 命令提示符中运行 ipconfig,您会看到相同的“真实”(不是 127.x.y.z)IP 地址吗?跨度>

标签: sql-server ubuntu pyodbc


【解决方案1】:

你应该试试

pyodbc.connect ('DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost;DATABASE=dbname;UID=sa;PWD=********')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 2021-06-19
    相关资源
    最近更新 更多