简体   繁体   English

docker-compose 卷问题和错误 -- 指定了初始化,但数据目录中有文件。 中止

[英]docker-compose volume question and error --initialize specified but the data directory has files in it. Aborting

I had a spring-boot project that used mysql docker-image so I didn't need to download the mysql benchwork.我有一个使用 mysql docker-image 的 spring-boot 项目,所以我不需要下载 mysql 基准工作。 For other reasons I had to start over so I created a new project that uses the same mysql docker image I previously used.由于其他原因,我不得不重新开始,所以我创建了一个新项目,该项目使用我之前使用的相同 mysql docker 图像。 My docker-compose.yml mysql service looks like this我的 docker-compose.yml mysql 服务看起来像这样

version: "3.7"

services:

  db:
    image: mysql:5.7
    command: --lower_case_table_names=1
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: farming_db
      MYSQL_USER: root
      MYSQL_PASSWORD: root
    restart: always
    volumes:
      - "./database/farming_db/:/var/lib/mysql" #local
      - farming_db:/var/lib/mysql/data #docker
    ports:
      - "3306:3306"
    container_name: farming_mysql
    networks:
      - backend-network


When I run docker-compose up This is the error:当我运行docker-compose up这是错误:

Attaching to farming_mysql, farming_server_springboot_1
farming_mysql | 2021-03-18 07:03:20+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
farming_mysql | 2021-03-18 07:03:20+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
farming_mysql | 2021-03-18 07:03:20+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
farming_mysql | 2021-03-18 07:03:21+00:00 [Note] [Entrypoint]: Initializing database files
farming_mysql | 2021-03-18T07:03:21.058436Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server opti
on (see documentation for more details).
farming_mysql | 2021-03-18T07:03:21.063630Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
farming_mysql | 2021-03-18T07:03:21.063710Z 0 [ERROR] Aborting
farming_mysql |
farming_mysql exited with code 1
springboot_1  |

I understood that my directory is not empty.我知道我的目录不是空的。 I am trying to use "./database/farming_db/:/var/lib/mysql" and "farming_db:/var/lib/mysql/data" both as the volume directories.我正在尝试将“./database/farming_db/:/var/lib/mysql”和“farming_db:/var/lib/mysql/data”都用作卷目录。 I think the problem is with the latter directory because the prior directory is empty.我认为问题出在后一个目录上,因为前一个目录是空的。 I'm having a problem deleting the contents in the latter directory because I don't know how to access it.我在删除后一个目录中的内容时遇到问题,因为我不知道如何访问它。

So this is what I've tried:所以这就是我尝试过的:

  1. I deleted all the containers and then deleted all the volumes.我删除了所有容器,然后删除了所有卷。 docker volume prune but didn't work. docker volume prune但没有用。

  2. I searched that I could do rm -rf /usr/local/var/mysql but I don't know where I can execute this command since the container won't run properly at all.我搜索了我可以执行rm -rf /usr/local/var/mysql但我不知道在哪里可以执行此命令,因为容器根本无法正常运行。

  3. I deleted the mysql image and just ran docker-compose up again.我删除了 mysql 映像,然后再次运行docker-compose up This seems to pull a new mysql image from somewhere?这似乎从某个地方拉了一个新的 mysql 图像? but I still get the same error.但我仍然得到同样的错误。 I guess volume directory has nothing do with the docker image itself.我猜卷目录与 docker 映像本身无关。

  4. I deleted the "- farming_db:/var/lib/mysql/data #docker" line from the docker-compose.我从 docker-compose 中删除了“-farming_db:/var/lib/mysql/data #docker”行。 But the same error is still occuring!但同样的错误仍在发生!

I'm using Windows10.我正在使用Windows10。

My question:我的问题:

  1. How can I access the directory?如何访问目录? I don't know where to use the rm -rf command.我不知道在哪里使用 rm -rf 命令。
  2. Why does this error still occur even when I erase "- farming_db:/var/lib/mysql/data #docker" from the docker-compose?为什么即使我从 docker-compose 中删除“-farming_db:/var/lib/mysql/data #docker”,仍然会出现此错误?
  3. And also could anyone explain what I am doing?还有谁能解释我在做什么? I'm new to docker and I don't really understand these volume problems.我是 docker 的新手,我不太了解这些音量问题。

