概要
O'Reillyから出版されてる初めてのSQL 第三版で使用するサンプルDBとしてMySQLが提供しているsakila
を使用するため、docker-composeとMySQL8.0のイメージを使用してサクッとsakila入りの環境を作れるようにする
※Mac M2で作業してます
参考資料
構築はこちらの記事をほぼ参考にさせて頂きました。
ありがとうございました。
https://zenn.dev/eevee_eevee/articles/663bc9725a7a9d
構築手順
1. プロジェクトのディレクトリやファイルを作成
% mkdir mysql-sakila && cd mysql-sakila && touch docker-compose.yml && mkdir initdb
mysql-sakila % ls
docker-compose.yml initdb
2. sakilaのファイルをダウンロード
ダウンロード元は公式のExample Databasesのsakila database
です
https://dev.mysql.com/doc/index-other.html
mysql-sakila % cd initdb
initdb % curl -O 'https://downloads.mysql.com/docs/sakila-db.zip'
initdb % unzip -j sakila-db.zip && rm sakila-db.zip
Archive: sakila-db.zip
inflating: sakila-data.sql
inflating: sakila-schema.sql
inflating: sakila.mwb
3. 解凍したinitdb内のファイルをリネーム
sakila-schema.sql
を元にテーブルを構築し、そこにsakila-data.sql
のデータを流し込むが、ファイルの並び順がデフォルトだとsakila-data.sql
が先にきてしまい実行順が逆のためテーブル構築に失敗してしまうため
initdb % mv sakila-schema.sql 1.sakila-schema.sql && mv sakila-data.sql 2.sakila-data.sql
4. docker-compose.yml作成
docker-compose.yml
version: '3.2'
services:
mysql:
container_name: mysql-sakiladb
image: mysql:8.0
platform: linux/x86_64 # M1 M2対応
volumes:
- ./initdb:/docker-entrypoint-initdb.d # 初期データ(sakila)を投入
environment:
MYSQL_ROOT_PASSWORD: root
TZ: 'Asia/Tokyo'
LANG: 'ja_JP.UTF-8' # 日本語対応
ports:
- 3306:3306
# Linux Native AIO interfaceはM2向けDockerではサポートされていないので無効化
command: mysqld --innodb_use_native_aio=0
5. コンテナ構築し、MySQLにログイン
mysql-sakila % docker-compose up
コンテナに入り、mysqlに入れるか確認(passwordはdocker-compose.ymlに書いてあるのを入力)
mysql-sakila % docker exec -it mysql-sakiladb bash
bash-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.0.36 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>
6. sakila確認
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
+--------------------+
5 rows in set (0.07 sec)
mysql> use sakila;
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_sakila |
+----------------------------+
| actor |
| actor_info |
| address |
| category |
| city |
| country |
| customer |
| customer_list |
| film |
| film_actor |
| film_category |
| film_list |
| film_text |
| inventory |
| language |
| nicer_but_slower_film_list |
| payment |
| rental |
| sales_by_film_category |
| sales_by_store |
| staff |
| staff_list |
| store |
+----------------------------+
23 rows in set (0.02 sec)
mysql> select * from actor limit 5\G
*************************** 1. row ***************************
actor_id: 1
first_name: PENELOPE
last_name: GUINESS
last_update: 2006-02-15 04:34:33
*************************** 2. row ***************************
actor_id: 2
first_name: NICK
last_name: WAHLBERG
last_update: 2006-02-15 04:34:33
*************************** 3. row ***************************
actor_id: 3
first_name: ED
last_name: CHASE
last_update: 2006-02-15 04:34:33
*************************** 4. row ***************************
actor_id: 4
first_name: JENNIFER
last_name: DAVIS
last_update: 2006-02-15 04:34:33
*************************** 5. row ***************************
actor_id: 5
first_name: JOHNNY
last_name: LOLLOBRIGIDA
last_update: 2006-02-15 04:34:33
5 rows in set (0.01 sec)
mysql>