如何从我的本地计算机中连接到Docker容器中的MSSQL实例?

发布于 2025-02-12 14:15:56 字数 1237 浏览 0 评论 0 原文

我正在尝试在我本地计算机上运行的Docker容器中连接到MSSQL的实例。我使用了Microsoft的这组指令来设置容器,并且容器中的所有内容似乎都按预期运行: https://learn.microsoft.com/en-us/sql/linux/quickstart-install-install-connect-docker?view=sql-sql-server-ver16&pivots = cs1-bash

我正在尝试使用Python从我的本地计算机(不在容器中)连接数据库。我已经尝试了各种迭代,但是连接脚本通常是:

import pyodbc
server = 'localhost,1433'
database = 'TestDB'
username = 'sa'
password = '<YourStrong@Passw0rd>'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()

生成PYODBC错误:

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

我检查了驱动程序是正确的

mssql@sql1:/$ cat /etc/odbcinst.ini
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.9.so.1.1
UsageCount=1

,并检查了端口是否打开。

谁能帮助如何使该连接起作用?

I'm trying to connect to an instance of MSSQL in a Docker container that is running on my local machine. I used this set of instructions from Microsoft to set up the container, and everything within the container seems to be functioning as expected:
https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-ver16&pivots=cs1-bash

I'm trying to connect to the database from my local machine (not in a container) using Python. I've tried various iterations, but the connection script is generally:

import pyodbc
server = 'localhost,1433'
database = 'TestDB'
username = 'sa'
password = '<YourStrong@Passw0rd>'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()

Which generates a pyodbc error:

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

I've checked the driver is correct

mssql@sql1:/$ cat /etc/odbcinst.ini
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.9.so.1.1
UsageCount=1

And checked that the ports are open.

Can anyone help with how to make this connection work?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文