简体   繁体   English

pyodbc 连接到在 docker 容器上运行的 MS SQL Server

[英]pyodbc connection to MS SQL Server running on a docker container

pyodbc doesn't seem to be able to connect when trying to connect through specifying Driver.尝试通过指定驱动程序进行连接时,pyodbc 似乎无法连接。 I am able to connect to setting up a DSN but I also want to make connection when user has got the Driver, Server, UID, PWD and Database details.我能够连接以设置 DSN,但我还想在用户获得驱动程序、服务器、UID、PWD 和数据库详细信息时建立连接。

I am on Mac and and using FreeTDS driver.我在 Mac 上并且使用 FreeTDS 驱动程序。

freetds.conf freetds.conf

[MYMSSQL]
host = 0.0.0.0
port = 1433
tds version = 7.3

odbcinst.ini odbcinst.ini

[FreeTDS]
Description=FreeTDS Driver for Linux & MSSQL
Driver=/usr/local/lib/libtdsodbc.so
Setup=/usr/local/lib/libtdsodbc.so
UsageCount=10

Here is how I am trying to connect:这是我尝试连接的方式:

conn_str = "DRIVER=FreeTDS;SERVER={0};UID={1};PWD={2};DATABASE={3}".format('MYMSSQL', 'sa', 'password','tempdb')
conn = pyodbc.connect(conn_str)

The error I get is this:我得到的错误是这样的:

pyodbc.OperationalError: ('08001', '[08001] [FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')

Exact same database details work when I try to connect through DSN.当我尝试通过 DSN 连接时,完全相同的数据库详细信息工作。

If you have a freetds.conf file containing the host name/IP and port, eg,如果您有一个包含主机名/IP 和端口的 freetds.conf 文件,例如,

gord@xubuntu64-nbk1:~$ cat /etc/freetds/freetds.conf 
[myFreeTdsHost]
    host = 192.168.0.179
    port = 49242

then you can use both of those values in your DSN-less connection string by simply specifying the SERVERNAME=然后您可以通过简单地指定SERVERNAME=在无 DSN 的连接字符串中使用这两个值

# get host name/IP and port from freetds.conf
cnxn_str = (
    'DRIVER=FreeTDS_ODBC;'
    'SERVERNAME=myFreeTdsHost;'
    'DATABASE=myDb;'
    'UID=sa;PWD=_whatever_;'
)

You can also supply the host name/IP and port directly via SERVER= and PORT= like so您还可以像这样直接通过SERVER=PORT=提供主机名/IP 和端口

# supply host name/IP and port directly (bypassing freetds.conf)
cnxn_str = (
    'DRIVER=FreeTDS_ODBC;'
    'SERVER=192.168.0.179;'
    'PORT=49242;'
    'DATABASE=myDb;'
    'UID=sa;PWD=_whatever_;'
)

For details, see the FreeTDS documentation here .有关详细信息,请参阅此处的 FreeTDS 文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与pyodbc到镜像MS SQL Server的第二次连接上的Segfault - Segfault on 2nd connection with pyodbc to mirrored MS SQL Server SQLAlchemy PyODBC MS SQL Server少DSN连接 - SQLAlchemy PyODBC MS SQL Server DSN-less connection PYODBC + MS SQL SERVER 连接与 Encrypt=yes 未连接 - PYODBC + MS SQL SERVER connection with Encrypt=yes not connecting 远程连接到 MS SQL - 使用 pyodbc 时出错与使用 SQL Server Management Studio 成功 - Remote connection to MS SQL - Error using pyodbc vs success using SQL Server Management Studio 使用pyodbc将Python连接到MS SQL Server - Connect Python to MS SQL Server using pyodbc 编码从pyodbc到MS SQL Server的调用 - Encoding calling from pyodbc to a MS SQL Server 我可以在与pyodbc和MS SQL Server的一个连接上使用多个游标吗? - Can I use multiple cursors on one connection with pyodbc and MS SQL Server? 如何为 SQL Server/Linux 堆栈的 python/pyodbc/unixODBC/MS ODBC Driver 11 设置数据库连接超时? - How to set a DB connection timeout for a python/pyodbc/unixODBC/MS ODBC Driver 11 for SQL Server/Linux stack? 如何使用 sqlalchemy+pyodbc 和 MS SQL Server 中的多个数据库为 pandas read_sql 创建 sql alchemy 连接? - How to create sql alchemy connection for pandas read_sql with sqlalchemy+pyodbc and multiple databases in MS SQL Server? SQL Server 2017 中的 pyodbc 连接 - pyodbc connection within SQL Server 2017
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM