DockerのVolumeを使用してMySQLデータを永続化する

## Volume
ボリュームは、Docker コンテナによって生成および使用されるデータを永続化するための推奨メカニズムです。バインド マウントはホスト マシンのディレクトリ構造と OS に依存しますが、ボリュームは Docker によって完全に管理されます。 
## MySQLデータの永続化
MySQLのイメージをPULLする(前回の記事を参照)
```shell-session
$ docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mysql        latest    56b21e040954   5 weeks ago   632MB
$ docker volume ls
DRIVER    VOLUME NAME
local     b333469b1329138728f70a9f6f74f66b19ffa3da0c878bb5e92017aa5b616bde
```
Volume「mysql-data」を使用してMySQLを起動する
```shell-session
$ docker container run --name my-mysql -e MYSQL_ROOT_PASSWORD=admin -d -p 3306:3306 -v mysql-data:/var/lib/mysql mysql
8e1983d1ea2ec7922392a4fca62caaadf810bb1e30ba947e7e4d5577ff4fff5b
$ docker container ls -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS
                            NAMES
8e1983d1ea2e   mysql     "docker-entrypoint.s…"   25 seconds ago   Up 24 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   my-mysql
$ docker volume ls
DRIVER    VOLUME NAME
local     b333469b1329138728f70a9f6f74f66b19ffa3da0c878bb5e92017aa5b616bde
local     mysql-data
```
MySQLにデータベースを作成する
```shell-session
$ docker container exec -it my-mysql sh
sh-4.4# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.3.0 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

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 |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> CREATE DATABASE my_db_01 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected, 2 warnings (0.00 sec)

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

mysql> exit
Bye
sh-4.4# exit
```
Volume の内容を確認する(my_db_01が作成されたこと)
```shell-session
$ docker volume ls
DRIVER    VOLUME NAME
local     b333469b1329138728f70a9f6f74f66b19ffa3da0c878bb5e92017aa5b616bde
local     mysql-data
$ docker volume inspect mysql-data
[
    {
        "CreatedAt": "2024-02-24T13:44:47+09:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/mysql-data/_data",
        "Name": "mysql-data",
        "Options": null,
        "Scope": "local"
    }
]
$ sudo ls /var/lib/docker/volumes/mysql-data/_data
[sudo] password for dxrdxr:
'#ib_16384_0.dblwr'   binlog.000001   client-cert.pem   *my_db_01*             private_key.pem   undo_001
'#ib_16384_1.dblwr'   binlog.000002   client-key.pem    mysql                public_key.pem    undo_002
'#innodb_redo'        binlog.index    ib_buffer_pool    mysql.ibd            server-cert.pem
'#innodb_temp'        ca-key.pem      ibdata1           mysql.sock           server-key.pem
 auto.cnf             ca.pem          ibtmp1            performance_schema   sys
```
MySQLのコンテナを削除して再作成する
```shell-session
$  docker container ls
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS
                            NAMES
8e1983d1ea2e   mysql     "docker-entrypoint.s…"   15 minutes ago   Up 15 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   my-mysql
$ docker container stop my-mysql
my-mysql
$ docker container rm my-mysql
my-mysql
$ docker container ls -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
$ docker container run --name my-mysql -e MYSQL_ROOT_PASSWORD=admin -d -p 3306:3306 -v mysql-data:/var/lib/mysql mysql
64b96952639ad771ba7a758599df20ba490482aaeae23366c1c7e12fcdd23025
$ docker container ls
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS
                            NAMES
64b96952639a   mysql     "docker-entrypoint.s…"   23 seconds ago   Up 22 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   my-mysql
```
再起動したMySQLにmy_db_01があること
```shell-session
$ docker container exec -it my-mysql sh
sh-4.4# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.3.0 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

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 |
| my_db_01           |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql>
```