--initialize specified but the data directory has files in it. Aborting. MySQL

In my compose.yaml I have this volume bind to my container mysql:

  chatbot_database:
    image: yukari268/primasbot:chatbot_database_JAN
    container_name: chatbot_database
    user: "1000:50"
    command: [--ignore-db-dir=lost+found]
    ports:
      - 3306:3306
    restart: unless-stopped
    volumes:
      - "chatbot_database_volume:/var/lib/mysql"
    environment:
    - MYSQL_ROOT_PASSWORD=root_password
    healthcheck:
         test: ["....."]
         timeout: 20s
         interval: 30s
         start_period: 300s
         retries: 5


volumes:
  chatbot_database_volume:
    driver: local
    driver_opts:
      type: none
      o: bind

and i have a simple image using mysql as base image:

FROM mysql:8.2.0

WORKDIR /db

COPY init.sql /docker-entrypoint-initdb.d/

# Update my.cnf for more detailed error logging
RUN sed -i 's/bind-address\s*=\s*127\.0\.0\.1/bind-address = 0.0.0.0/' /etc/my.cnf && \
    sed -i '/^#\s*log_error/s/^# //' /etc/my.cnf || \
    echo "log_error = /var/log/mysql/error.log" >> /etc/my.cnf

I followed a thread on github “–initialize specified but the data directory has files in it” error when setting volume for subdirectories · Issue #757 · docker-library/mysql · GitHub added user: "1000:50" to my service. and i also followed a Stack Overflow post php - Initializing mysql directory error - Stack Overflow. But none of the solution work i keep getting the error:

chatbot_database      | 2024-01-23 04:35:11+00:00 [Note] [Entrypoint]: Initializing database files
chatbot_database      | 2024-01-23T04:35:11.018564Z 0 [System] [MY-015017] [Server] MySQL Server Initialization - start.
chatbot_database      | 2024-01-23T04:35:11.043003Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.2.0) initializing of server in progress as process 41
chatbot_database      | 2024-01-23T04:35:11.047474Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
chatbot_database      | 2024-01-23T04:35:11.047527Z 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.
chatbot_database      | 2024-01-23T04:35:11.050646Z 0 [ERROR] [MY-010119] [Server] Aborting
chatbot_database      | 2024-01-23T04:35:11.050877Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.2.0)  MySQL Community Server - GPL.
chatbot_database      | 2024-01-23T04:35:11.051437Z 0 [System] [MY-015018] [Server] MySQL Server Initialization - end.

My OS:

root@debian11:~/compose# lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 11 (bullseye)
Release:        11
Codename:       bullseye

Docker version:

root@debian11:~/compose# docker version
Client: Docker Engine - Community
 Version:           24.0.7
 API version:       1.43
 Go version:        go1.20.10
 Git commit:        afdd53b
 Built:             Thu Oct 26 09:08:17 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          24.0.7
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.10
  Git commit:       311b9ff
  Built:            Thu Oct 26 09:08:17 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.26
  GitCommit:        3dd1e886e55dd695541fdcd67420c2888645a495
 runc:
  Version:          1.1.10
  GitCommit:        v1.1.10-0-g18a0cb0
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Side note: When i run this without the volume option. It works normally. But as any database container i want to save data so please anyone please send help. :frowning:

Why do you have all those options?

Why not just go with simple and defaults?

volumes:
  chatbot_database_volume:

The only additional parameter I ever used was external: true (doc).

PS: I assume you want to use a Docker volume, not a bind mount of a local folder. For bind mount (doc) you just need

service:
  name:
    …
    volumes:
      - /path/outside:/path/inside
    …

I did try with a similar one. I tried only the driver option: local, but it still doesn’t work. If i tried to leave everything empty like:

volumes:
  chatbot_database_volume
  callflow_database_volume
  gptbot_database_volume

Docker will reposonse: volumes must be a mapping

You need a : after each volume name.

1 Like

I tried again. But it still has that bug --initialize specified but the data directory has files in it. Aborting. . Weird

Maybe you created volumes before. Try docker volume ls and docker volume rm.

1 Like

It worked.But for future reference. I want to ask a question: "If i want to update the database and start new one. Do i need to zip the data in the volume folder and then clear it. Or do i need to add external: true like you. "

external: true means that an external created Docker volume is used, that was manually created on the command line.

Usually the volume of docker-compose.yml is created when you use compose up, or an existing previously created will be used.

When you use compose down the volume will be kept, unless you use compose down --volumes which will remove them.

Personally I prefer bind mounts for important data, that way it’s a bit easier to make a backup of the database files. And you can (almost) never delete the files when making a mistake with Docker.

1 Like