然后我在 IIS Express 上运行调试一切正常。
我通过 Ssh.NET 库通过 ssh 连接到数据库。
PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo("here is remote ip", 22, "ssh login here", "password for ssh")
{
Timeout = TimeSpan.FromSeconds(30)
};
var client = new SshClient(connectionInfo);
client.Connect();
ForwardedPortLocal portForward = new ForwardedPortLocal("127.0.0.1", 22, "127.0.0.1", 3306);
client.AddForwardedPort(portForward);
portForward.Start();
services.AddDbContext<WhetherContext>(options =>
options.UseMySQL(Configuration.GetConnectionString("Server=127.0.0.1;Port=22;Database=MyDb;Uid=MySqlDatabaseLogin;Pwd=DbPassword;SslMode=none;")));
当我在 docker-machine 上运行它时,它会在尝试使用上下文时抛出异常。
我使用 Linux 容器。可能是什么原因造成的?
更新: Dockerfile
FROM microsoft/aspnetcore:2.0 AS base
RUN curl -sL https://deb.nodesource.com/setup_8.x
RUN bash -
RUN apt-get -y update
RUN apt-get install -y build-essential nodejs
RUN ln -s /usr/bin/nodejs /usr/bin/node
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY WhetherService/WhetherService.csproj WhetherService/
RUN dotnet restore
COPY . .
WORKDIR /src/WhetherService
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WhetherService.dll"]
docker-compose.yml
version: '3'
services:
whetherservice:
image: alexandrs/whetherservice
build:
context: .
dockerfile: WhetherService/Dockerfile
docker-compose.ci.build.yml
version: '3'
services:
ci-build:
image: microsoft/aspnetcore-build:1.0-2.0
volumes:
- .:/src
working_dir: /src
command: /bin/bash -c "dotnet restore ./WhetherService.sln && dotnet publish ./WhetherService.sln -c Release -o ./obj/Docker/publish"
/etc/mysql/my.cnf文件:
The MySQL database server configuration file.
You can copy this to one of: - "/etc/mysql/my.cnf" to set global options, - "~/.my.cnf" to set user-specific options.
One can use all long options that the program supports. Run program with --help to get a list of available options and with --print-defaults to see which it would actually understand and use.
For explanations see http://dev.mysql.com/doc/mysql/en/server-system-variables.html
- IMPORTANT: Additional settings that can override those from this file! The files must end with '.cnf', otherwise they'll be ignored.
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
netstat -tln :
最佳答案
从您提供的信息来看,您似乎只是忘记将远程 mysql 连接到图像。这样做的方法是在远程服务器上打开一个端口进行监听,然后在运行该程序的 docker 镜像中,您需要使用带有该远程服务器的主机名或 IP 地址的 conn 字符串进行连接。
关于c# - 在 docker 机器上调试时 MySql 连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48600904/