This line indicate that mysql container is storing the data inside a directory database in the same directory than your docker-compose.yml :此行表明 mysql 容器将数据存储在与docker-compose.yml相同的目录中的目录database中:

    volumes:
      - "./database/farming_db/:/var/lib/mysql" #local

This kind of volume isn't managed by Docker, it's just a directory in your filesystem, this is why docker volume prune doesn't work.这种卷不是由 Docker 管理的,它只是文件系统中的一个目录,这就是docker volume prune不起作用的原因。 I know that, because it starts with a "path" relative or absolute.我知道,因为它以相对或绝对的“路径”开头。

The other volume, farming_db , are managed by Docker.另一个卷, farming_db ,由 Docker 管理。 I know that because it starts with a simple name.我知道,因为它以一个简单的名称开头。 This kinds of volume are managed by Docker and are removed with prune .这种卷由 Docker 管理并用prune删除。

So, answering:所以,回答:

  1. In the same directory than your docker-compose.yml you can remove that database folder.在与docker-compose.yml相同的目录中,您可以删除该database文件夹。
  2. Because the first volume, the one with /var/lib/mysql still exists.因为第一个卷,带有/var/lib/mysql的那个仍然存在。 MySQL keeps all files inside this directory and any other child directory are a database. MySQL 将所有文件保存在此目录中,任何其他子目录都是数据库。
  3. You're just trying to put a container running and docker-compose hides a lot of details.你只是想让一个容器运行,而docker-compose隐藏了很多细节。
  4. This is just a detail, but MYSQL_USER should be different than root .这只是一个细节,但MYSQL_USER应该不同于root

You can let Docker manage the entire volume, creating a single volume to hold all data, in this case I named it as mysql_data :您可以让 Docker 管理整个卷,创建一个单独的卷来保存所有数据,在这种情况下,我将其命名为mysql_data

    volumes:
    - mysql_data:/var/lib/mysql

Or, you can explore a bit more the docker run equivalent command to get used with it:或者,您可以进一步探索docker run等效命令来使用它:

docker run -d --name mysql \
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_DATABASE=farming_db \
-e MYSQL_USER=myuser \
-e MYSQL_PASSWORD=mypass \
-v mysql_data:/var/lib/mysql \
-p 3306:3306 \
mysql:5.7

Run docker system prune --volumes运行docker system prune --volumes

This frees up the memory by removing all unused containers.这通过删除所有未使用的容器来释放 memory。 Sometimes, the mentioned issue can occur due to memory limitations有时,由于 memory 限制,可能会出现上述问题

Generally I emptied the volume's data directory and just changed the versions of the MySQL.一般来说,我清空了卷的数据目录,只是更改了 MySQL 的版本。 So in steps:所以分步骤:

  1. empty volume directory content空卷目录内容
  1. modify docker-compose.yml mysql version from 5.7 to 5.7.16将 docker-compose.yml mysql 版本从 5.7 修改为 5.7.16

As vencedor's answer, it worked for me.作为 vencedor 的回答,它对我有用。 If anyone need stay with mysql 5.7, you can add these lines to your db service in docker-compose.yml:如果有人需要使用 mysql 5.7,您可以在 docker-compose.yml 中将这些行添加到您的数据库服务中:

 - /etc/group:/etc/group:ro
 - /etc/passwd:/etc/passwd:ro
user: "1000:1000"

I used docker-compose to run mysql image and encountered the error.我使用 docker-compose 运行 mysql 映像并遇到错误。 I use the following configuration to set volume.我使用以下配置来设置音量。 -./mysql/data:/var/lib/mysql/data -./mysql/data:/var/lib/mysql/data

Then I changed it to the following and the error was solved.然后我把它改成下面的,错误就解决了。 -./mysql:/var/lib/mysql -./mysql:/var/lib/mysql

I found on my Windows system that although my local volume (ie/db) looked empty it really wasn't.我在我的 Windows 系统上发现,虽然我的本地卷 (ie/db) 看起来是空的,但实际上并非如此。 I wound up running ls -la from a bash shell on the same folder it showed a.sock file in it.我最终从 bash shell 运行ls -la在它显示一个 .sock 文件的同一文件夹中。 Once I removed ( rm -f *.sock ) that and ran docker-compose up --build it seemed to mount the volume properly.一旦我删除( rm -f *.sock )并运行docker-compose up --build它似乎正确安装了卷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM