gpt4 book ai didi

sql-server - Linux 上的 Microsoft SQL Server (docker) - 连接被拒绝

转载 作者:太空宇宙 更新时间:2023-11-04 04:55:48 25 4
gpt4 key购买 nike

我正在尝试从我的 Asp.Net Core 应用程序连接到我使用 docker 运行的 MSSQL,但收到一条错误消息:

"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 35 - An internal exception was caught)"

这是我的 docker 组成:

  api:
image: api
container_name: api
build:
context: ./Api
dockerfile: Dockerfile
ports:
- "8000:80"
depends_on:
- db
db:
image: "microsoft/mssql-server-linux"
container_name: mssql
environment:
SA_PASSWORD: "YourStrong!Passw0rd"
ACCEPT_EULA: "Y"
MSSQL_PID: "Developer"
ports:
- "127.0.0.1:8001:1433"

和appsettings.json:

{
"ConnectionStrings": {
"DefaultConnection": "server=127.0.0.1,8001;database=MyTestDb;user=sa;password=YourStrong!Passw0rd"
}
}

通过 SSMS 的连接效果很好,但在我的代码中我尝试使用:

Database.EnsureCreated();

我收到了上面描述的错误。

你知道我该如何解决这个问题吗?

最佳答案

您的问题是连接字符串

{
"ConnectionStrings": {
"DefaultConnection": "server=127.0.0.1,8001;database=MyTestDb;user=sa;password=YourStrong!Passw0rd"
}
}

在 docker-compose 容器到容器网络中,您关心容器本身公开的容器服务名称和端口。您不想考虑容器内部的主机端口映射。使用下面的连接字符串

{
"ConnectionStrings": {
"DefaultConnection": "server=db, 1433;database=MyTestDb;user=sa;password=YourStrong!Passw0rd"
}
}

关于sql-server - Linux 上的 Microsoft SQL Server (docker) - 连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46510920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com