简体   繁体   中英

SQL Server running as docker container on Macbook. I am unable to connect with python

I am trying to connect to a SQL SERVER database that is running as a docker container on my macbook. I am able to connect to the database using Azure Data Studio. I have been unsuccessful in connecting via sqlcmd as well from my local macbook. When I open a python3 interactive session from my local macbook and type:

import pyodbc 
pyodbc.drivers()

I get the following output:

['ODBC Driver 17 for SQL Server']

So I think my driver is setup correctly. The README_NOTES files says the specific driver version is 17.10.1.1.

I then try to setup a connection string using something like this:

cnxn_str = ("Driver={ODBC Driver 17 for SQL Server};Server=localhost;Database=test;UID=SA;PWD=my_password;")
cnxn = pyodbc.connect(cnxn_str)

And I get:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')

Any help figuring out what the issue is would be appreciated.

I have already created a docker bridge.network and inspected the.network and got the IP address 172.20.0.2. I have also used that IP address in place of localhost with the same result.

Here is my docker container:

CONTAINER ID   IMAGE                                        COMMAND                  CREATED        STATUS      PORTS                    NAMES
ab983a860cc5   mcr.microsoft.com/mssql/server:2019-latest   "/opt/mssql/bin/perm…"   2 months ago   Up 5 days   0.0.0.0:1433->1433/tcp   sql_server

It turns out that since I am using a company laptop they had the firewall locked down and the docker application was not included in the list of firewall exceptions. Once I was able to gain access to the firewall settings, I was able to add the docker application to the firewall exception list and I was able to connect via python and sqlcmd on local port 1433 mapped to my docker container using "localhost,1433"

Thank you to the community for working through this issue with me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM