在 Windows 10 Pro 上从 WSL2 将 pyodbc 连接到 MS SQL 数据库时出现问题 ->乌班图20.04

发布于 2025-01-09 06:28:50 字数 1747 浏览 1 评论 0原文

我有一台装有 Windows 10 的电脑。我安装了 Microsoft SQL Server 2019。下一步,我在 Windows 10 上安装了 WSL2 (Ubuntu 20.04)。 我尝试从 WSL2 (Ubuntu 20.04) 连接到 Windows 10 上的 MS SQL。

我已经尝试了我所知道的和我发现的一切。下面是我尝试过的关于该主题的两篇文章,但没有成功。

我在Windows防火墙中打开了1433端口,并尝试使用他,而不是在服务器和驱动程序的所有选项中。我尝试对服务器使用五个选项,对驱动程序使用两个选项,您可以在下面的代码中看到这些选项。我尝试编辑文件/etc/hosts,在其中添加IP地址172.29.176.1(其操作在第二个链接中描述)

使用 pyodbc 从 WSL2 连接到 Windows 上的 SQL Server - 连接超时

连接到 SQL Server从 WSL 2/Ubuntu sqlcmd 在 Windows 主机上运行

如果从 Windows 连接到 MS SQL,一切正常。

可能是什么问题,我做错了什么,我将不胜感激您的帮助?!

import pyodbc
import pandas as pd
import socket

#server1 = f'{socket.gethostname()}.local'
#server2 = 'DESKTOP-2TUPNJK.local'
#server3 = '172.29.176.1' # this is IP-address WSL2 and this IP-address specified in WSL2 (Ubuntu 20.04) -> /etc/resolv.conf
#server4 = '192.168.1.4' # this is IP-address my PC on Windows 10
server5 = '127.0.0.1' # this is IP-address from /etc/hosts -> localhost

#driver1 = '{ODBC Driver 17 for SQL Server}'
driver2 = '/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.9.so.1.1'

cnxn = pyodbc.connect('DRIVER=' + driver2 + ';SERVER='+server5+';PORT=1433;DATABASE=Test;UID=user;PWD=Password')  
df = pd.read_sql_query('SELECT name FROM sys.Tables', cnxn)
print(df)

我收到下一个错误:

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

I have pc with Windows 10. I installed Microsoft SQL Server 2019. Next action, on Windows 10 I installed WSL2 (Ubuntu 20.04). I try to connect from WSL2 (Ubuntu 20.04) to MS SQL on Windows 10.

I have tried everything I know and what I have found. Below are two articles on the subject that I have tried with no success.

I opened the 1433 port in Windows Firewall, and tried use him and not in all options with server and driver. I tried use five options with server and two options with driver, you can see this options in code bellow. I tried edit file /etc/hosts, where I add IP-address 172.29.176.1 (it action describe in second link)

Connect to SQL Server on Windows from WSL2 with pyodbc - Connection Timeout

Connect to SQL Server running on Windows host from a WSL 2/Ubuntu sqlcmd

If connect to the MS SQL from Windows everything works.

What could be the problem, what am I doing wrong, I will be grateful for the help?!

import pyodbc
import pandas as pd
import socket

#server1 = f'{socket.gethostname()}.local'
#server2 = 'DESKTOP-2TUPNJK.local'
#server3 = '172.29.176.1' # this is IP-address WSL2 and this IP-address specified in WSL2 (Ubuntu 20.04) -> /etc/resolv.conf
#server4 = '192.168.1.4' # this is IP-address my PC on Windows 10
server5 = '127.0.0.1' # this is IP-address from /etc/hosts -> localhost

#driver1 = '{ODBC Driver 17 for SQL Server}'
driver2 = '/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.9.so.1.1'

cnxn = pyodbc.connect('DRIVER=' + driver2 + ';SERVER='+server5+';PORT=1433;DATABASE=Test;UID=user;PWD=Password')  
df = pd.read_sql_query('SELECT name FROM sys.Tables', cnxn)
print(df)

I get next error:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呢古 2025-01-16 06:28:50

我设法解决了这个问题:

首先 ->服务器名称必须是您 PC 的 IP 地址(Windows cmd 中的 ipconfig)

第二个 ->;下一步请按照此说明 (我没有设置 -> ApexSQL 工具)

这是我的代码:

import pyodbc
import pandas as pd

cnxn = pyodbc.connect('DRIVER=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.9.so.1.1;SERVER=192.168.0.2,1433;DATABASE=Test;UID=sa;PWD=Test')

df = pd.read_sql_query('SELECT * FROM sys.Tables', cnxn)
print(df)

I managed to solve this issue:

First -> the server name must be the IP address of your PC (ipconfig in Windows cmd)

Second -> next step follow this instruction (I didn't set up -> ApexSQL tools)

And this is my code:

import pyodbc
import pandas as pd

cnxn = pyodbc.connect('DRIVER=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.9.so.1.1;SERVER=192.168.0.2,1433;DATABASE=Test;UID=sa;PWD=Test')

df = pd.read_sql_query('SELECT * FROM sys.Tables', cnxn)
print(df)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文