Docker Compose部署MySQL容器启动后立即停止问题求助
问题:MySQL容器启动后立即停止,InnoDB初始化失败
环境配置
docker-compose.yml
version: '3.4' networks: backend: services: mysqldbnew: container_name: community-db-new image: mysql:8.0 ports: - 8006:3306 environment: - MYSQL_DATABASE=dbissues - MYSQL_ROOT_PASSWORD=**** volumes: - ./mysql-data:/var/lib/mysql networks: - backend communitydb.server: container_name: community-db-server image: ${DOCKER_REGISTRY-}communitydbserver build: context: . dockerfile: CommunityDB.Server/Dockerfile ports: - 8002:80 depends_on: - mysqldbnew environment: - DB_HOST=mysqldbnew - DB_NAME=dbissues - DB_ROOT_PASSWORD=**** networks: - backend
Program.cs
using CommunityDB.Server.Data; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); builder.Services.AddCors(); var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); builder.Services.AddDbContext<DataContext>(options => { options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)); }); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); //see in swagger url has the following formats; https://localhost:7128/api/Account/register app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); app.UseDefaultFiles(); app.UseStaticFiles(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.MapFallbackToFile("/index.html"); app.Run();
连接字符串配置
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "ConnectionStrings": { "DefaultConnection": "Server=mysqldbnew;Port=3306;Database=dbissues;User=root;Password=****;" } }
问题现象
执行docker-compose命令后,ASP.NET Core服务容器正常启动,但MySQL容器启动后立刻停止,日志关键错误如下:
2023-11-06 16:02:34 2023-11-06T12:02:34.234888Z 1 [ERROR] [MY-012960] [InnoDB] Cannot create redo log files because data files are corrupt or the database was not shut down cleanly after creating the data files. 2023-11-06 16:02:34 2023-11-06T12:02:34.234972Z 1 [ERROR] [MY-012930] [InnoDB] Plugin initialization aborted with error Generic error. 2023-11-06 16:02:34 2023-11-06T12:02:34.520305Z 1 [ERROR] [MY-010334] [Server] Failed to initialize DD Storage Engine 2023-11-06 16:02:34 2023-11-06T12:02:34.551241Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed. 2023-11-06 16:02:34 2023-11-06T12:02:34.551322Z 0 [ERROR] [MY-010119] [Server] Aborting
已尝试方案
- 删除Docker中的卷
- 删除Docker镜像并重启Docker
解决建议
日志明确指向本地挂载的./mysql-data目录中存在损坏的InnoDB数据文件,之前的清理操作未彻底清除本地目录的脏数据,按以下步骤操作:
- 停止并清除所有相关容器、卷
docker-compose down -v - 彻底删除本地的mysql-data目录
- Linux/macOS:
rm -rf ./mysql-data - Windows(PowerShell):
Remove-Item -Recurse -Force .\mysql-data
- Linux/macOS:
- 重新启动服务
docker-compose up -d
原理:本地挂载的目录保留了之前异常停止时损坏的InnoDB redo log和数据文件,MySQL启动时无法初始化这些损坏的文件,彻底删除后会重新创建干净的数据库文件。
内容的提问来源于stack exchange,提问作者Akshay H
相关产品推荐
相关产品推荐

