Docker - 编写永久数据MySQL(Docker-Compose persistent data MySQL)

2079 IT屋

I can't seem to get MySQL data to persist if I run $ docker-compose down with the following .yml

version: '2'
services:
  # other services

  data:
    container_name: flask_data
    image: mysql:latest
    volumes:
      - /var/lib/mysql
    command: "true"

  mysql:
    container_name: flask_mysql
    restart: always
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: 'test_pass' # TODO: Change this
      MYSQL_USER: 'test'
      MYSQL_PASS: 'pass'
    volumes_from:
      - data
    ports:
      - "3306:3306"

My understanding is that in my data container using volumes: - /var/lib/mysql maps it to my local machines directory where mysql stores data to the container and because of this mapping the data should persist even if the containers are destroyed. And the mysql container is just a client interface into the db and can see the local directory because of volumes_from: - data

Attempted this answer and it did not work. Docker-Compose Persistent Data Trouble

EDIT

Changed my .yml as shown below and created a the dir ./data but now when I run docker-compose up --build the mysql container wont start throws error saying

  data:
    container_name: flask_data
    image: mysql:latest
    volumes:
      - ./data:/var/lib/mysql
    command: "true"

  mysql:
    container_name: flask_mysql
    restart: always
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: 'test_pass' # TODO: Change this
      MYSQL_USER: 'test'
      MYSQL_PASS: 'pass'
    volumes_from:
      - data
    ports:
      - "3306:3306"


flask_mysql | mysqld: Can't create/write to file '/var/lib/mysql/is_writable' (Errcode: 13 - Permission denied)
flask_mysql | 2016-08-26T22:29:21.182144Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
flask_mysql | 2016-08-26T22:29:21.185392Z 0 [ERROR] --initialize specified but the data directory exists and is not writable. Aborting.
解决方案

The data container is an superfluous workaround. Data-volumes would do the trick for you. Alter your docker-compose.yml to:

version: '2'
services:
  mysql:
    container_name: flask_mysql
    restart: always
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: 'test_pass' # TODO: Change this
      MYSQL_USER: 'test'
      MYSQL_PASS: 'pass'
    volumes:
      - my-datavolume:/var/lib/mysql
volumes:
  my-datavolume:

Docker will create the volume for you in the /var/lib/docker/volumes folder. This volume persist as long as you are not typing docker-compose down -v

如果我运行 $ docker-compose down ,使用以下 .yml



 版本:'2'
服务:
#其他服务

data:
container_name:flask_data
image:mysql:latest
volumes:
- / var / lib / mysql
命令:"true"

mysql:
container_name:flask_mysql
重新启动:始终
图像:mysql:最新
环境:
MYSQL_ROOT_PASSWORD:'test_pass'#TODO:更改此
MYSQL_USER:'test'
MYSQL_PASS:'pass'
volumes_from:
- data
ports:
- "3306:3306"


我的理解是,在我的数据容器中使用卷: - / var / lib / mysql 将其映射到我的本地机器目录,其中mysql将数据存储到容器,由于此映射,即使c船员被毁。而$ code> mysql 容器只是一个客户端的数据库接口,可以看到本地目录,因为 volumes_from: - data



尝试此答案,但没有起作用。 Docker-Compose持久数据故障



编辑



更改了我的 .yml ,如下所示, dir ./ data 但是现在当我运行 docker-compose up --build mysql container wont start throws错误说



  data:
container_name:flask_data
image:mysql:最新
卷:
- ./data:/var/lib/mysql
命令:"true"

mysql:
container_name :tar_mysql
重新启动:始终
图像:mysql:最新
环境:
MYSQL_ROOT_PASSWORD:'test_pass'#TODO:更改此
MYSQL_USER:'test'
MYSQL_PASS:'pass'
volumes_from:
- data
ports:
- "3306:3306"


flask_mysql | mysqld:无法创建/写入文件'/ var / lib / mysql / is_writable'(Errcode:13 - Permission denied)
flask_mysql | 2016-08-26T22:29:21.182144Z 0 [警告]具有隐含DEFAULT值的TIMESTAMP已被弃用。请使用--explicit_defaults_for_timestamp服务器选项(有关详细信息,请参阅文档)。
flask_mysql | 2016-08-26T22:29:21.185392Z 0 [错误] - 指定了初始化,但数据目录存在且不可写。中止。

解决方案

数据容器是一种多余的解决方法。 数据卷将为您做点窍门。将您的 docker-compose.yml 更改为:



  version:'2 '
服务:
mysql:
container_name:flask_mysql
重新启动:始终
图像:mysql:最新
环境:
MYSQL_ROOT_PASSWORD:'test_pass '#TODO:更改此
MYSQL_USER:'test'
MYSQL_PASS:'pass'
volumes:
- my-datavolume:/ var / lib / mysql
卷:
my-datavolume:


Docker将为您在 / var / lib / docker / volumes 文件夹。只要你没有输入 docker-compose down -v


本文地址:IT屋 » Docker - 编写永久数据MySQL