Docker: local directory mount

1. Case: Data mounting of MySQL container

  1. Check the MySQL container to determine whether there is a data volume mounted
  2. Implement the mounting of MySQL data directory, configuration files, and initialization scripts based on the host directory
[root@172 _data]# docker inspect nginx

docker inspect nginxis a command used to get detailed information about the configuration and metadata of a Docker container or image.

When you run thedocker inspect command, it will return a JSON-formatted output that contains various information about the specified container or image, including: name, ID, creation time, Network settings, mount points, port mappings, environment variables, image information, etc.

For the docker inspect nginx command, it returns detailed information about the container named "nginx". You can use this information to understand the container's configuration, network settings, mount points, and other relevant metadata.

Mount information
Insert image description here

View data volumes
Insert image description here

Check the location where the MySQL data volume is mounted on the host, but we have not set the data volume, so it is garbled.
Insert image description here

The important data of mysql is in the host machine
Insert image description here

2. Mount MySQL to the directory we specify

MySQL will mount the data storage directory to the host, but MySQL will generate an anonymous volume. When we upgrade MySQL, we will delete the original MySQL container so that the original data is not lost (of course you can migrate the original data to the new one). After the upgraded version of mysql (anonymous volume), we can mount the container directory and the specified directory on the host.

# 挂载本地目录
-v 本地目录:容器内目录
# 挂载本地文件
-v 本地文件:容器内文件

Note: The local directory or file must start with / or ./. If it starts with the name directly, it will be recognized as the data volume name instead of the local directory name.


Implement the mounting of MySQL data directory, configuration files, and initialization scripts based on the host directory

Insert image description here

# 1.删除原来的MySQL容器
docker rm -f mysql

# 2.进入root目录
cd ~

# 3.创建并运行新mysql容器,挂载本地目录
docker run -d \
  --name mysql \
  -p 3306:3306 \
  -e TZ=Asia/Shanghai \
  -e MYSQL_ROOT_PASSWORD=123 \
  -v ./mysql/data:/var/lib/mysql \
  -v ./mysql/conf:/etc/mysql/conf.d \
  -v ./mysql/init:/docker-entrypoint-initdb.d \
  mysql

# 4.查看root目录,可以发现~/mysql/data目录已经自动创建好了
ls -l mysql
# 结果:
总用量 4
drwxr-xr-x. 2 root    root   20 5月  19 15:11 conf
drwxr-xr-x. 7 polkitd root 4096 5月  19 15:11 data
drwxr-xr-x. 2 root    root   23 5月  19 15:11 init

# 查看data目录,会发现里面有大量数据库数据,说明数据库完成了初始化
ls -l data

# 5.查看MySQL容器内数据
# 5.1.进入MySQL
docker exec -it mysql mysql -uroot -p123
# 5.2.查看编码表
show variables like "%char%";
# 5.3.结果,发现编码是utf8mb4没有问题
+--------------------------+--------------------------------+
| Variable_name            | Value                          |
+--------------------------+--------------------------------+
| character_set_client     | utf8mb4                        |
| character_set_connection | utf8mb4                        |
| character_set_database   | utf8mb4                        |
| character_set_filesystem | binary                         |
| character_set_results    | utf8mb4                        |
| character_set_server     | utf8mb4                        |
| character_set_system     | utf8mb3                        |
| character_sets_dir       | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+

# 6.查看数据
# 6.1.查看数据库
show databases;
# 结果,hmall是黑马商城数据库
+--------------------+
| Database           |
+--------------------+
| hmall              |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
# 6.2.切换到hmall数据库
use hmall;
# 6.3.查看表
show tables;
# 结果:
+-----------------+
| Tables_in_hmall |
+-----------------+
| address         |
| cart            |
| item            |
| order           |
| order_detail    |
| order_logistics |
| pay_order       |
| user            |
+-----------------+
# 6.4.查看address表数据
+----+---------+----------+--------+----------+-------------+---------------+-----------+------------+-------+
| id | user_id | province | city   | town     | mobile      | street        | contact   | is_default | notes |
+----+---------+----------+--------+----------+-------------+---------------+-----------+------------+-------+
| 59 |       1 | 北京     | 北京   | 朝阳区    | 13900112222 | 金燕龙办公楼   | 李佳诚    | 0          | NULL  |
| 60 |       1 | 北京     | 北京   | 朝阳区    | 13700221122 | 修正大厦       | 李佳红    | 0          | NULL  |
| 61 |       1 | 上海     | 上海   | 浦东新区  | 13301212233 | 航头镇航头路   | 李佳星    | 1          | NULL  |
| 63 |       1 | 广东     | 佛山   | 永春      | 13301212233 | 永春武馆       | 李晓龙    | 0          | NULL  |
+----+---------+----------+--------+----------+-------------+---------------+-----------+------------+-------+
4 rows in set (0.00 sec)

Guess you like

Origin blog.csdn.net/Blue_Pepsi_Cola/article/details/134189205