Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么不是';"--“强制重新创建”;重建我的docker mysql映像?_Mysql_Docker_Docker Compose_Recreate - Fatal编程技术网

为什么不是';"--“强制重新创建”;重建我的docker mysql映像?

为什么不是';"--“强制重新创建”;重建我的docker mysql映像?,mysql,docker,docker-compose,recreate,Mysql,Docker,Docker Compose,Recreate,我创建了一个docker MySql 5.7映像,然后想更改几个名称。我的docker-compose.yml文件的结局是这样的 version: '3.3' services: db: image: mysql:5.7 restart: always environment: MYSQL_DATABASE: 'maps_data' # So you don't have to use root, but you can if you lik

我创建了一个docker MySql 5.7映像,然后想更改几个名称。我的docker-compose.yml文件的结局是这样的

version: '3.3'

services:
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: 'maps_data'
      # So you don't have to use root, but you can if you like
      MYSQL_USER: 'myuser'
      # You can use whatever password you like
      MYSQL_PASSWORD: 'password'
      # Password for root access
      MYSQL_ROOT_PASSWORD: 'password'
    ports:
      # <Port exposed> : < MySQL Port running inside container>
      - '3406:3306'
    expose:
      # Opens port 3406 on the container
      - '3406'
      # Where our data will be persisted
    volumes:
      - my-db:/var/lib/mysql
# Names our volume
volumes:
  my-db:
但是,当我以root用户身份登录时,我在那里看不到我的数据库“maps\u data”。在我做更改之前,我看到了旧的“db”数据库

localhost:maps davea$ mysql -u root -h 127.0.0.1 --port=3406 -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db                 |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)
完全从头开始重建docker MySql映像的正确方法是什么?

docker compose的--force recreate将删除该容器,并根据提供的映像名称/Id(在您的示例中为MySql:5.7)构建一个新容器。我想这是应该发生的。如果在docker组装之前和之后查看容器ID,您可以知道--强制重新创建。如果容器的ID发生变化,docker compose确实从mysql:5.7映像创建了一个新的容器

如果查看docker compose yaml文件,可以看到它将使用一个名为“my db”的docker卷,该卷安装在容器内的/var/lib/mysql中。Docker卷是持久性的(这是它们的主要用例之一),因此在创建新容器时,它会将“my db”卷装载回/var/lib/mysql,从而将数据持久化到新容器中

卷是保存Docker容器生成和使用的数据的首选机制


如果您想真正从头开始,您要做的是发出一个
docker compose down
(这将删除容器),并确保“my db”卷也被删除(使用docker volume ls)。如果该卷仍然存在,请使用
docker volume rm my db
删除该卷。之后,发出一个
docker compose up-d
,它将构建一个新卷(该卷将为空),新容器将在/var/lib/mysql中看不到任何文件。

通过将
--rmi
标志传递到down,告诉docker compose删除图像:

docker编写下来--rmi all

这会告诉compose停止并删除所有容器、网络,并删除docker compose文件中任何服务使用的所有图像。此外,您可以传递
--volumes
以删除所有命名卷(
my db
)。但是,这将导致删除docker compose文件中的命名卷,并且您将丢失该数据。确保这是您想要的

然后,要使用新图像重新运行应用程序,请在启动容器之前使用docker compose up的
--build
选项构建图像

docker compose up-d--build

有关更多选项,请参阅docker compose参考

localhost:maps davea$ mysql -u root -h 127.0.0.1 --port=3406 -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db                 |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)