在Docker Compose中连接ASP.NET核心+ MySQL

wyyhbhjk  于 2022-11-02  发布在  Docker
关注(0)|答案(1)|浏览(147)

我在ASP.NET Core 6.0和Docker容器中的MySQL数据库之间的连接中遇到错误。
错误:

workwise-aspnet-mvc-mysql-csharpemployeecrud-1  |        ---> MySqlConnector.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.

workwise-aspnet-mvc-mysql-csharpemployeecrud-1  |          at MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, MySqlConnection connection, Int32 startTickCount, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/ServerSession.cs:line 433
workwise-aspnet-mvc-mysql-csharpemployeecrud-1  |

当使用dotnet run运行应用程序时,我可以在容器中连接数据库,并像往常一样进行所有CRUD操作,但当运行docker compose up并通过容器访问应用程序时,它不起作用。
我怀疑这是在ConnectionString中的一些不同的东西或类似的东西,但我还不能发现。
这是我的docker-compose.yml文件:

version: '3.4'

services:
  csharpemployeecrud:
    image: csharpemployeecrud
    build:
      context: .
      dockerfile: ./Dockerfile
    ports:
      - 5255:5255

  db:
    container_name: 'employee_csharp'
    image: mysql:8.0.29
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    ports: 
      - 3307:3306
    environment: 
      - MYSQL_ROOT_PASSWORD=root
      - TZ=America/Sao_Paulo

这是我的Program.cs文件:

using Microsoft.EntityFrameworkCore;
using CSharp_EmployeeCrud.Data;

var builder = WebApplication.CreateBuilder(args);
var connectionString = "server=localhost;port=3307;user=root;password=root;database=employee_csharp";
var serverVersion = new MySqlServerVersion(new Version(8, 0, 29));

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<EmployeeContext>(
    dbContextOptions => dbContextOptions
                .UseMySql(connectionString, serverVersion)
                // The following three options help with debugging, but should
                // be changed or removed for production.
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging()
                .EnableDetailedErrors()
);
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

为了以防万一,这是“docker”分支的存储库:https://github.com/brunogroth/Workwise-ASPNET-MVC-MYSQL/tree/docker
关于connectionString,我也尝试过传递以下参数,都没有成功:
第一个
还尝试传递不带port参数的connectionString。没有新内容。
希望你们能帮帮我!

eqoofvh9

eqoofvh91#

我会使用您尝试过的连接字符串:

connectionString = "server=db;port=3307;user=root;password=root;database=employee_csharp"

并将数据库添加到docker-compose.yaml文件中
MYSQL_DATABASE: employee_csharp
以及出于测试目的而删除该命令。

--default-authentication-plugin=mysql_native_password

您还可以尝试通过DB-Viewer工具进行连接,以确保可以从容器外部访问数据库。

相关问题