"Unable to connect: Adaptive Server is unavailable or does not exist" in SQL Server Docker Image

ojsjcaue  于 2023-05-28  发布在  SQL Server
关注(0)|答案(1)|浏览(256)

I am trying to connect to a Docker container running SQL Server using pymssql and I am getting the following error,

Can't connect to db: (20009, b'DB-Lib error message 20009, severity 9:
Unable to connect: Adaptive Server is unavailable or does not exist (localhost)
Net-Lib error during Connection refused (111)
DB-Lib error message 20009, severity 9:
Unable to connect: Adaptive Server is unavailable or does not exist (localhost)
Net-Lib error during Connection refused (111)\n')

Now, I know similar questions have been asked before, such as the one given below,
Database connection failed for local MSSQL server with pymssql

However, most of the solutions given revolve around opening up the Sql Server Configuration Manager and making changes through the UI or allowing network traffic to pass through. However, I am not entirely certain how to do this with a Docker container.

How can I resolve this? Is there a different SQL Server I should run? Or a different Python package I should use to establish the connection?

I have spin up my container based on the instructions given here,
https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-ver16&pivots=cs1-bash

Given below are the parameters I am using to establish the connection,

{
    "host": "localhost",
    "port": "1433",
    "user": "SA",
    "password": "<my-password>",
    "database": "TestDB"
}

Update: I am actually trying to connect to the SQL Server Docker instance from within another container. It is the MindsDB container. This MindsDB container is, however, using pymysql to establish the connection.

Update: Given below are the two commands I am using to run my containers.
SQL Server:

sudo docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<my-password>" \
   -p 1433:1433 --name sql1 --hostname sql1 \
   -d \
   mcr.microsoft.com/mssql/server:2022-latest

MindsDB:

docker run -p 47334:47334 -p 47335:47335 mindsdb/mindsdb
z9smfwbn

z9smfwbn1#

The only way I was able to solve this is by using the host 172.17.0.1 . Nothing else worked: localhost , host.docker.internal etc.

This is how I ran my containers,

SQL Server:

sudo docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=SQLServer@123" \
   -p 1433:1433 --name sql1 --hostname sql1 \
   -d \
   mcr.microsoft.com/mssql/server:2022-latest

MindsDB:

docker run -p 47334:47334 -p 47335:47335 mindsdb/mindsdb

To establish a connection to SQL Server using MindsDB,

CREATE DATABASE mssql_datasource
WITH
engine='mssql',
PARAMETERS={
    "host": "172.17.0.1",
    "port": "1433",
    "user": "SA",
    "password": "SQLServer@123",
    "database": "TestDB"
};

Note: I am running Ubuntu. I have a feeling that this might be different for Windows or MacOS.

相关问题