MySQLコンテナにデフォルトでデータを格納する方法は先程まとめましたが、そこそこ大きいデータをデフォルトでインサートしようとするとバルクインサートでも少し時間がかかります。そのような場合にはLOAD DATA LOCAL INFILEコマンドでインサートしたくなります。その方法をDockerで実現する方法について、ここではまとめておきます。
ファルダ構成は下記のとおりです。20_data_load.shでLOAD DATA LOCAL INFILEコマンドを実行し、インサートしています。ここでは、test_dbの中のtest_tbl1とtest_tbl2に対して、data1.csvとdata2.csvをコンテナ起動時にインサートするような流れを想定しています。
docker-compose.ymlに大きな変更はありません。
10_ddl.sqlの中身はこちらです。テーブルを作成するSQLの頭にset global local_infile = 1;を設定しています。MySQLの設定として、デフォルトでは、local_infileシステム変数がOFFなのでONにします。
➜ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c241beed989 mysql:latest "docker-entrypoint.s…" About a minute ago Up About a minute 33060/tcp, 0.0.0.0:13306->3306/tcp silly_wu
➜ docker exec -it 1c241beed989 bash
root@1c241beed989:/# 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.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> CREATE DATABASE sample;
Query OK, 1 row affected (0.01 sec)
mysql> USE sample;
Database changed
mysql> CREATE TABLE sample(id int, name varchar(255));
mysql> INSERT INTO sample values(1, 'Tanaka'), (2, 'Sato'), (3, 'Suzuki');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM sample;
+------+--------+
| id | name |
+------+--------+
| 1 | Tanaka |
| 2 | Suzuki |
| 3 | Sato |
+------+--------+
3 rows in set (0.00 sec)
mysql> exit
Bye
root@1c241beed989:/# exit
exit
➜ docker exec -it 1c241beed989 bash
root@1c241beed989:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
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> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sample |
| sys |
+--------------------+
5 rows in set (0.00 sec)
➜ docker rm -f 1c241beed989
1c241beed989
➜ docker run -e MYSQL_ROOT_PASSWORD=pass -p 13306:3306 -d mysql:latest
334434eafd7b9b5ceea5b0cc96917266d9b12bf81d2cf207cb8048f160ab8b3d
➜ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
334434eafd7b mysql:latest "docker-entrypoint.s…" 27 seconds ago Up 26 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp reverent_greider
➜ docker exec -it 334434eafd7b bash
root@334434eafd7b:/# 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.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> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> exit
Bye
root@334434eafd7b:/# exit
exit
➜ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
334434eafd7b mysql:latest "docker-entrypoint.s…" 3 hours ago Up 3 hours 33060/tcp, 0.0.0.0:13306->3306/tcp reverent_greider
➜ docker rm -f 334434eafd7b
334434eafd7b
➜ docker exec -it d4937bb0fad7 bash
root@d4937bb0fad7:/# 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.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> CREATE DATABASE sample;
Query OK, 1 row affected (0.01 sec)
mysql> USE sample;
Database changed
mysql> CREATE TABLE sample(id int, name varchar(255));
Query OK, 0 rows affected (0.03 sec)
mysql> INSERT INTO sample values(1, 'New_Tanaka'), (2, 'New_Sato'), (3, 'New_Suzuki');
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM sample;
+------+------------+
| id | name |
+------+------------+
| 1 | New_Tanaka |
| 2 | New_Sato |
| 3 | New_Suzuki |
+------+------------+
3 rows in set (0.00 sec)
mysql> exit
Bye
root@d4937bb0fad7:/# exit
exit
➜ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4937bb0fad7 mysql:latest "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 33060/tcp, 0.0.0.0:13306->3306/tcp sad_engelbart
➜ docker rm -f d4937bb0fad7
d4937bb0fad7
➜ docker run -v "$PWD/mysql_data":/var/lib/mysql -e MYSQL_ROOT_PASSWORD=pass -p 13306:3306 -d mysql:latest
43d476bea0f95c22986517ee39627bf1db30255ce22c6388ee509c5ca0cd5741
➜ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43d476bea0f9 mysql:latest "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp dreamy_kilby
➜ docker exec -it 43d476bea0f9 bash
root@43d476bea0f9:/# 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.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> 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> SELECT * FROM sample;
+------+------------+
| id | name |
+------+------------+
| 1 | New_Tanaka |
| 2 | New_Sato |
| 3 | New_Suzuki |
+------+------------+
3 rows in set (0.01 sec)
→ mkdir mysql_docker
→ cd mysql_docker
➜ tree mysql_docker
mysql_docker
├── docker-compose.yml
├── data
└── init
└── init.sql
CREATE DATABASE knock;
USE knock;
CREATE TABLE `category` (
`category_major_cd` varchar(10) NOT NULL,
`category_major_name` varchar(50) NOT NULL,
`category_medium_cd` varchar(10) NOT NULL,
`category_medium_name` varchar(50) NOT NULL,
`category_small_cd` varchar(10) NOT NULL,
`category_small_name` varchar(50) NOT NULL,
`no` int(11) NOT NULL,
PRIMARY KEY (`no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
CREATE TABLE `customer` (
`customer_id` varchar(50) NOT NULL,
`customer_name` varchar(50) NOT NULL,
`gender_cd` varchar(10) NOT NULL,
`gender` varchar(10) NOT NULL,
`birth_day` date NOT NULL,
`age` int(11) NOT NULL,
`postal_cd` varchar(10) NOT NULL,
`address` varchar(100) NOT NULL,
`application_store_cd` varchar(50) NOT NULL,
`application_date` date NOT NULL,
`status_cd` varchar(50) NOT NULL,
`no` int(11) NOT NULL,
PRIMARY KEY (`no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
CREATE TABLE `product` (
`product_cd` varchar(20) NOT NULL,
`category_major_cd` varchar(20) NOT NULL,
`category_medium_cd` varchar(20) NOT NULL,
`category_small_cd` varchar(20) NOT NULL,
`unit_price` int(11) NOT NULL,
`unit_cost` int(11) NOT NULL,
`no` int(11) NOT NULL,
PRIMARY KEY (`no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
CREATE TABLE `receipt` (
`sales_ymd` date NOT NULL,
`sales_epoch` varchar(20) NOT NULL,
`store_cd` varchar(20) NOT NULL,
`receipt_no` varchar(20) NOT NULL,
`receipt_sub_no` varchar(20) NOT NULL,
`customer_id` varchar(20) NOT NULL,
`product_cd` varchar(20) NOT NULL,
`quantity` int(11) NOT NULL,
`amount` int(11) NOT NULL,
`no` int(11) NOT NULL,
PRIMARY KEY (`no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
CREATE TABLE `store` (
`store_cd` varchar(20) NULL,
`store_name` varchar(20) NOT NULL,
`prefecture_cd` varchar(20) NOT NULL,
`no` int(11) NOT NULL,
PRIMARY KEY (`no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
INSERT INTO `category` VALUES ('04','惣菜','0401','御飯類','040101','弁当類',1),('04','惣菜','0401','御飯類','040102','寿司類',2);
INSERT INTO `customer` VALUES ('CS021313000114','大野 あや子','1','女性','1981-04-29',37,'259-1113','神奈川県伊勢原市粟窪**********','S14021','2015-09-05','0-00000000-0',1),('CS037613000071','六角 雅彦','9','不明','1952-04-01',66,'136-0076','東京都江東区南砂**********','S13037','2015-04-14','0-00000000-0',2);
INSERT INTO `product` VALUES ('P040101001','04','0401','040101',198,149,1),('P040101002','04','0401','040101',218,164,2);
INSERT INTO `receipt` VALUES ('2018-11-03','1257206400','S14006','112','1','CS006214000001','P070305012',1,158,1),('2018-11-18','1258502400','S13008','1132','2','CS008415000097','P070701017',1,81,2);
INSERT INTO `store` VALUES ('0000-00-00','千草台店','12',1),('0000-00-00','国分寺店','13',2);
➜ docker-compose up -d
Creating mysql_docker_mysql_1 ... done
➜ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
064a80209c5c mysql:8.0.20 "docker-entrypoint.s…" 18 seconds ago Up 17 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp mysql_docker_mysql_1
➜ docker exec -it 064a80209c5c bash
root@064a80209c5c:/# 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.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> use knock;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
mysql> INSERT INTO `product` VALUES ('P040101003','04','0401','040101',230,173,3);
Query OK, 1 row affected (0.00 sec)
mysql> select * from knock.product;
+------------+-------------------+--------------------+-------------------+------------+-----------+----+
| product_cd | category_major_cd | category_medium_cd | category_small_cd | unit_price | unit_cost | no |
+------------+-------------------+--------------------+-------------------+------------+-----------+----+
| P040101001 | 04 | 0401 | 040101 | 198 | 149 | 1 |
| P040101002 | 04 | 0401 | 040101 | 218 | 164 | 2 |
| P040101003 | 04 | 0401 | 040101 | 230 | 173 | 3 |
+------------+-------------------+--------------------+-------------------+------------+-----------+----+
3 rows in set (0.00 sec)
mysql> exit
Bye
root@064a80209c5c:/# exit
exit
➜ docker-compose stop
Stopping mysql_docker_mysql_1 ... done
➜ docker-compose rm
Going to remove mysql_docker_mysql_1
Are you sure? [yN] y
Removing mysql_docker_mysql_1 ... done
➜ docker-compose up -d
Creating mysql_docker_mysql_1 ... done
➜ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db05ef019a2e mysql:8.0.20 "docker-entrypoint.s…" 15 seconds ago Up 14 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp mysql_docker_mysql_1
➜ docker exec -it db05ef019a2e bash
root@db05ef019a2e:/# 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.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.
➜ cat ~/Desktop/mysql/init/10_ddl.sql
set global local_infile = 1;
create table if not exists test_tbl1(
`code` char(3) not null,
`name` varchar(80) not null,
primary key(`code`)
) engine=innodb default charset=utf8;
create table if not exists test_tbl2(
`code` char(3) not null,
`age` int not null,
primary key(`code`)
) engine=innodb default charset=utf8;
➜ cat ~/Desktop/mysql/init/20_data_load.sh
mysql -uroot -pPass --local-infile=1 test_db -e "LOAD DATA LOCAL INFILE '/docker-entrypoint-initdb.d/data1.csv' INTO TABLE test_tbl1 FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES"
mysql -uroot -pPass --local-infile=1 test_db -e "LOAD DATA LOCAL INFILE '/docker-entrypoint-initdb.d/data2.csv' INTO TABLE test_tbl2 FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES"
➜ docker-compose up -d
Creating desktop_mysql_1 ... done
➜ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce0bcf0f20db mysql:8.0.20 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp desktop_mysql_1
➜ docker exec -it ce0bcf0f20db bash
root@ce0bcf0f20db:/# 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.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> use test_db
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> select * from test_tbl1;
+------+-----------+
| code | name |
+------+-----------+
| 001 | Tanaka |
| 002 | Sato |
| 003 | Suzuki |
| 004 | Takahashi |
+------+-----------+
4 rows in set (0.01 sec)
mysql> select * from test_tbl2;
+------+-----+
| code | age |
+------+-----+
| 001 | 10 |
| 002 | 20 |
| 003 | 30 |
| 004 | 40 |
+------+-----+
4 rows in set (0.01 sec)