更新:见下文
我是docker的新手,尝试在ubuntu 18主机上使用docker 18.05.0-ce(build f150324)和这些服务,在同一个网络中编写.net core 2.0 web api:
sql server数据库;
mysql数据库(https://github.com/docker-library/docs/tree/master/mysql/);
mongo数据库。
我的docker compose文件从它们各自的图像提供这些服务,如下所述。简而言之:
来自映像的sql server microsoft/mssql-server-linux
,端口1433, sa
通过环境变量设置用户密码 SA_PASSWORD
接受eula后,通过 ACCEPT_EULA
;
图片中的mongodb mongo
,端口27017;
来自图像的mysql mysql/mysql-server
,端口3306,密码 root
通过环境变量设置的用户 MYSQL_ROOT_PASSWORD
. 我基本上使用这些源来配置服务:https://github.com/docker-library/docs/tree/master/mysql#mysql_database 以及https://docs.docker.com/samples/library/mysql/#environment-变量。
当然,访问这些服务的webapi在其开发环境中使用其他凭据,但是我通过环境变量重写它们,以将系统调整为docker(在aspnet core 2中,您可以从中看到)https://github.com/aspnet/metapackages/blob/dev/src/microsoft.aspnetcore/webhost.cs,的 CreateDefaultBuilder
方法已包含环境变量作为配置源): DATA__DEFAULTCONNECTION__CONNECTIONSTRING
:到sql server的连接字符串,使用sa和docker服务设置的相同密码(见上文): "Server=sqlserver\\sqlexpress,1433;Database=lexmin;User Id=SA;Password=P4ss-W0rd!;MultipleActiveResultSets=true"
. 请注意,对于windows,我们使用 :
作为配置层次结构分隔符,但对于非windows操作系统,我们必须使用 __
(在https://github.com/aspnet/configuration/issues/469). SERILOG__CONNECTIONSTRING
与serilog记录器使用的连接相同。 LEX__CONNECTIONSTRING
是mongodb连接字符串: mongodb://mongo:27017/lex_catalog
. ZAX__CONNECTIONSTRING
是mysql连接字符串: Server=mysql;Database=zax_master;Uid=root;Pwd=password;SslMode=none
.
其他环境变量用于使用mysql和mongo命令行工具转储数据库,因为我应该从我的api调用它(从脚本中可以看到,我仍然需要找出这些可执行文件在ubuntu环境中的确切位置,但这是一个细节)。
这是相应的 appsettings.json
在my web api(缩写)中,它显示与环境变量名称对应的路径:
{
"Data": {
"DefaultConnection": {
"ConnectionString": "..."
}
},
"Serilog": {
"ConnectionString": "...",
"TableName": "Log",
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Information",
"System": "Warning"
}
}
},
"Lex": {
"ConnectionString": "..."
},
"Zax": {
"ConnectionString": "..."
},
"Environment": {
"MongoDirectory": "...",
"MySqlDirectory": "...",
"MySqlDumpUser": "root",
"MySqlDumpPassword": "..."
}
}
现在,当我跑的时候 docker-compose up
,所有服务启动正常,包括mysql;然而,我的应用程序在尝试连接到mysql时抛出了一个异常: Application startup exception: MySql.Data.MySqlClient.MySqlException (0x80004005): Host '172.19.0.5' is not allowed to connect to this MySQL server
.
有人能帮忙吗?这是我的作曲脚本:
version: '3.4'
services: # SQL Server at default port lexminmssql: image: microsoft/mssql-server-linux container_name: sqlserver environment: ACCEPT_EULA: Y SA_PASSWORD: "P4ss-W0rd!" ports: - 1433:1433 networks: - lexminnetwork
# MongoDB - at default port lexminmongo: image: mongo container_name: mongo ports: - 27017:27017 networks: - lexminnetwork
# MySql at default port lexminmysql: image: mysql/mysql-server container_name: mysql environment: # the password that will be set for the MySQL root superuser account MYSQL_ROOT_PASSWORD: "password" ports: - 3306:3306 networks: - lexminnetwork
# Web API lexminapi: image: naftis/lexminapi ports: - 58942:58942 depends_on: - lexminmssql: condition: service_healthy - lexminmongo: condition: service_healthy - lexminmysql: condition: service_healthy build: context: . dockerfile: LexminApi/Dockerfile environment: # for Windows use : as separator, for non Windows use __ # (see https://github.com/aspnet/Configuration/issues/469) DATA__DEFAULTCONNECTION__CONNECTIONSTRING: "Server=sqlserver\sqlexpress,1433;Database=lexmin;User Id=SA;Password=P4ss-W0rd!;MultipleActiveResultSets=true" SERILOG__CONNECTIONSTRING: "Server=sqlserver\sqlexpress,1433;Database=lexmin;User Id=SA;Password=P4ss-W0rd!;MultipleActiveResultSets=true" LEX__CONNECTIONSTRING: "mongodb://mongo:27017/lex_catalog" ZAX__CONNECTIONSTRING: "Server=mysql;Database=zax_master;Uid=root;Pwd=password;SslMode=none" # TODO: locate BIN directories in Linux ENVIRONMENT__MONGODIRECTORY: "" ENVIRONMENT__MYSQLDIRECTORY: "" ENVIRONMENT__MYSQLDUMPUSER: "root" ENVIRONMENT__MYSQLDUMPPASSWORD: "password" networks: - lexminnetwork volumes: - ./zax-users.xml:/etc/lexmin/zax-users.xml
# Web app # TODO
networks: lexminnetwork: driver: bridge
附加
谢谢,我正在尝试一次解决一个连接问题,因为我发现在我的组合容器中,sql server也会遇到同样的问题。因此,我也将这个问题扩展到SQLServer,但我认为最好是将讨论保持在同一个位置,因为这些问题看起来很相似。
我从一组较小的容器开始,目前排除了mysql。我的作曲者是:
version: '3.4'
services:
# SQL Server
cadmusmssql:
image: microsoft/mssql-server-linux
container_name: cadmussqlserver
environment:
ACCEPT_EULA: Y
SA_PASSWORD: "P4ss-W0rd!"
ports:
- 1433:1433
networks:
- cadmusnetwork
# MongoDB
cadmusmongo:
image: mongo
container_name: cadmusmongo
environment:
- MONGO_DATA_DIR=/data/db
- MONGO_LOG_DIR=/dev/null
ports:
- 27017:27017
command: mongod --smallfiles --logpath=/dev/null # --quiet
networks:
- cadmusnetwork
cadmusapi:
image: ...myprivaterepoimage...
environment:
- ASPNETCORE_ENVIRONMENT=Production
ports:
- 60304:60304
depends_on:
- cadmusmssql
- cadmusmongo
environment:
DATA__DEFAULTCONNECTION__CONNECTIONSTRING: "Server=127.0.0.1\\sqlexpress,1433;Database=cadmusapi;User Id=SA;Password=P4ss-W0rd!;MultipleActiveResultSets=true"
SERILOG__CONNECTIONSTRING: "Server=127.0.0.1\\sqlexpress,1433;Database=cadmusapi;User Id=SA;Password=P4ss-W0rd!;MultipleActiveResultSets=true"
networks:
- cadmusnetwork
networks:
cadmusnetwork:
driver: bridge
通过搜索,我发现需要在连接字符串中显式设置端口号。因此,即使当前我覆盖docker compose文件中的环境变量,我也会将它们添加到我的 appsettings.Production.json
,也是。在我的
Program.cs Main
方法,我设置如下配置(对于serilog:seehttp://www.carlrippon.com/?p=1118):
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile(
$"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json",
optional: true)
.Build();
所以这覆盖了 appsettings.json
文件 appsettings.Production.json
如果 ASPNETCORE_ENVIRONMENT
未指定。总之,为了让它更清楚,我在我的作曲中添加了:
environment:
+ ASPNETCORE_ENVIRONMENT=Production
为了检查我的环境变量,我在应用程序启动时添加了转储代码。转储具有预期的连接字符串:
cadmusapi_1 | ASPNETCORE_PKG_VERSION = 2.0.8
cadmusapi_1 | ASPNETCORE_URLS = http://+:80
cadmusapi_1 | DATA__DEFAULTCONNECTION__CONNECTIONSTRING = Server=127.0.0.1\sqlexpress,1433;Database=cadmusapi;User Id=SA;Password=P4ss-W0rd!;MultipleActiveResultSets=true
cadmusapi_1 | DOTNET_DOWNLOAD_SHA = d8f6035a591b5500a8b81188d834ed4153c4f44f1618e18857c610d0b332d636970fd8a980af7ae3fbff84b9f1da53aa2f45d8d305827ea88992195cd5643027
cadmusapi_1 | DOTNET_DOWNLOAD_URL = https://dotnetcli.blob.core.windows.net/dotnet/Runtime/2.0.7/dotnet-runtime-2.0.7-linux-x64.tar.gz
cadmusapi_1 | DOTNET_RUNNING_IN_CONTAINER = true
cadmusapi_1 | DOTNET_VERSION = 2.0.7
cadmusapi_1 | HOME = /root
cadmusapi_1 | HOSTNAME = 29884ca26699
cadmusapi_1 | PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
cadmusapi_1 | SERILOG__CONNECTIONSTRING = Server=127.0.0.1\sqlexpress,1433;Database=cadmusapi;User Id=SA;Password=P4ss-W0rd!;MultipleActiveResultSets=true
这是转储代码,可能对快速复制和粘贴有用:
private static void DumpEnvironment()
{
IDictionary dct = Environment.GetEnvironmentVariables();
List<string> keys = new List<string>();
var enumerator = dct.GetEnumerator();
while (enumerator.MoveNext())
{
keys.Add(((DictionaryEntry)enumerator.Current).Key.ToString());
}
foreach (string key in keys.OrderBy(s => s))
Console.WriteLine($"{key} = {dct[key]}");
}
但是,我不断从sqlserver得到连接错误,比如: Application startup exception: System.Data.SqlClient.SqlException (0x80131904): 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)
.
当服务启动时,我试图用ip127.0.0.1从我的(ubuntu)docker主机连接到它们。我安装了sql operations studio,并连接到 127.0.0.1\sqlexpress,1433
使用用户名 SA
以及在compose文件中指定的密码。那么,当在容器中从我的asp.net核心应用程序中使用相同的身份验证参数时,如何会失败呢?
1条答案
按热度按时间o4hqfura1#
这在我看来像是一个mysql安全错误。基本上,您需要配置mysql以允许外部连接。
这里有一个非常好的线索,通过故障排除步骤来解决这个问题。