0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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

Last updated at Posted at 2024-02-24

bind mount

バインド マウントはホスト マシン上のファイルまたはディレクトリがコンテナにマウントされます。ファイルまたはディレクトリは、ホスト マシン上の絶対パスによって参照されます。対照的に、ボリュームを使用する場合、ホスト マシン上の Docker のストレージ ディレクトリ内に新しいディレクトリが作成され、Docker がそのディレクトリの内容を管理します。

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

Dockerのbind mountを使用してMySQLのコンテナを作成する

$ pwd
/home/dxrdxr/data
$ docker container run --name your-mysql -e MYSQL_ROOT_PASSWORD=admin -d -p 3306:3306 -v /home/dxrdxr/data:/var/lib/mysql mysql
a0cb29730448c71b424ba1c3e6478adcde5f570ba9e9ebf2350de7930db7cc64
$ docker container ls -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS
                            NAMES
a0cb29730448   mysql     "docker-entrypoint.s…"   35 seconds ago   Up 33 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   your-mysql

データベースを作成する

$ docker container exec -it your-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 9
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 your_db_01 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
]Query OK, 1 row affected, 2 warnings (0.02 sec)

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

mysql>

mountのディレクトリを確認する

$ pwd
/home/dxrdxr/data
$ ll
total 100724
-rw-r-----  1    999 docker   196608 Feb 24 21:57 '#ib_16384_0.dblwr'
-rw-r-----  1    999 docker  8585216 Feb 24 21:51 '#ib_16384_1.dblwr'
drwxr-x---  2    999 docker     4096 Feb 24 21:51 '#innodb_redo'/
drwxr-x---  2    999 docker     4096 Feb 24 21:51 '#innodb_temp'/
drwxr-xr-x  8    999 dxrdxr     4096 Feb 24 21:57  ./
drwxr-x--- 14 dxrdxr dxrdxr     4096 Feb 24 21:48  ../
-rw-r-----  1    999 docker       56 Feb 24 21:51  auto.cnf
-rw-r-----  1    999 docker  3040246 Feb 24 21:51  binlog.000001
-rw-r-----  1    999 docker      414 Feb 24 21:57  binlog.000002
-rw-r-----  1    999 docker       32 Feb 24 21:51  binlog.index
-rw-------  1    999 docker     1680 Feb 24 21:51  ca-key.pem
-rw-r--r--  1    999 docker     1108 Feb 24 21:51  ca.pem
-rw-r--r--  1    999 docker     1108 Feb 24 21:51  client-cert.pem
-rw-------  1    999 docker     1680 Feb 24 21:51  client-key.pem
-rw-r-----  1    999 docker     5746 Feb 24 21:51  ib_buffer_pool
-rw-r-----  1    999 docker 12582912 Feb 24 21:57  ibdata1
-rw-r-----  1    999 docker 12582912 Feb 24 21:51  ibtmp1
drwxr-x---  2    999 docker     4096 Feb 24 21:51  mysql/
-rw-r-----  1    999 docker 32505856 Feb 24 21:57  mysql.ibd
lrwxrwxrwx  1    999 docker       27 Feb 24 21:51  mysql.sock -> /var/run/mysqld/mysqld.sock
drwxr-x---  2    999 docker     4096 Feb 24 21:51  performance_schema/
-rw-------  1    999 docker     1680 Feb 24 21:51  private_key.pem
-rw-r--r--  1    999 docker      452 Feb 24 21:51  public_key.pem
-rw-r--r--  1    999 docker     1108 Feb 24 21:51  server-cert.pem
-rw-------  1    999 docker     1676 Feb 24 21:51  server-key.pem
drwxr-x---  2    999 docker     4096 Feb 24 21:51  sys/
-rw-r-----  1    999 docker 16777216 Feb 24 21:57  undo_001
-rw-r-----  1    999 docker 16777216 Feb 24 21:53  undo_002
drwxr-x---  2    999 docker     4096 Feb 24 21:57  your_db_01/

MySQLのコンテナを削除する

$ docker container ls -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS
                            NAMES
a0cb29730448   mysql     "docker-entrypoint.s…"   16 minutes ago   Up 16 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   your-mysql
$ docker container stop your-mysql
your-mysql
$ docker container ls -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

Dockerのbind mountを使用してMySQLのコンテナを再作成する

$ docker container run --name your-mysql -e MYSQL_ROOT_PASSWORD=admin -d -p 3306:3306 -v /home/dxrdxr/data:/var/lib/mysql mysql
bf35b36c27c281d619a44fc035172bdd4f0c69014808c9b369fece88f8e9290f
$ docker container ls
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS
                                    NAMES
bf35b36c27c2   mysql     "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   your-mysql

MySQLのデータベースを確認する

$ docker container exec -it your-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                |
| *your_db_01*        |
+--------------------+
5 rows in set (0.00 sec)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?