LoginSignup
1
3

More than 1 year has passed since last update.

MySQL環境をDokcer上に構築するまで

Posted at

MySQL環境をDocker上に構築するまで

動機

  • とある授業で、SQL(データベース操作言語?)の動作についての課題が出され、「自前でデータベース環境を作って、動作を確かめながら課題を解きたい!」と思ったから
  • データベース環境の構築も、Dockerの経験も将来使うかもしれないから

Ubuntu 20.04(自分のPC)上に Docker をインストールし、Docker 上で MySQL(データベース)を構築しよう!!と思いました。

Dockerの環境を構築する

まずは以下の記事を参考にして、docker で MySQLが動作する環境を作成します。

$ sudo docker search mysql


$ sudo docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
afb6ec6fdc1c: Pull complete 
0bdc5971ba40: Pull complete 
97ae94a2c729: Pull complete 
f777521d340e: Pull complete 
1393ff7fc871: Pull complete 
a499b89994d9: Pull complete 
7ebe8eefbafe: Pull complete 
597069368ef1: Pull complete 
ce39a5501878: Pull complete 
7d545bca14bf: Pull complete 
211e5bb2ae7b: Pull complete 
5914e537c077: Pull complete 
Digest: sha256:a31a277d8d39450220c722c1302a345c84206e7fd4cdb619e7face046e89031d
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest


$ sudo docker run mysql
2020-06-09 09:01:08+00:00 \[Note\] [Entrypoint]: Entrypoint script for MySQL Server 8.0.20-1debian10 started.
2020-06-09 09:01:08+00:00 \[Note\] [Entrypoint]: Switching to dedicated user 'mysql'
2020-06-09 09:01:08+00:00 \[Note\] [Entrypoint]: Entrypoint script for MySQL Server 8.0.20-1debian10 started.
2020-06-09 09:01:08+00:00 \[ERROR\] [Entrypoint]: Database is uninitialized and password option is not specified
        You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD


$ sudo docker run -e MYSQL_ROOT_PASSWORD=password mysql
2020-06-09 09:03:16+00:00 \[Note\] [Entrypoint]: Entrypoint script for MySQL Server 8.0.20-1debian10 started.
2020-06-09 09:03:16+00:00 \[Note\] [Entrypoint]: Switching to dedicated user 'mysql'
2020-06-09 09:03:16+00:00 \[Note\] [Entrypoint]: Entrypoint script for MySQL Server 8.0.20-1debian10 started.
2020-06-09 09:03:16+00:00 \[Note\] [Entrypoint]: Initializing database files
2020-06-09T09:03:16.947816Z 0 \[Warning\] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2020-06-09T09:03:16.947918Z 0 \[System\] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.20) initializing of server in progress as process 44
2020-06-09T09:03:16.953588Z 1 \[System\] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-06-09T09:03:28.691982Z 1 \[System\] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-06-09T09:03:57.185732Z 6 \[Warning\] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.


$ sudo docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
127d80111ad6        mysql               "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes        3306/tcp, 33060/tcp   unruffled_neumann
$ sudo docker exec -it 127d80111ad6 bin/bash


root@127d80111ad6:/# 
root@127d80111ad6:/# mysql mysql -u root -p
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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> 

Docker-composeへ

ここから、以下の記事を参考にしてmysqlのテーブル作成および、データの永続化を行います。

まずは、docker内で起動しているマシンから抜けましょう

mysql> exit;
Bye
root@127d80111ad6:/# exit
exit
$

ここから、docker-composeのセットアップを行います。
・・・ですが、私のマシンではdocker-composeのバージョンが1.25.0だったため、下記のサイトを参考にしてバージョンアップを試みます。

アップデートの様子

$ sudo docker-compose -v
docker-compose version 1.25.0, build unknown
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   638  100   638    0     0   4589      0 --:--:-- --:--:-- --:--:--  4589
100 11.6M  100 11.6M    0     0  2799k      0  0:00:04  0:00:04 --:--:-- 3034k
$ sudo chmod +x /usr/local/bin/docker-compose
$ sudo docker-compose -v
docker-compose version 1.26.0, build d4451659

アップデートができました。ところで、docker-composeについて調べてみると、「バージョン3系」なるものがあるらしいのですが、私にはさっぱりでした。

追記:どうやらdocker-compose.ymlにはCompose file formatというファイルの書き方に関するフォーマットがあり、これがversion 3系だったということです。また、Docker Engineのバージョンによって、利用できるCompose file formatのバージョンが異なるため注意しましょう。詳しくは公式ドキュメントを参照ください

その後、docker-compose.ymlを下記のように作成

version: '3'
services:
        mydatabase:
                image: mysql:latest
                ports: 
                        - "3306:3306"
                volumes:
                        - ./mount/mysql_init:/docker-entrypoint-initdb.d
                        - ./mount/mysql_data:/var/lib/mysql
                environment:
                        MYSQL_ROOT_PASSWORD: password

記事で紹介されたものとはサービス名やディレクトリ名を変更してみました。特に意味はありません(反抗期)。

その後、記事を参考にしてdocker-composeの起動まで実行。参考記事のように、サンプルのテーブルを作成するためのSQL文を書きます。

$ cat mount/mysql_init/1_create.sql 
CREATE DATABASE sample;
use sample;

CREATE TABLE users (
        id INT(11) AUTO_INCREMENT NOT NULL,
        name VARCHAR(64) NOT NULL,
        PRIMARY KEY (id)
);
$ tree
.
├── about.txt
├── docker-compose.yml
└── mount
    ├── mysql_data
    └── mysql_init
        └── 1_create.sql

さて、docker-composeでdockerを起動してみましょう

$ sudo docker-compose up -d
WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

Creating network "dbgairon_default" with the default driver
Creating dbgairon_mydatabase_1 ... done

ここから、docker内で起動しているmysqlにログインしてみます。

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
451d2a7303d5        mysql:latest        "docker-entrypoint.s…"   34 seconds ago      Up 27 seconds       0.0.0.0:3306->3306/tcp, 33060/tcp   dbgairon_mydatabase_1
$ sudo docker exec -it 451d2a7303d5 bin/bash
root@451d2a7303d5:/#  mysql mysql -u root -p
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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> 

ログインできているようですね。ここから、テーブルの操作を行ってみます。

まずは、1_create.sqlで定義したテーブルからアクセスしてみましょう
データベースの操作については以下のサイトを参考にしました。

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

mysql> use sample;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+------------------+
| Tables_in_sample |
+------------------+
| users            |
+------------------+
1 row in set (0.00 sec)

sampleデータベース、usersテーブルともにしっかり作成されているようです。
ということで、ここからは課題用のデータベースを頑張って作っていきます。

追記

課題について
課題の出題範囲が変わったため、使われることはありませんでした。残念ですね。

1
3
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
1
3