2
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のmysqlにCSVをインポートする

Posted at

初期データ等を投入する際に使えそうだったのでメモ。

パッケージ構成

.
├── docker
│   └── db
│       ├── Dockerfile
│       ├── ddl
│       │   └── test.sql
│       └── dml
│           └── test.csv
└── docker-compose.yml

docker/db/ddl/test.sqlの中身。

CREATE TABLE IF NOT EXISTS test
(
    id bigint NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name varchar(256) NOT NULL,
    category varchar(256) NOT NULL,
    created datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    created_user varchar(128) NOT NULL,
    updated datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    updated_user varchar(128) NOT NULL
) CHARSET=utf8mb4 COMMENT='テストテーブル' ;

docker/db/dml/test.csvの中身。

名前,種類
大根,根菜類
はくさい,葉茎菜類
キャベツ,葉茎菜類
きゅうり,果菜類
トマト,果菜類

docker-compose.ymlの中身

services:
    db:
        image: mysql:5.7
        container_name: test-mysql
        build:
            context: .
            dockerfile: ./docker/db/Dockerfile
        environment:
            MYSQL_ROOT_PASSWORD: root
            MYSQL_DATABASE: test
            MYSQL_USER: test
            MYSQL_PASSWORD: test
            TZ: 'Asia/Tokyo'
        command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --innodb-use-native-aio=0
        volumes:
            - ./docker/db/data:/var/lib/mysql
            - ./docker/db/my.cnf:/etc/mysql/conf.d/my.cnf
            - ./docker/db/sql:/docker-entrypoint-initdb.d
            - ./docker/db/dml:/tmp/dml # ./docker/db/dmlをマウント
        user: 1000:1000
        ports:
            - 3306:3306
            - 3307:3306

データ投入

# docker立ち上げ
$ docker-compose up -d --build

# mysqlにテーブル追加
$ docker exec -it test-mysql mysql -u test -p -h localhost test -e"$(cat docker/db/ddl/test.sql)"

# 追加したテーブルに対してLOAD DATA LOCAL INFILEでデータを流す。
docker exec -it test-mysql mysql -u test -p -h localhost test -e "LOAD DATA LOCAL INFILE \"/tmp/dml/test.csv\" INTO TABLE test FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'  LINES TERMINATED BY '\n' IGNORE 1 LINES (@name, @category) SET name=@name, category=@category, created_user='test', updated_user='test';"

# データ確認
$ docker exec -it test-mysql mysql -u test -p -h localhost test -e "SELECT * FROM test;"
+----+--------------+--------------+---------------------+--------------+---------------------+--------------+
| id | name         | category     | created             | created_user | updated             | updated_user |
+----+--------------+--------------+---------------------+--------------+---------------------+--------------+
|  1 | 大根         | 根菜類       | 2023-02-19 16:22:42 | test         | 2023-02-19 16:22:42 | test         |
|  2 | はくさい     | 葉茎菜類     | 2023-02-19 16:22:42 | test         | 2023-02-19 16:22:42 | test         |
|  3 | キャベツ     | 葉茎菜類     | 2023-02-19 16:22:42 | test         | 2023-02-19 16:22:42 | test         |
|  4 | きゅうり     | 果菜類       | 2023-02-19 16:22:42 | test         | 2023-02-19 16:22:42 | test         |
|  5 | トマト       | 果菜類       | 2023-02-19 16:22:42 | test         | 2023-02-19 16:22:42 | test         |
+----+--------------+--------------+---------------------+--------------+---------------------+--------------+

データ登録されてました。
色々使えそう。

2
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
2
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?