docker-compose启动mysql一直提示挂载目录存在文件

version: "3.8"
services:
  mysql:
    image: mysql:8.0.33
    container_name: mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_DATABASE: sample_db
      TZ: Asia/Shanghai
    ports:
      - "3306:3306"
    volumes:
      - /example/workspace/docker/dev-db/mysql8:/var/lib/mysql
      - /example/cloud-demo-main/doc/sql/nacos-init.sql:/docker-entrypoint-initdb.d/nacos-init.sql
      - /example/cloud-demo-main/doc/sql/seata-init.sql:/docker-entrypoint-initdb.d/seata-init.sql
      - /example/cloud-demo-main/doc/sql/seata-project-init.sql:/docker-entrypoint-initdb.d/seata-project-init.sql
    command:
      - --authentication_policy=mysql_native_password
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p123456"]
      interval: 5s
      timeout: 60s
      retries: 10

 上面的docker-compose.yml启动一直报错,实际挂载目录是干净的。

2025-06-30 06:16:17+08:00 [Note] [Entrypoint]: Initializing database files 2025-06-29T22:16:17.737732Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead. 2025-06-29T22:16:17.737830Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead. 2025-06-29T22:16:17.737848Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.42) initializing of server in progress as process 80 2025-06-29T22:16:17.739493Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting. 2025-06-29T22:16:17.739500Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it. 2025-06-29T22:16:17.739558Z 0 [ERROR] [MY-010119] [Server] Aborting 2025-06-29T22:16:17.739713Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.42) MySQL Community Server - GPL.

 解决方法,怀疑dokcer一直在使用什么缓存,读取旧的容器信息,存在脏数据

解决方法:

# 停止并删除容器
docker stop mysql
docker rm mysql

# 删除所有与 MySQL 相关的匿名卷(警告:这将删除所有 MySQL 容器的数据)
docker volume rm $(docker volume ls -qf dangling=true | grep mysql)

# 删除所有与 MySQL 相关的网络(谨慎操作,确保不删除其他服务需要的网络)
docker network prune

你可能感兴趣的:(docker-compose启动mysql一直提示挂载目录存在文件)