3
1

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 5 years have passed since last update.

ローカル環境に docker-compose で mysql コンテナを立ち上げる

Last updated at Posted at 2019-07-06

ローカル環境に docker-compose で mysql コンテナを立ち上げる

はじめに

mysql の公式イメージを使用して、docker-compose で mysql コンテナを立ち上げた時のメモです

環境

  • macOS Mojave 10.14
  • docker 18.09.2
  • docker-compose 1.24.1

docker-compose ファイル作成

docker-compose.yml
version: "3"
services:
  database:
    image: mysql:5.7
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    environment:
      MYSQL_ROOT_PASSWORD: hoge
      MYSQL_DATABASE: testdb
    volumes:
      - /tmp/test_mysql:/var/lib/mysql
    ports:
      - 3306:3306
  • version : composeファイルのフォーマットバージョン。2019/7/6時点の最新バージョン3を指定
  • command : 日本語や絵文字に対応するため、文字コードとcollationをutf8mb4に設定
  • volumes : コンテナ内の/var/lib/mysqlにデータが書き込まれるようなので、ローカルの/tmp/test_mysqlをマウントし、データを保持できるようにしています

mysqlを起動

$ docker-compose up -d
$ docker-compose logs -f
Creating network "tmp_default" with the default driver
Creating tmp_database_1
Attaching to tmp_database_1
database_1  | Initializing database
...
(省略)
...
database_1  | 2019-07-06T09:21:11.935739Z 0 [Note] Event Scheduler: Loaded 0 events
database_1  | 2019-07-06T09:21:11.936773Z 0 [Note] mysqld: ready for connections.
database_1  | Version: '5.7.26'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)
$ docker-compose ps
       Name                 Command                State                 Ports
-------------------------------------------------------------------------------------
tmp_database_1        docker-               Up                    0.0.0.0:3306->3306/
                      entrypoint.sh mysql                         tcp, 33060/tcp
                      ...

ポート3306で起動しました!

mysqlに接続

$ mysql -u root -p -h 127.0.0.1 -P 3306 testdb
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 MySQL Community Server (GPL)

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

ログインできた!

文字コード確認

mysql> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

utf8mb4が設定できています

参考にしたサイト

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?